有没有办法用textmesh脚本循环文本?
我对循环文本感兴趣,这样我就可以在 2D 游戏中显示不同的文本。
我已经尝试过这个网站上的脚本。但我认为这不起作用。也许我做错了什么?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class CyclingText : MonoBehaviour
{
public TMP_Text text;
void Update()
{
TextMeshPro textmeshPro = GetComponent<TextMeshPro>();
textmeshPro.SetText("The first number is {0} and the 2nd is {1:2} and the 3rd is {3:0}.", 4, 6.345f, 3.5f);
// The text displayed will be:
// The first number is 4 and the 2nd is 6.35 and the 3rd is 4.
}
}
Run Code Online (Sandbox Code Playgroud)
有人可以给我答案或解释我做错了什么吗?
你似乎对使用有点困惑TMP
1
如果您在检查器中将 TMP 设置为公开,您可以简单地
public TMP_text text;
Run Code Online (Sandbox Code Playgroud)
然后将文本拖到检查器中,然后简单地:
text.text = "Text displayed";
Run Code Online (Sandbox Code Playgroud)
2
如果您不创建公共变量而是创建私有变量,您可以:
private TMP_text text;
Run Code Online (Sandbox Code Playgroud)
然后,如果您的脚本位于文本对象中,您可以:
text = this.gameObject.GetComponent<TextMeshPro>();
Run Code Online (Sandbox Code Playgroud)
和
text.text = "Your text";
Run Code Online (Sandbox Code Playgroud)