【问题标题】:displaying different texts on different button clicks in unity统一在不同的按钮点击上显示不同的文本
【发布时间】:2016-11-27 06:58:28
【问题描述】:

我正在使用统一制作用户交互式选择系统,其中有表示各种产品的按钮,并且当用户单击产品时,产品名称将以系统顺序显示在另一个下方。我使用了 OnGUI() 函数来显示产品名称。但是在我的输出中,所有的名字都被叠加打印出来了。

我使用静态变量 i(最初定义为 0)增加了 GUI.label 的 y 位置。我尝试在每次单击时增加 i 的值并将其添加到 GUI.label 的 y 位置。现在,当我单击第二个按钮时,第一个按钮标签和第二个按钮标签都会移动到新坐标。

using UnityEngine;
using UnityEngine.EventSystems;// 1
using UnityEngine.UI;

``public class Example : MonoBehaviour, IPointerClickHandler // 2

// ... And many more available!
{
    SpriteRenderer sprite;
    Color target = Color.red;
    int a=200,b=100;

    public GUIText textObject;
    public bool showGUI;
    public int s=0;



    public static int i=0;



    void Awake()
    {
        sprite = GetComponent<SpriteRenderer>();
    }

    void test()
    {
        i = i + 20;

        OnGUI();
    }

    void Update()
    {
        if (sprite)
            sprite.color = Vector4.MoveTowards(sprite.color, target, Time.deltaTime * 10);
    }

    public void OnPointerClick(PointerEventData eventData) // 3
    {
        showGUI = true;
        Debug.Log(gameObject.name);
        target = Color.blue;
        PlayerPrefs.SetString ("Display", gameObject.name);
        s = 1;
        test ();


    }

    void OnGUI()
    {

        if (s == 1) {

            GUI.color = Color.red;
            GUIStyle myStyle = new GUIStyle (GUI.skin.GetStyle ("label"));
            myStyle.fontSize = 20;

            GUI.Label (new Rect (a, b, 100f, 10f), "");

            if (showGUI) {

                //GUI.Box (new Rect (a,b+i, 300f, 100f), "");
                GUI.Label (new Rect (a, b + i, 300f, 100f), gameObject.name, myStyle);

                s = 0;
            }


        }


    }



}

【问题讨论】:

    标签: c# user-interface button unity3d unity5


    【解决方案1】:

    not使用OnGUI()函数。它旨在作为程序员的工具,而不是作为将在您的游戏中运行的 UI。 .这是 Unity 中一个简单的 Button 和 Button 点击​​检测器。

    假设您需要两个按钮,下面的示例将展示如何做到这一点:

    首先,创建两个Button:

    游戏对象->UI->按钮

    其次,将UnityEngine.UI; 命名空间包含在using UnityEngine.UI; 中。

    声明Buttons变量:

    public Button button1;
    public Button button2;
    

    创建一个回调函数,当每个Button被点击时会被调用:

    private void buttonCallBack(Button buttonPressed)
    {
        if (buttonPressed == button1)
        {
            //Your code for button 1
        }
    
        if (buttonPressed == button2)
        {
            //Your code for button 2
        }
    }
    

    启用脚本后,将Buttons 连接到该回调函数(注册按钮事件)。

    void OnEnable()
    {
        //Register Button Events
        button1.onClick.AddListener(() => buttonCallBack(button1));
        button2.onClick.AddListener(() => buttonCallBack(button2));
    }
    

    当脚本被禁用时,断开Buttons 与该回调函数(取消注册按钮事件)的连接。

    void OnDisable()
    {
        //Un-Register Button Events
        button1.onClick.RemoveAllListeners();
        button2.onClick.RemoveAllListeners();
    }
    

    修改附加到按钮的文本:

    button1.GetComponentInChildren<Text>().text = "Hello1";
    button2.GetComponentInChildren<Text>().text = "Hello2";
    

    您使用GetComponentInChildren,因为创建的Text 是每个Button 的子级。我不认为我可以让这更容易理解。 Here 是 Unity UI 的教程。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-29
      • 1970-01-01
      • 2020-11-08
      • 1970-01-01
      相关资源
      最近更新 更多