美文网首页
快速获取按钮类型的点击事件或者直接拖入按钮

快速获取按钮类型的点击事件或者直接拖入按钮

作者: 咆哮的小老虎 | 来源:发表于2019-08-15 10:59 被阅读0次

    快速获取按钮类型的点击事件

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    
    public class aaaa : MonoBehaviour {
        private  GameObject tests;
    
        private Button[] btns;
    
        // Use this for initialization
        void Start ()
        {
            tests = transform.gameObject;
    
            btns = tests.GetComponentsInChildren<Button>();
    
            for (int i = 0; i < btns.Length; i++)
            {
                GameObject sender = btns[i].gameObject;
                btns[i].onClick.AddListener(() =>
                {
                    OnClickBtn(sender);
                });
            }
        }
    
        void OnClickBtn(GameObject obj)
        {
    
            switch (obj.name)
            {
                case "1":
                    Run(obj.name);
                    break;
                case "2":
                    Run(obj.name);
                    break;
                default:
                    break;
            }
        }
    
        private void Run(string name)
        {
            print(name + "跑");
        }
    }
    

    直接拖入按钮

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    
    public class OnClickBtn : MonoBehaviour {
    
    
    
        // Use this for initialization
        void Start ()
        {
            GetComponent<Button>().onClick.AddListener(() =>
            {
                AAA();
            });
        }
    
        void AAA()
        {
    
        }
    
        // Update is called once per frame
        void Update () {
            
        }
    }
    

    相关文章

      网友评论

          本文标题:快速获取按钮类型的点击事件或者直接拖入按钮

          本文链接:https://www.haomeiwen.com/subject/mtiajctx.html