• 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("测试成功!!");
    }

  • 相关阅读:
    oob中程序的监视
    使用内联 XAML
    silverlight 导航注意点
    动画入门,用actionscript实现A*寻路算法【游戏自动寻路】 转
    Remoting 转
    XSD文件 转
    Security 转
    游戏开发(程序)职位招聘的一些感受和经验 转
    WCF 转
    WPF 转
  • 原文地址:https://www.cnblogs.com/Study088/p/7110345.html
Copyright © 2020-2023  润新知