我想在点击按钮时在按钮上打印一个值。怎么可能?
[更新]
using UnityEngine;
using System.Collections;
public class tic : MonoBehaviour
{
void OnGUI() {
string value="";
if(GUI.Button(new Rect(10,10,50,50),value)) {
value="y";
print ("y");
GUI.Button(new Rect(10,10,50,50),value);
}
}
}
答案 0 :(得分:1)
调整Unity gui example您可以通过将该文本存储在变量中并在单击按钮时更改它来修改按钮文本,如下所示:
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!";
}
}
}
该按钮首先显示为“Click Me”,然后将更改为“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);
}
}
}
答案 1 :(得分:0)
你可以这样做;
var ControlsButton = GameObject.Find("ControlsButton");
ControlsButton.GetComponentInChildren<Text>().text = "Accelerometer"