【发布时间】:2014-12-16 17:44:55
【问题描述】:
我已经尝试了好几天没有成功。如何使用 C# 脚本更改 Unity 4.6 按钮的文本?任何建议表示赞赏。
【问题讨论】:
我已经尝试了好几天没有成功。如何使用 C# 脚本更改 Unity 4.6 按钮的文本?任何建议表示赞赏。
【问题讨论】:
创建一个脚本并将其添加到您的 UIButton 您应该添加参考 Unity.UI
using UnityEngine.UI;
然后声明一个变量
Text yourButtonText;
关于函数Start put this
void Start()
{
yourButtonText = transform.FindChild("Text").GetComponent<Text>();
}
然后当你想改变你的文字添加这个
yourButtonText.text = "i am a button!";
如果您需要应用更多修改,请访问http://docs.unity3d.com/ScriptReference/UI.Text.html
[带标签的版本]
添加此参考
using System.Collections.Generic;
public Text[] yourButtonTextArrays = new Text[15];
void Start()
{
for (int i = 0; i < 15; i++ )
{
yourButtonTextArrays[i] = GameObject.FindGameObjectWithTag("Button" + i+1.ToString()).transform.FindChild("Text").GetComponent<Text>();
}
}
然后当你想改变你的文字添加这个
yourButtonTextArrays[yourButtonNumber].text = "i am a button from the array of buttons";
【讨论】: