zoukankan      html  css  js  c++  java
  • Unity中Button按钮的触发监听事件

    第一种方式:需要把自己添加的Button按钮属性(Inspector)中的(Button)onclick添加方法。

    public void BtnCreteClick()
    {
      Debug.Log("一切正常!!");
    }

    第二种方式:把自己添加的Button按钮拖到代码所在的物体下和不拖Button按钮的代码如下。

    //拖动Button按钮执行的代码如下

    public  GameObject btnPrint;

    Button btn = btnPrint.GetComponent<Button>();
    btn.onClick.AddListener(delegate
    {

      this.BtnTestClick(btnPrint);

      //Debug.Log("委托成功!!");
    });

    void BtnTestClick(GameObject btn)
    {
      Debug.Log("测试成功!!");
    }

    //不拖动Button按钮执行的代码如下

    private GameObject btnPrint;

    btnPrint = GameObject.Find("Canvas/Button");
    btnPrint.GetComponent<Button>().onClick.AddListener(delegate
    {
      Debug.Log("测试成功!!");
    });

    第三种方式:使用Lambda 表达式实现

    private GameObject Button;

    Button = GameObject.Find("Canvas/Button");
    Button.GetComponent<Button>().onClick.AddListener(() =>
    {
      BtnTestClick(Button);

      //Debug.Log("Lambda 表达式测试正常");

    });

    void BtnTestClick(GameObject btn)
    {
      Debug.Log("测试成功!!");
    }

  • 相关阅读:
    poj1330 Nearest Common Ancestors
    poj3237 Tree
    spoj2798 QTREE3 Query on a tree again!
    spoj913 QTREE2 Query on a treeⅡ
    自动类型转换
    js "+"连接符号
    js parseFloat
    js字符串与数字的运算
    js prompt
    js数组排序
  • 原文地址:https://www.cnblogs.com/Study088/p/7110345.html
Copyright © 2011-2022 走看看