在Unity中更改Button的文本

时间:2018-10-29 03:14:30

标签: c# unity3d button

在Unity中打开场景时,我试图更改Button的文本。我尝试了其他方法来更改Button的文本。

这是我的代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;

public class script : MonoBehaviour {

    public Button b1;
    public TMP_Text b1text;


    void Start()
    {
        GameObject b1object = (GameObject)Instantiate(b1);
        b1text = b1object.GetComponentInChildren<TMP_Text>(true);
        btnValue();
    }

    public void btnValue()
    {
        b1text.text = "sdfa";
    }

}

1 个答案:

答案 0 :(得分:2)

   public Button b1;
public TextMeshProUGUI b1text;


void Start()
{
    b1text = b1.GetComponentInChildren<TextMeshProUGUI>();
    btnValue();
}

public void btnValue()
{
    b1text.text = "sdfa";
}

或者您可以创建一个

  

公共TextMeshProUGUI txt;

在检查器中拖动textmeshpro文本并更改文本。

    txt.text = "sdfa";