检测在画布中按了哪个UI按钮? [英] Detecting which UI button was pressed within canvas?

查看:217
本文介绍了检测在画布中按了哪个UI按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的UI上有10个按钮,我要检查哪个被触摸。我使用下面的逻辑,它是工作正常,但现在我得到这个错误的某种原因:

  NullReferenceException:Object引用未设置为对象的实例
DetectButton.Start()(在Assets / Scripts / DetectButton.cs:14)

任何想法可能会发生什么?这里是我的代码(附加到画布),我使用Unity版本5.1.0f3。如果你需要任何其他信息,我会很乐意提供,提前感谢

  void Start()
{
this.GetComponent< Button>()。onClick.AddListener(()=>

{

if(this.name ==btnJogadores2)
{
print(2 jogadores);
jogadores = 2;
}
//QuantidadeJogadores(this.name);
// QuantidadePartidas name);
});
}


解决方案



一个更容易,更好的做法是为画布中的每个按钮创建10个独立的GameObject。然后为其中的所有这些按钮创建一个具有10个单独函数的单个脚本。将该脚本附加到画布。然后在按钮GameObject上选择所需功能的脚本。以下示例

  void Start(){} 
void Update(){}

public void button1()
{
Debug.Log(Button3);
}

public void button2()
{
Debug.Log(Button1);
}

public void button3()
{
Debug.Log(Button3);
}

注意:button1,button2和button3函数为3个单独的按钮



然后在你的unity检查器内:


  1. 使用按钮功能选择脚本。

  2. 此后,您的场景和按钮将正确调用分配的方法。

    >

    I have like 10 buttons on my UI and I gotta check which one was touched. I was using the following logic and it was working fine, but now I am getting this error for some reason:

     NullReferenceException: Object reference not set to an instance of an object
     DetectButton.Start () (at Assets/Scripts/DetectButton.cs:14)
    

    Any ideas what could be going on? Here is my code (attached to the canvas), and I am using Unity version 5.1.0f3. If you need any other info I will gladly provide, thanks in advance

    void Start()
     {
         this.GetComponent<Button>().onClick.AddListener(() => 
    
                                                         { 
    
             if (this.name == "btnJogadores2")
             {
                 print ("2 jogadores");
                 jogadores = 2;
             }
             //QuantidadeJogadores(this.name);
             //QuantidadePartidas(this.name);
         }); 
     }
    

    解决方案

    You don't have to all this the way you are doing.

    An Easier and good practice would be to create 10 separate GameObjects for each button inside your canvas. and then create a single script with 10 separate functions for all those buttons in it. Attach that script to you canvas. and then on the button GameObject select the script on the desired function. Sample below

    void Start() { }
    void Update() { }
    
    public void button1()
    {
        Debug.Log("Button3");
    }
    
    public void button2()
    {
        Debug.Log("Button1");
    }
    
    public void button3()
    {
        Debug.Log("Button3");
    }
    

    NOTE: button1, button2 and button3 are the functions for 3 separate buttons

    Then inside your unity Inspector:

    1. Select your script with you button functions.
    2. Assign you desired method to you button.

    After this run your scene and your button will call the assigned methods properly.

    这篇关于检测在画布中按了哪个UI按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆