Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
193 views
in Technique[技术] by (71.8m points)

c# - Set Text with textmesh pro

Is there any way to cycle text with textmesh script?

I'm interested in cycling text so i can display different text on my 2D game.

I have try the script on this website. But I don't think it works. Maybe I'm doing something wrong?

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.
    }   
}

Can someone provide me with an answer or explained what I done wrong?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You seem a bit confused about the use of TMP

1

If you set the TMP in the inspector as public you can simply

public TMP_text text;

And then simply drag the text inside the inspector and simply:

text.text = "Text displayed";

2

If you don't create a public variable but a private, you can:

private TMP_text text;

And then, if you script is in the text object you can:

text = this.gameObject.GetCOmponent<TextMeshPro>();

And

text.text = "Your text";

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...