按钮事件添加方法3个例子
Eg1: using UnityEngine; using System.Collections; using UnityEngine.Events;//引用事件命名空间 using UnityEngine.UI;//引用UI命名空间 public class Test : MonoBehaviour { // Use this for initialization void Start () { //定义Action,并赋予delegate方法 UnityAction<Button> btnActions = new UnityAction<Button>(onClick); //找到Button控件,并订阅事件 Button btn = gameObject.GetComponent<Button>(); btn.onClick.AddListener(btnActions); } void onClick(Object obj) { Debug.Log("button==========="); Debug.Log("button-----------" + obj.name); } } Eg2: Button btn = kid.GetComponent<Button>(); btn.onClick.AddListener(delegate() { this.Number(kid.name); } ); void Number(string name){} Eg3: void Update(){ if (Input.GetMouseButtonDown(0) ) { Debug.Log(EventSystem.current.gameObject.name); if (EventSystem.current.IsPointerOverGameObject()) Debug.Log("当前触摸在UI上"); else Debug.Log("当前没有触摸在UI上"); } }
时间: 2024-10-22 13:35:11