Button 单击按钮时如何在按钮上打印值?

Button 单击按钮时如何在按钮上打印值?,button,unity3d,Button,Unity3d,我想在点击按钮时在按钮上打印一个值。这怎么可能 [更新] 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";

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

[更新]

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); 
        } 
    } 
} 
调整,您可以通过将按钮文本存储在变量中并在单击按钮时更改该文本来修改按钮文本,如下所示:

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);
        }
    }
}
调整,您可以通过将按钮文本存储在变量中并在单击按钮时更改该文本来修改按钮文本,如下所示:

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);
        }
    }
}
你可以这样做

var ControlsButton = GameObject.Find("ControlsButton");
ControlsButton.GetComponentInChildren<Text>().text = "Accelerometer"
var ControlsButton=GameObject.Find(“ControlsButton”); ControlsButton.getComponentChildren().text=“加速计” 你可以这样做

var ControlsButton = GameObject.Find("ControlsButton");
ControlsButton.GetComponentInChildren<Text>().text = "Accelerometer"
var ControlsButton=GameObject.Find(“ControlsButton”); ControlsButton.getComponentChildren().text=“加速计”
请看,在第一个示例中,有一个示例是您想要的。但它不是打印在按钮上,而是打印在屏幕的左下角。所以我很清楚,您想单击一个按钮,让文本显示在按钮上吗?@jerdak:是的,我想打印在按钮上。请看,在他们显示的第一个例子中,有一个例子说明了你想要什么。但它不是打印在按钮上,而是打印在屏幕的左下角。所以我很清楚,你想单击一个按钮,然后让文本显示在按钮上吗?@jerdak:是的,我想打印在按钮上。@RajeshKumar什么不起作用?如果您的原始问题包含屏幕截图或代码,或者一些指示您想要的行为与您看到的行为的东西。最初我有一个空值按钮。单击该按钮后,一些值应该打印在按钮本身上。@RajeshKumar请在您的初始问题中包含该值,以便我们可以看到出了什么问题。使用UnityEngine;使用系统集合;公共类tic:monobhavior{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);}}}@RajeshKumar我的答案仍然正确。在您的代码中,字符串值是一个局部变量,它需要在OnGUI之外声明。每次调用OnGUI时,值都会再次设置为“”。@RajeshKumar什么东西不正常?如果您的原始问题包含屏幕截图或代码,或者一些指示您想要的行为与您看到的行为的东西。最初我有一个空值按钮。单击该按钮后,一些值应该打印在按钮本身上。@RajeshKumar请在您的初始问题中包含该值,以便我们可以看到出了什么问题。使用UnityEngine;使用系统集合;公共类tic:monobhavior{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);}}}@RajeshKumar我的答案仍然正确。在您的代码中,字符串值是一个局部变量,它需要在OnGUI之外声明。每次调用OnGUI时,值都会再次设置为“”。