1

我想在单击按钮时在按钮上打印一个值。这怎么可能?

[更新]

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); 
        } 
    } 
} 
4

2 回答 2

1

调整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);
        }
    }
}
于 2013-03-12T17:14:28.533 回答
0

你可以这样做;

var ControlsButton = GameObject.Find("ControlsButton");
ControlsButton.GetComponentInChildren<Text>().text = "Accelerometer"
于 2015-03-05T14:42:41.957 回答