| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319 |
- using System;
- using System.Collections.Generic;
- using UnityEngine;
- public static class EventMgr
- {
- public delegate void CoreEventDelegate<T, T1>(CoreEvent<T, T1> e);
- public delegate void CoreEventDelegate<T>(CoreEvent<T> e);
- public delegate void LuaCoreEventDelegate<T>(LuaCoreEvent<T> e);
- public delegate void LuaCoreEventDelegate(LuaCoreEvent e);
- static readonly Dictionary<int, Delegate> m_Delegates = new Dictionary<int, Delegate>();
- static readonly Dictionary<string, Delegate> m_LuaDelegates = new Dictionary<string, Delegate>();
- public static void AddEventListener<T>(int type, CoreEventDelegate<T> listener)
- {
- Delegate d;
- if (m_Delegates.TryGetValue(type, out d))
- {
- m_Delegates[type] = Delegate.Combine(d, listener);
- }
- else
- {
- m_Delegates[type] = listener;
- }
- }
- public static void AddEventListener<T,T1>(int type, CoreEventDelegate<T,T1> listener)
- {
- Delegate d;
- if (m_Delegates.TryGetValue(type, out d))
- {
- m_Delegates[type] = Delegate.Combine(d, listener);
- }
- else
- {
- m_Delegates[type] = listener;
- }
- }
- public static void RemoveEventListener<T>(int type, CoreEventDelegate<T> listener)
- {
- Delegate d;
- if (m_Delegates.TryGetValue(type, out d))
- {
- Delegate currentDel = Delegate.Remove(d, listener);
- if (currentDel == null)
- {
- m_Delegates.Remove(type);
- }
- else
- {
- m_Delegates[type] = currentDel;
- }
- }
- }
- public static void RemoveEventListener<T,T1>(int type, CoreEventDelegate<T, T1> listener)
- {
- Delegate d;
- if (m_Delegates.TryGetValue(type, out d))
- {
- Delegate currentDel = Delegate.Remove(d, listener);
- if (currentDel == null)
- {
- m_Delegates.Remove(type);
- }
- else
- {
- m_Delegates[type] = currentDel;
- }
- }
- }
- public static void AddLuaEventListenerArgs<T>(string luaFunction, LuaCoreEventDelegate<T> listener)
- {
- Delegate d;
- if (m_LuaDelegates.TryGetValue(luaFunction, out d))
- {
- m_LuaDelegates[luaFunction] = Delegate.Combine(d, listener);
- }
- else
- {
- m_LuaDelegates[luaFunction] = listener;
- }
- }
- public static void RemoveLuaEventListenerArgs<T>(string luaFunction, LuaCoreEventDelegate<T> listener)
- {
- Delegate d;
- if (m_LuaDelegates.TryGetValue(luaFunction, out d))
- {
- Delegate currentDel = Delegate.Remove(d, listener);
- if (currentDel == null)
- {
- m_LuaDelegates.Remove(luaFunction);
- }
- else
- {
- m_LuaDelegates[luaFunction] = currentDel;
- }
- }
- }
- public static void AddLuaEventListener(string luaFunction, LuaCoreEventDelegate listener)
- {
- Delegate d;
- if (m_LuaDelegates.TryGetValue(luaFunction, out d))
- {
- m_LuaDelegates[luaFunction] = Delegate.Combine(d, listener);
- }
- else
- {
- m_LuaDelegates[luaFunction] = listener;
- }
- }
- public static void RemoveLuaEventListener(string luaFunction, LuaCoreEventDelegate listener)
- {
- Delegate d;
- if (m_LuaDelegates.TryGetValue(luaFunction, out d))
- {
- Delegate currentDel = Delegate.Remove(d, listener);
- if (currentDel == null)
- {
- m_LuaDelegates.Remove(luaFunction);
- }
- else
- {
- m_LuaDelegates[luaFunction] = currentDel;
- }
- }
- }
- public static void RemoveLuaEventListener<T>(int type)
- {
- if (m_Delegates.ContainsKey(type))
- {
- m_Delegates.Remove(type);
- }
- }
- public static void DispatchEvent<T>(CoreEvent<T> e)
- {
- if (e == null)
- {
- throw new ArgumentNullException("e");
- }
- Delegate d;
- if (m_Delegates.TryGetValue(e.EventType, out d))
- {
- CoreEventDelegate<T> callback = d as CoreEventDelegate<T>;
- if (callback != null)
- {
- callback(e);
- }
- }
- }
- public static void DispatchEvent<T,T1>(CoreEvent<T,T1> e)
- {
- if (e == null)
- {
- throw new ArgumentNullException("e");
- }
- Delegate d;
- if (m_Delegates.TryGetValue(e.EventType, out d))
- {
- CoreEventDelegate<T, T1> callback = d as CoreEventDelegate<T, T1>;
- if (callback != null)
- {
- callback(e);
- }
- }
- }
- public static void LuaDispatchEvent<T>(LuaCoreEvent<T> e)
- {
- if (e == null)
- {
- throw new ArgumentNullException("e");
- }
- Delegate d;
- if (m_LuaDelegates.TryGetValue(e.Func, out d))
- {
- LuaCoreEventDelegate<T> callback = d as LuaCoreEventDelegate<T>;
- if (callback != null)
- {
- callback(e);
- }
- }
- }
- public static void LuaDispatchEvent(LuaCoreEvent e)
- {
- if (e == null)
- {
- throw new ArgumentNullException("e");
- }
- Delegate d;
- if (m_LuaDelegates.TryGetValue(e.Func, out d))
- {
- LuaCoreEventDelegate callback = d as LuaCoreEventDelegate;
- if (callback != null)
- {
- callback(e);
- }
- }
- }
- }
- public class CoreEvent<T,T1>
- {
- int m_EventType;
- public int EventType
- {
- get { return m_EventType; }
- }
- T m_Data;
- public T Data
- {
- get { return m_Data; }
- }
- T1 m_Data1;
- public T1 Data1
- {
- get { return m_Data1; }
- }
-
- public CoreEvent(int type, T data,T1 data1)
- {
- m_EventType = type;
- m_Data = data;
- m_Data1 = data1;
- }
- }
- public class CoreEvent<T>
- {
- int m_EventType;
- public int EventType
- {
- get { return m_EventType; }
- }
- T m_Data;
- public T Data
- {
- get { return m_Data; }
- }
- object mParam = null;
- public object Param
- {
- get { return mParam; }
- set { mParam = value; }
- }
- object mParam2 = null;
- public object Param2
- {
- get { return mParam2; }
- set { mParam2 = value; }
- }
- public CoreEvent(int type, T data)
- {
- m_EventType = type;
- m_Data = data;
- }
- public void SetData(T data)
- {
- m_Data = data;
- }
- }
- public class LuaCoreEvent<T>
- {
- string m_Func;
- public string Func
- {
- get { return m_Func; }
- }
- T m_Data;
- public T Data
- {
- get { return m_Data; }
- }
- public LuaCoreEvent(string luaFunc, T data)
- {
- m_Func = luaFunc;
- m_Data = data;
- }
- }
- public class LuaCoreEvent
- {
- string m_Func;
- public string Func
- {
- get { return m_Func; }
- }
- public LuaCoreEvent(string luaFunc)
- {
- m_Func = luaFunc;
- }
- }
|