调整Unity gui 示例,您可以通过将该文本存储在变量中并在单击按钮时更改它来修改按钮文本,如下所示:
using UnityEngine;
using System.Collections;
public class GUITest : MonoBehaviour {
public string ButtonText = "Click Me"
void OnGUI () {
if (GUI.Button (new Rect (10,10,150,100), ButtonText )) {
ButtonText = "Huzzah!";
}
}
}
该按钮将首先读取为“单击我”,然后将更改一次为“Huzzah”。
如果您不想更改按钮中的实际文本,它会变得有点困难。您需要创建一个位于按钮上方的标签,我不建议您走这条路。它看起来不太好,标签不会随按钮移动:
using UnityEngine;
using System.Collections;
public class GUITest : MonoBehaviour {
public bool DrawLabel = false;
public string LabelText = "Huzzah"
void OnGUI () {
if (GUI.Button (new Rect (10,10,150,100), "Click Me")) {
DrawLabel = true;
}
if(DrawLabel){
// use the same rect parameters as you did to create the button
GUI.Label (new Rect (10, 10, 150,100), LabelText);
}
}
}