I am creating an animation in my Unity Game using Playable Directors and Animators, and i need to animate the contents of a TextMeshPro so it changes its text within keyframe, but there is no Text property in the animator.
I initialy thought that this was just with TextMeshPro, and changed my component to a normal Text component. No property found either.
My last try was to create a new script called TextAnimator, and a string property that changes the TextMeshPro contents in editor using the [ExecuteInEditMode] annotation.
But when i tried to animate THIS property, it doesnt show up either!
Here is my component in game:
Here is when i try to add the string property:
Here is the TextAnimator:
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
[ExecuteInEditMode]
public class TextAnimator : MonoBehaviour
{
public TextMeshProUGUI textUI;
[TextArea]
public string text;
void Update()
{
if (textUI) {
textUI.text = text;
}
}
}
It seems that Unity can't animate strings at all, why is that? And what can i do as a workaround to make my animations?
There is a simple solution without code. Consider using DOTween asset as alternative. Just add "DOTWeen Animation" component to your text object, set the animation type to "Text" and write the sentence that should gradually appear in "To" input field. See screenshot
Related
Problem consist in incorrect background looping. Instead of scrolling the picture horizontally, it distort over time. This script correctly work with another material, but not with that one.
How it`s looks like (Right side is distorted):
I have this backgroundObject:
This background script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BackgroundLoop : MonoBehaviour
{
public float bgSpeed;
public Renderer bgRend;
void Update()
{
bgRend.material.mainTextureOffset += new Vector2(bgSpeed * Time.deltaTime, 0f);
}
}
Thanks for any help.
Select the texture in the project window.
Then in the inspector window set Wrap Mode to Repeat (or Mirror)
Click Apply
I am creating a test project in Unity & I have an input field and a button. I would like to take the text from the input and simply Debug.Log to the Unity Console onclick of a button. All the examples show the variables and functions but don't show HOW they are getting the input to the variable. I already have UI set up including linking the function to the button. Is there some way I can give it an id like in JavaScript & HTML? I dont want to do anything fancy. Here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class main : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public void debugInput() {
Debug.Log()
}
}
I would like to pass the variable in the Debug.Log in the debugInput function.
Can someone help me?
Thanks in advance
The InputField uses a callback for entering text to an input field called OnSubmit. If you would rather print every frame the user enters new text, there is also OnValueChanged.
These callbacks can be assigned in the inspector by selecting your InputField object. The inspector should look something like the below photo.
Simply click the + button, drag in the object that has the script that has the method you would like to callback. Make sure when selecting it to choose it from the Dynamic String list as it will auto pass in the string you have in the InputField to your function. You will need to change your code to take a string parameter tho.
public void debugInput(string str)
{
Debug.Log(str)
}
If you would rather set these values through code, you can do so by adding new listener delegates to your object. I can post a snippet for this solution if you would rather keep everything in a script.
Edit: I am using a TMPro Input Field, but the above example still works for a Unity UI Input Field. Instead of OnSubmit, you can use OnEndEdit. You can also add your own EventTriggers to use OnSubmit if you want.
I am building an AR app using AR Foundation but got stuck with changing the material of my model during runtime by pressing a button.
The script below works perfectly fine when I attach it to my model and then create buttons with OnClick() Events to access the specific material WHEN my model is in the hierarchy.
But I don't want it to stay in the hierarchy because otherwise it will constantly show up in AR. If I remove it from the hierarchy, the OnClick() is obviously missing a prefab.
So I need to have the possibility to still change materials from button clicks but without my model staying in the hierarchy. I think "AddListener" could be a solution, but I don't really know how to edit the script in order to make it work. With AddListener I wouldn't have to use OnClick().
Can anyone please show me the solution?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ChangeColor : MonoBehaviour
{
public Material[] material;
Material setMaterial;
Renderer rend;
void Start()
{
rend = GetComponent<Renderer>();
rend.enabled=true;
}
void Update()
{
}
public void GreyMaterial()
{
rend.sharedMaterial = material[0];
setMaterial = rend.material;
}
public void YellowMaterial()
{
rend.sharedMaterial = material[1];
setMaterial = rend.material;
}
public void RedMaterial()
{
rend.sharedMaterial = material[2];
setMaterial = rend.material;
}
}
You can out your prefab in folder called "Resources". And get your prefab using Resources.Load in script next way:
GameObject prefab = Resources.Load<GameObject>("Path"); // Path WITHOUT "Resources/"
Here prefab is... your prefab and you get change some components and values in it.
P.S.
It is very important to write Resources letter to letter without mistakes
You may put folders into (folders into folders...) into Resources, but here you must set the exact path to your file.
I believe the script is not referencing all the new instantiated objects, its needs to reference them. Either store those new objects in a list that the script can change, or try to search for every item of that material you want to change when you run the change material function(not very efficient, but might be easier)
i found this on youtube
I am making a customizable game using Unity 2019.3f. In my main menu, I have the option for people to input a number into an input field for the amount of ammo. The input field then converts that value to an int and sets a gameobject's transform position.x equal to it. For some reason, when I input a number into the input field and click apply settings, the input field completely disappears and I have to click Play and then go back to the main menu for the input field to reappear and then somehow start working and correctly assigning the right numbers to the right spots. I used Debug.Log to see if the value of the input field were convertible to an int but they were. My code to set the gameobject transform to the input field number is here:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using TMPro;
public class GetInputValue : MonoBehaviour
{
public InputField player1ammoinput;
public InputField player2ammoinput;
public GameObject ammo1;
ammo1.transform.position = new Vector3(System.Convert.ToInt32(player1ammoinput.text), 0f, 0f);
ammo1.transform.localScale = new Vector3(System.Convert.ToInt32(player1ammoinput.text), 0f, 0f);
Debug.Log(player1ammoinput.text);
}
Can someone find out why unity is doing this? ammo1 is just the gameobject that stores the number. It is an empty Gamebject. The actual input field is a different gameobject which I refer to in this script as player1ammoinput.
If I understand this correctly you are parsing the text to an integer value, and then using that value to move the ammo1 object to the side (or towards the player).
I suggest moving the parsing to an extra variable before the actual adaptation, and think the value you get (say 22 ammo) will move the ammo1 item 22 units away, which is FAR, hence it disappears immediately from view.
I have struggle the problem two days ago. Most of solution is use CanvasRenderer, I also use it amostly achieve with the minial code:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class TextMeshCtrl : MonoBehaviour
{
public CanvasRenderer CanvasRenderer;
public Mesh meshModle;//standard Quad mesh just for test
// Update is called once per frame
void Update()
{
this.CanvasRenderer.SetMesh(meshModle);
}
}
But encounter problem is text not refresh when I change Text content.
I also test CanvasRenderer.Clear() at up of SetMesh, it clear text content, but not show text anymore.
By the way, I have another question, how to custom mesh to fit text rendering?Under Shaded Wireframe editor modle,I have seem every text char rendered as a seperation quad and I'm wonder how does unity engine render text with custom mesh and default(not has custom mesh)? Thanks