using UnityEngine; using UnityEngine.EventSystems; //以下是按下事件,想要别的事件EventTriggerType枚举点出来自己就能看到了 public class EventTriggerDelegateExample : MonoBehaviour { void Start() { EventTrigger trigger = GetComponent<EventTrigger>(); EventTrigger.Entry entry = new EventTrigger.Entry(); entry.eventID = EventTriggerType.PointerDown;
/*这里的(PinterEventData)data参数不固定,即,可以使其他别的类型的参数,
比如说gameobject啊之类的(不要用数组传,传不过去的,会提示越界)*/ entry.callback.AddListener((data) => { OnPointerDownDelegate((PointerEventData)data); }); trigger.triggers.Add(entry); } public void OnPointerDownDelegate(PointerEventData data) { Debug.Log("OnPointerDownDelegate called."); } }
题外话:虽然这个方法好,但是如果按钮结合scrollview组件的话你会神奇的发现你拖不动按钮了。。。所以这时候我们只能用回onClick.AddListener()了,参数传递的话就用上面那样的方法,比如:
private void Start() { int a = 1; int b = 2; btn.OnClick.AddListener(()=>{ Method(a, b); }); } private void Method(int a, int b) { Debug.Log(a + b); }