实用的信号系统

在项目中信号系统(或者事件系统)用的非常多,而且很好用,我喜欢用于全局信号,用于不同系统之间的交互,加上MVC,ECS模块的内部信号。

以下列出实现的内容:

 

SignalCenter:全局的信号中心用于交互不同系统之间的消息

EntitySignal(下面没有,实际上就是换个名字,并且不是静态类):实体内部信号,我常用于MVC结构,或者ECS结构的内部信号,比如数据跟UI的交互,UI监听数据,系统监听UI点击事件之类的

SignalParam:信号的参数,用于扩展传递复杂数据

具体实现

  1 using UnityEngine;
  2 using UnityEngine.Events;
  3 using System.Collections;
  4 using System.Collections.Generic;
  5 
  6 /// <summary>
  7 /// event
  8 /// </summary>
  9 [System.Serializable]
 10 public class Signal : UnityEvent<SignalParam> { }
 11 
 12 
 13 /// <summary>
 14 /// 信号行为参数
 15 /// </summary>
 16 public class SignalParam {
 17     public int intVal;
 18     public string stringVal;
 19     public Dictionary<int, int> dicVal;
 20     public List<int> listVal;
 21     public GameObject gameObjectVal;
 22     // 自己扩展其他的参数
 23 }
 24 
 25 /// <summary>
 26 /// 消息中心
 27 /// </summary>
 28 public class SignalCenter
 29 {
 30     // 单例模式
 31     public static SignalCenter instance;
 32 
 33     // 信号表
 34     private Dictionary<string, Signal> eventDictionary = new Dictionary<string, Signal>();
 35 
 36     private static SignalParam onceParam { get; set; } // 简单信号参数,内部扩展接口,注意引用
 37     /// <summary>
 38     /// 开始监听,注册信号
 39     /// </summary>
 40     /// <param name="signalName">Event name.</param>
 41     /// <param name="signalAction">signalAction.</param>
 42     public static void Connect(string signalName, UnityAction<SignalParam> signalAction)
 43     {
 44         if (instance == null)
 45         {
 46             instance = new SignalCenter();
 47             if (instance == null)
 48             {
 49                 Debug.Log("Have no event manager on scene");
 50                 return;
 51             }
 52         }
 53         Signal signal = null;
 54         if (instance.eventDictionary.TryGetValue(signalName, out signal))
 55         {
 56             signal.AddListener(signalAction);
 57         }
 58         else
 59         {
 60             signal = new Signal();
 61             signal.AddListener(signalAction);
 62             instance.eventDictionary.Add(signalName, signal);
 63         }
 64     }
 65 
 66     /// <summary>
 67     /// 断开监听,取消信号
 68     /// </summary>
 69     /// <param name="signalName">Event name.</param>
 70     /// <param name="signalAction">signalAction.</param>
 71     public static void Disconnect(string signalName, UnityAction<SignalParam> signalAction)
 72     {
 73         if (instance == null)
 74         {
 75             return;
 76         }
 77         Signal signal = null;
 78         if (instance.eventDictionary.TryGetValue(signalName, out signal))
 79         {
 80             signal.RemoveListener(signalAction);
 81         }
 82     }
 83 
 84     /// <summary>
 85     /// 发送信号,触发
 86     /// </summary>
 87     /// <param name="signalName">Event name.</param>
 88     /// <param name="obj">Object.</param>
 89     /// <param name="param">Parameter.</param>
 90     public static void Emit(string signalName, SignalParam param)
 91     {
 92         if (instance == null)
 93         {
 94             return;
 95         }
 96         Signal signal = null;
 97         if (instance.eventDictionary.TryGetValue(signalName, out signal))
 98         {
 99             signal.Invoke(param);
100         }
101     }
102 
103     /// <summary>
104     /// 简单发送信号,整形参数
105     /// </summary>
106     /// <param name="signalName">Event name.</param>
107     /// <param name="obj">Object.</param>
108     /// <param name="param">Parameter.</param>
109     public static void Emit(string signalName, int intval)
110     {
111         if (instance == null)
112         {
113             return;
114         }
115         Signal signal = null;
116         onceParam.intVal = intval;
117         if (instance.eventDictionary.TryGetValue(signalName, out signal))
118         {
119             signal.Invoke(onceParam);
120         }
121     }
122 }
posted @ 2019-12-28 00:12  leilei199441  阅读(159)  评论(0编辑  收藏  举报