事件监听

1.CallBack

1 public delegate void CallBack();
2 public delegate void CallBack<T>(T arg);
3 public delegate void CallBack<T, X>(T arg1, X arg2);
4 public delegate void CallBack<T, X, Y>(T arg1, X arg2, Y arg3);
5 public delegate void CallBack<T, X, Y, Z>(T arg1, X arg2, Y arg3, Z arg4);
6 public delegate void CallBack<T, X, Y, Z, W>(T arg1, X arg2, Y arg3, Z arg4, W arg5);

2.EventCenter

复制代码
  1 using System;
  2 using System.Collections;
  3 using System.Collections.Generic;
  4 using UnityEngine;
  5 
  6 public class EventCenter
  7 {
  8     private static Dictionary<EventDefine, Delegate> m_EventTable = new Dictionary<EventDefine, Delegate>();
  9 
 10     private static void OnListenerAdding(EventDefine eventType, Delegate callBack)
 11     {
 12         if (!m_EventTable.ContainsKey(eventType))
 13         {
 14             m_EventTable.Add(eventType, null);
 15         }
 16         Delegate d = m_EventTable[eventType];
 17         if (d != null && d.GetType() != callBack.GetType())
 18         {
 19             throw new Exception(string.Format("尝试为事件{0}添加不同类型的委托,当前事件所对应的委托是{1},要添加的委托类型为{2}", eventType, d.GetType(), callBack.GetType()));
 20         }
 21     }
 22     private static void OnListenerRemoving(EventDefine eventType, Delegate callBack)
 23     {
 24         if (m_EventTable.ContainsKey(eventType))
 25         {
 26             Delegate d = m_EventTable[eventType];
 27             if (d == null)
 28             {
 29                 throw new Exception(string.Format("移除监听错误:事件{0}没有对应的委托", eventType));
 30             }
 31             else if (d.GetType() != callBack.GetType())
 32             {
 33                 throw new Exception(string.Format("移除监听错误:尝试为事件{0}移除不同类型的委托,当前委托类型为{1},要移除的委托类型为{2}", eventType, d.GetType(), callBack.GetType()));
 34             }
 35         }
 36         else
 37         {
 38             throw new Exception(string.Format("移除监听错误:没有事件码{0}", eventType));
 39         }
 40     }
 41     private static void OnListenerRemoved(EventDefine eventType)
 42     {
 43         if (m_EventTable[eventType] == null)
 44         {
 45             m_EventTable.Remove(eventType);
 46         }
 47     }
 48     //no parameters
 49     public static void AddListener(EventDefine eventType, CallBack callBack)
 50     {
 51         OnListenerAdding(eventType, callBack);
 52         m_EventTable[eventType] = (CallBack)m_EventTable[eventType] + callBack;
 53     }
 54     //Single parameters
 55     public static void AddListener<T>(EventDefine eventType, CallBack<T> callBack)
 56     {
 57         OnListenerAdding(eventType, callBack);
 58         m_EventTable[eventType] = (CallBack<T>)m_EventTable[eventType] + callBack;
 59     }
 60     //two parameters
 61     public static void AddListener<T, X>(EventDefine eventType, CallBack<T, X> callBack)
 62     {
 63         OnListenerAdding(eventType, callBack);
 64         m_EventTable[eventType] = (CallBack<T, X>)m_EventTable[eventType] + callBack;
 65     }
 66     //three parameters
 67     public static void AddListener<T, X, Y>(EventDefine eventType, CallBack<T, X, Y> callBack)
 68     {
 69         OnListenerAdding(eventType, callBack);
 70         m_EventTable[eventType] = (CallBack<T, X, Y>)m_EventTable[eventType] + callBack;
 71     }
 72     //four parameters
 73     public static void AddListener<T, X, Y, Z>(EventDefine eventType, CallBack<T, X, Y, Z> callBack)
 74     {
 75         OnListenerAdding(eventType, callBack);
 76         m_EventTable[eventType] = (CallBack<T, X, Y, Z>)m_EventTable[eventType] + callBack;
 77     }
 78     //five parameters
 79     public static void AddListener<T, X, Y, Z, W>(EventDefine eventType, CallBack<T, X, Y, Z, W> callBack)
 80     {
 81         OnListenerAdding(eventType, callBack);
 82         m_EventTable[eventType] = (CallBack<T, X, Y, Z, W>)m_EventTable[eventType] + callBack;
 83     }
 84 
 85     //no parameters
 86     public static void RemoveListener(EventDefine eventType, CallBack callBack)
 87     {
 88         OnListenerRemoving(eventType, callBack);
 89         m_EventTable[eventType] = (CallBack)m_EventTable[eventType] - callBack;
 90         OnListenerRemoved(eventType);
 91     }
 92     //single parameters
 93     public static void RemoveListener<T>(EventDefine eventType, CallBack<T> callBack)
 94     {
 95         OnListenerRemoving(eventType, callBack);
 96         m_EventTable[eventType] = (CallBack<T>)m_EventTable[eventType] - callBack;
 97         OnListenerRemoved(eventType);
 98     }
 99     //two parameters
100     public static void RemoveListener<T, X>(EventDefine eventType, CallBack<T, X> callBack)
101     {
102         OnListenerRemoving(eventType, callBack);
103         m_EventTable[eventType] = (CallBack<T, X>)m_EventTable[eventType] - callBack;
104         OnListenerRemoved(eventType);
105     }
106     //three parameters
107     public static void RemoveListener<T, X, Y>(EventDefine eventType, CallBack<T, X, Y> callBack)
108     {
109         OnListenerRemoving(eventType, callBack);
110         m_EventTable[eventType] = (CallBack<T, X, Y>)m_EventTable[eventType] - callBack;
111         OnListenerRemoved(eventType);
112     }
113     //four parameters
114     public static void RemoveListener<T, X, Y, Z>(EventDefine eventType, CallBack<T, X, Y, Z> callBack)
115     {
116         OnListenerRemoving(eventType, callBack);
117         m_EventTable[eventType] = (CallBack<T, X, Y, Z>)m_EventTable[eventType] - callBack;
118         OnListenerRemoved(eventType);
119     }
120     //five parameters
121     public static void RemoveListener<T, X, Y, Z, W>(EventDefine eventType, CallBack<T, X, Y, Z, W> callBack)
122     {
123         OnListenerRemoving(eventType, callBack);
124         m_EventTable[eventType] = (CallBack<T, X, Y, Z, W>)m_EventTable[eventType] - callBack;
125         OnListenerRemoved(eventType);
126     }
127 
128 
129     //no parameters
130     public static void Broadcast(EventDefine eventType)
131     {
132         Delegate d;
133         if (m_EventTable.TryGetValue(eventType, out d))
134         {
135             CallBack callBack = d as CallBack;
136             if (callBack != null)
137             {
138                 callBack();
139             }
140             else
141             {
142                 throw new Exception(string.Format("广播事件错误:事件{0}对应委托具有不同的类型", eventType));
143             }
144         }
145     }
146     //single parameters
147     public static void Broadcast<T>(EventDefine eventType, T arg)
148     {
149         Delegate d;
150         if (m_EventTable.TryGetValue(eventType, out d))
151         {
152             CallBack<T> callBack = d as CallBack<T>;
153             if (callBack != null)
154             {
155                 callBack(arg);
156             }
157             else
158             {
159                 throw new Exception(string.Format("广播事件错误:事件{0}对应委托具有不同的类型", eventType));
160             }
161         }
162     }
163     //two parameters
164     public static void Broadcast<T, X>(EventDefine eventType, T arg1, X arg2)
165     {
166         Delegate d;
167         if (m_EventTable.TryGetValue(eventType, out d))
168         {
169             CallBack<T, X> callBack = d as CallBack<T, X>;
170             if (callBack != null)
171             {
172                 callBack(arg1, arg2);
173             }
174             else
175             {
176                 throw new Exception(string.Format("广播事件错误:事件{0}对应委托具有不同的类型", eventType));
177             }
178         }
179     }
180     //three parameters
181     public static void Broadcast<T, X, Y>(EventDefine eventType, T arg1, X arg2, Y arg3)
182     {
183         Delegate d;
184         if (m_EventTable.TryGetValue(eventType, out d))
185         {
186             CallBack<T, X, Y> callBack = d as CallBack<T, X, Y>;
187             if (callBack != null)
188             {
189                 callBack(arg1, arg2, arg3);
190             }
191             else
192             {
193                 throw new Exception(string.Format("广播事件错误:事件{0}对应委托具有不同的类型", eventType));
194             }
195         }
196     }
197     //four parameters
198     public static void Broadcast<T, X, Y, Z>(EventDefine eventType, T arg1, X arg2, Y arg3, Z arg4)
199     {
200         Delegate d;
201         if (m_EventTable.TryGetValue(eventType, out d))
202         {
203             CallBack<T, X, Y, Z> callBack = d as CallBack<T, X, Y, Z>;
204             if (callBack != null)
205             {
206                 callBack(arg1, arg2, arg3, arg4);
207             }
208             else
209             {
210                 throw new Exception(string.Format("广播事件错误:事件{0}对应委托具有不同的类型", eventType));
211             }
212         }
213     }
214     //five parameters
215     public static void Broadcast<T, X, Y, Z, W>(EventDefine eventType, T arg1, X arg2, Y arg3, Z arg4, W arg5)
216     {
217         Delegate d;
218         if (m_EventTable.TryGetValue(eventType, out d))
219         {
220             CallBack<T, X, Y, Z, W> callBack = d as CallBack<T, X, Y, Z, W>;
221             if (callBack != null)
222             {
223                 callBack(arg1, arg2, arg3, arg4, arg5);
224             }
225             else
226             {
227                 throw new Exception(string.Format("广播事件错误:事件{0}对应委托具有不同的类型", eventType));
228             }
229         }
230     }
231 }
复制代码

3.public enum EventDfine//枚举

{

}

 

posted @   一路繁华的夏ˇ  阅读(78)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
点击右上角即可分享
微信分享提示