1.创建事件类型,继承自IEventSystemHandler
using UnityEngine.EventSystems; public interface IMyEvent : IEventSystemHandler { void MyMessage(); }
2.实现这个自定义的接口表示监听这个事件类型
using UnityEngine; public class EventTesta : MonoBehaviour,IMyEvent { public void MyMessage() { Debug.LogError("接收到我的消息"); } }
3.触发事件
using UnityEngine; using UnityEngine.EventSystems; public class OnCubeClick : MonoBehaviour,IPointerClickHandler { //监听了这个事件的目标 private GameObject target; public void OnPointerClick(PointerEventData eventData) { //触发事件,目标,参数,执行 ExecuteEvents.Execute<IMyEvent>(target, null, (x, y) => x.MyMessage()); } private void Awake() { target = GameObject.Find("Main Camera"); } }