2017/3/3 EventSystems-----IEventSystemHandler和ExecuteEvents.Execute使用案例

举例:

  需求:实现"对象或系统等"生成事件后,"事件管理器"接受收集事件[并处理记录等等]后发送给"关心该事件的对象"进行响应[该事件的处理]的流程。

  实现:

  事件管理器:

public class EventManager : MonoBehaviour
{
    Queue<IEvent> events = new Queue<IEvent>();

    public void RaiseEvent() { }//加入事件队列

    void Update()
    {
        //Execute                        //执行队列中事件

        //Clear
    }
}

  自定义事件接口:

  

1 public interface IEvent
2 {
3     void Execute ();
4 }

  事件处理接口:

1 public interface IMyEventHandler : IEventSystemHandler//所有自定义事件继承 IEventSystemHandler
2 {
3     void HandleEvent();
4 }

  自定义事件:

 1 public class MyEvent : IEvent
 2 {
 3     public GameObject target;
 4 
 5     public MyEvent(GameObject _target)
 6     {
 7         target = _target;
 8     }
 9 
10     public void Execute()
11     {
12         if (target == null)
13             return;
14 
15         ExecuteEvents.Execute<IMyEventHandler>(target, null, (handler, data) =>
16         {
17             handler.HandleEvent();
18         });
19 
20     }
21 }

  自定义事件处理:

1 public class IMyHandler :  IMyEventHandler
2 {
3     void HandleEvent() { }
4 }

  总结:通过该系统可实现收集事件,并选择不同关心对象分发,以及事件记录排序等等的功能。

原文地址:https://www.cnblogs.com/munetiey/p/6496336.html