事件系统优化更新

事件系统优化更新
1.使用静态代替单例,静态方法调用时候内存地址无需二次偏移定位
2.UI事件在关闭UI时自动反监听
This commit is contained in:
ALEXTANG
2022-10-26 15:14:04 +08:00
parent 2e175fbcac
commit 74092e6399
37 changed files with 905 additions and 1187 deletions

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: a7f4494cbe10422e98e459dcf2af76a8
timeCreated: 1666765074

View File

@@ -0,0 +1,265 @@
using System;
using System.Collections.Generic;
namespace TEngine.Runtime
{
class DEventDelegateData
{
private int m_eventType = 0;
public List<Delegate> m_listExist = new List<Delegate>();
private List<Delegate> m_addList = new List<Delegate>();
private List<Delegate> m_deleteList = new List<Delegate>();
private bool m_isExcute = false;
private bool m_dirty = false;
public DEventDelegateData(int evnetType)
{
m_eventType = evnetType;
}
public bool AddHandler(Delegate handler)
{
if (m_listExist.Contains(handler))
{
Log.Fatal("Repeated Add Handler");
return false;
}
if (m_isExcute)
{
m_dirty = true;
m_addList.Add(handler);
}
else
{
m_listExist.Add(handler);
}
return true;
}
public void RmvHandler(Delegate hander)
{
if (m_isExcute)
{
m_dirty = true;
m_deleteList.Add(hander);
}
else
{
if (!m_listExist.Remove(hander))
{
Log.Fatal("Delete handle failed, not exist, EventId: {0}", StringId.HashToString(m_eventType));
}
}
}
private void CheckModify()
{
m_isExcute = false;
if (m_dirty)
{
for (int i = 0; i < m_addList.Count; i++)
{
m_listExist.Add(m_addList[i]);
}
m_addList.Clear();
for (int i = 0; i < m_deleteList.Count; i++)
{
m_listExist.Remove(m_deleteList[i]);
}
m_deleteList.Clear();
}
}
public void Callback()
{
m_isExcute = true;
for (var i = 0; i < m_listExist.Count; i++)
{
var d = m_listExist[i];
Action action = d as Action;
if (action != null)
{
action();
}
}
CheckModify();
}
public void Callback<T>(T arg1)
{
m_isExcute = true;
for (var i = 0; i < m_listExist.Count; i++)
{
var d = m_listExist[i];
var action = d as Action<T>;
if (action != null)
{
action(arg1);
}
}
CheckModify();
}
public void Callback<T, U>(T arg1, U arg2)
{
m_isExcute = true;
for (var i = 0; i < m_listExist.Count; i++)
{
var d = m_listExist[i];
var action = d as Action<T, U>;
if (action != null)
{
action(arg1, arg2);
}
}
CheckModify();
}
public void Callback<T, U, V>(T arg1, U arg2, V arg3)
{
m_isExcute = true;
for (var i = 0; i < m_listExist.Count; i++)
{
var d = m_listExist[i];
var action = d as Action<T, U, V>;
if (action != null)
{
action(arg1, arg2, arg3);
}
}
CheckModify();
}
public void Callback<T, U, V, W>(T arg1, U arg2, V arg3, W arg4)
{
m_isExcute = true;
for (var i = 0; i < m_listExist.Count; i++)
{
var d = m_listExist[i];
var action = d as Action<T, U, V, W>;
if (action != null)
{
action(arg1, arg2, arg3, arg4);
}
}
CheckModify();
}
public void Callback<T, U, V, W, X>(T arg1, U arg2, V arg3, W arg4, X arg5)
{
m_isExcute = true;
for (var i = 0; i < m_listExist.Count; i++)
{
var d = m_listExist[i];
var action = d as Action<T, U, V, W, X>;
if (action != null)
{
action(arg1, arg2, arg3, arg4, arg5);
}
}
CheckModify();
}
}
/// <summary>
/// 封装消息的底层分发和注册
/// </summary>
class DEventDispatcher
{
static Dictionary<int, DEventDelegateData> m_eventTable = new Dictionary<int, DEventDelegateData>();
#region
public bool AddEventListener(int eventType, Delegate handler)
{
DEventDelegateData data;
if (!m_eventTable.TryGetValue(eventType, out data))
{
data = new DEventDelegateData(eventType);
m_eventTable.Add(eventType, data);
}
return data.AddHandler(handler);
}
public void RemoveEventListener(int eventType, Delegate handler)
{
DEventDelegateData data;
if (m_eventTable.TryGetValue(eventType, out data))
{
data.RmvHandler(handler);
}
}
#endregion
#region
public void Send(int eventType)
{
DEventDelegateData d;
if (m_eventTable.TryGetValue(eventType, out d))
{
d.Callback();
}
}
public void Send<T>(int eventType, T arg1)
{
DEventDelegateData d;
if (m_eventTable.TryGetValue(eventType, out d))
{
d.Callback(arg1);
}
}
public void Send<T, U>(int eventType, T arg1, U arg2)
{
DEventDelegateData d;
if (m_eventTable.TryGetValue(eventType, out d))
{
d.Callback(arg1, arg2);
}
}
public void Send<T, U, V>(int eventType, T arg1, U arg2, V arg3)
{
DEventDelegateData d;
if (m_eventTable.TryGetValue(eventType, out d))
{
d.Callback(arg1, arg2, arg3);
}
}
public void Send<T, U, V, W>(int eventType, T arg1, U arg2, V arg3, W arg4)
{
DEventDelegateData d;
if (m_eventTable.TryGetValue(eventType, out d))
{
d.Callback(arg1, arg2, arg3, arg4);
}
}
public void Send<T, U, V, W, X>(int eventType, T arg1, U arg2, V arg3, W arg4, X arg5)
{
DEventDelegateData d;
if (m_eventTable.TryGetValue(eventType, out d))
{
d.Callback(arg1, arg2, arg3, arg4, arg5);
}
}
#endregion
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: ff8f95404d2a458682f4eca4cdbc93f0
timeCreated: 1666765082

View File

@@ -0,0 +1,27 @@
using System;
namespace TEngine.Runtime
{
public enum DEventGroup
{
/// <summary>
/// UI相关的交互
/// </summary>
GroupUI,
/// <summary>
/// 逻辑层内部相关的交互
/// </summary>
GroupLogic,
}
[System.AttributeUsage(System.AttributeTargets.Interface)]
public class DEventInterface : Attribute
{
public DEventGroup m_group;
public DEventInterface(DEventGroup group)
{
m_group = group;
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: a1bd30ea28cb457ea60b77c28fad0a2b
timeCreated: 1666765181

View File

@@ -0,0 +1,51 @@
using System.Collections.Generic;
namespace TEngine.Runtime
{
internal class DEventEntryData
{
public object m_interfaceWrap;
};
class DEventMgr
{
private DEventDispatcher m_dispatcher = new DEventDispatcher();
/// <summary>
/// 封装了调用的代理函数
/// </summary>
private Dictionary<string, DEventEntryData> m_entry = new Dictionary<string, DEventEntryData>();
public T GetInterface<T>()
{
string typeName = typeof(T).FullName;
DEventEntryData entry;
if (m_entry.TryGetValue(typeName, out entry))
{
return (T)entry.m_interfaceWrap;
}
return default(T);
}
/// <summary>
/// 注册wrap的函数
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="callerWrap"></param>
public void RegWrapInterface<T>(T callerWrap)
{
string typeName = typeof(T).FullName;
Log.Assert(!m_entry.ContainsKey(typeName));
var entry = new DEventEntryData();
entry.m_interfaceWrap = callerWrap;
m_entry.Add(typeName, entry);
}
public DEventDispatcher GetDispatcher()
{
return m_dispatcher;
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 5313d1a916d548028065c2de344836f3
timeCreated: 1666765217

View File

@@ -0,0 +1,128 @@
using System;
namespace TEngine.Runtime
{
public class GameEvent
{
private static DEventMgr m_mgr = new DEventMgr();
public static void Init()
{
// RegisterEventInterface_Logic.Register(m_mgr);
// RegisterEventInterface_UI.Register(m_mgr);
}
#region
public static bool AddEventListener(int eventType, Action handler)
{
return m_mgr.GetDispatcher().AddEventListener(eventType, handler);
}
public static bool AddEventListener<T>(int eventType, Action<T> handler)
{
return m_mgr.GetDispatcher().AddEventListener(eventType, handler);
}
public static bool AddEventListener<T, U>(int eventType, Action<T, U> handler)
{
return m_mgr.GetDispatcher().AddEventListener(eventType, handler);
}
public static bool AddEventListener<T, U, V>(int eventType, Action<T, U, V> handler)
{
return m_mgr.GetDispatcher().AddEventListener(eventType, handler);
}
public static bool AddEventListener<T, U, V, W>(int eventType, Action<T, U, V, W> handler)
{
return m_mgr.GetDispatcher().AddEventListener(eventType, handler);
}
public static bool AddEventListener<T, U, V, W, X>(int eventType, Action<T, U, V, W, X> handler)
{
return m_mgr.GetDispatcher().AddEventListener(eventType, handler);
}
public static void RemoveEventListener(int eventType, Action handler)
{
m_mgr.GetDispatcher().RemoveEventListener(eventType, handler);
}
public static void RemoveEventListener<T>(int eventType, Action<T> handler)
{
m_mgr.GetDispatcher().RemoveEventListener(eventType, handler);
}
public static void RemoveEventListener<T, U>(int eventType, Action<T, U> handler)
{
m_mgr.GetDispatcher().RemoveEventListener(eventType, handler);
}
public static void RemoveEventListener<T, U, V>(int eventType, Action<T, U, V> handler)
{
m_mgr.GetDispatcher().RemoveEventListener(eventType, handler);
}
public static void RemoveEventListener<T, U, V, W>(int eventType, Action<T, U, V, W> handler)
{
m_mgr.GetDispatcher().RemoveEventListener(eventType, handler);
}
public static void RemoveEventListener<T, U, V, W, X>(int eventType, Action<T, U, V, W, X> handler)
{
m_mgr.GetDispatcher().RemoveEventListener(eventType, handler);
}
public static void RemoveEventListener(int eventType, Delegate handler)
{
m_mgr.GetDispatcher().RemoveEventListener(eventType, handler);
}
#endregion
#region
public static T Get<T>()
{
return m_mgr.GetInterface<T>();
}
public static void Send(int eventType)
{
m_mgr.GetDispatcher().Send(eventType);
}
public static void Send<T>(int eventType, T arg1)
{
m_mgr.GetDispatcher().Send(eventType, arg1);
}
public static void Send<T, U>(int eventType, T arg1, U arg2)
{
m_mgr.GetDispatcher().Send(eventType, arg1, arg2);
}
public static void Send<T, U, V>(int eventType, T arg1, U arg2, V arg3)
{
m_mgr.GetDispatcher().Send(eventType, arg1, arg2, arg3);
}
public static void Send<T, U, V, W>(int eventType, T arg1, U arg2, V arg3, W arg4)
{
m_mgr.GetDispatcher().Send(eventType, arg1, arg2, arg3);
}
public static void Send<T, U, V, W, X>(int eventType, T arg1, U arg2, V arg3, W arg4, X arg5)
{
m_mgr.GetDispatcher().Send(eventType, arg1, arg2, arg3, arg4, arg5);
}
public static void Send(int eventType, Delegate handler)
{
m_mgr.GetDispatcher().Send(eventType, handler);
}
#endregion
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: ce11e95182ff4ac8928b21c833fe1d0b
timeCreated: 1666765360

View File

@@ -0,0 +1,88 @@
using System;
using System.Collections.Generic;
namespace TEngine.Runtime
{
public class GameEventMgr : IMemory
{
private List<int> m_listEventTypes;
private List<Delegate> m_listHandles;
private bool m_isInit = false;
public GameEventMgr()
{
if (m_isInit)
{
return;
}
m_isInit = true;
m_listEventTypes = new List<int>();
m_listHandles = new List<Delegate>();
}
public void Clear()
{
if (!m_isInit)
{
return;
}
for (int i = 0; i < m_listEventTypes.Count; ++i)
{
var eventType = m_listEventTypes[i];
var handle = m_listHandles[i];
GameEvent.RemoveEventListener(eventType, handle);
}
m_listEventTypes.Clear();
m_listHandles.Clear();
}
private void AddEvent(int eventType, Delegate handler)
{
m_listEventTypes.Add(eventType);
m_listHandles.Add(handler);
}
public void AddUIEvent(int eventType, Action handler)
{
if (GameEvent.AddEventListener(eventType, handler))
{
AddEvent(eventType, handler);
}
}
public void AddUIEvent<T>(int eventType, Action<T> handler)
{
if (GameEvent.AddEventListener(eventType, handler))
{
AddEvent(eventType, handler);
}
}
public void AddUIEvent<T, U>(int eventType, Action<T, U> handler)
{
if (GameEvent.AddEventListener(eventType, handler))
{
AddEvent(eventType, handler);
}
}
public void AddUIEvent<T, U, V>(int eventType, Action<T, U, V> handler)
{
if (GameEvent.AddEventListener(eventType, handler))
{
AddEvent(eventType, handler);
}
}
public void AddUIEvent<T, U, V, W>(int eventType, Action<T, U, V, W> handler)
{
if (GameEvent.AddEventListener(eventType, handler))
{
AddEvent(eventType, handler);
}
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: dbfc91838eab49f6a9c8e0cd222e5f16
timeCreated: 1666765305

View File

@@ -1,880 +0,0 @@
using System;
using System.Collections.Generic;
namespace TEngine.Runtime
{
#region EventInfo
internal interface IEventInfo
{
void Free();
}
public class EventInfo : IEventInfo, IMemory
{
private Action _actions;
public Action Actions => _actions;
public int EventCount { set; get; }
public void AddAction(Action action)
{
_actions += action;
EventCount++;
}
public void RmvAction(Action action)
{
_actions -= action;
EventCount--;
}
public void Clear()
{
EventCount = 0;
_actions = null;
}
public void Free()
{
MemoryPool.Release(this);
}
}
public class EventInfo<T> : IEventInfo, IMemory
{
private Action<T> _actions;
public Action<T> Actions => _actions;
public int EventCount { set; get; }
public void AddAction(Action<T> action)
{
_actions += action;
EventCount++;
}
public void RmvAction(Action<T> action)
{
_actions -= action;
EventCount--;
}
public void Clear()
{
EventCount = 0;
_actions = null;
}
public void Free()
{
MemoryPool.Release(this);
}
}
public class EventInfo<T, U> : IEventInfo, IMemory
{
private Action<T, U> _actions;
public Action<T, U> Actions => _actions;
public int EventCount { set; get; }
public void AddAction(Action<T, U> action)
{
_actions += action;
EventCount++;
}
public void RmvAction(Action<T, U> action)
{
_actions -= action;
EventCount--;
}
public void Clear()
{
EventCount = 0;
_actions = null;
}
public void Free()
{
MemoryPool.Release(this);
}
}
public class EventInfo<T, U, W> : IEventInfo, IMemory
{
private Action<T, U, W> _actions;
public Action<T, U, W> Actions => _actions;
public int EventCount { set; get; }
public void AddAction(Action<T, U, W> action)
{
_actions += action;
EventCount++;
}
public void RmvAction(Action<T, U, W> action)
{
_actions -= action;
EventCount--;
}
public void Clear()
{
EventCount = 0;
_actions = null;
}
public void Free()
{
MemoryPool.Release(this);
}
}
public class EventInfo<T, U, W, X> : IEventInfo, IMemory
{
private Action<T, U, W, X> _actions;
public Action<T, U, W, X> Actions => _actions;
public int EventCount { set; get; }
public void AddAction(Action<T, U, W, X> action)
{
_actions += action;
EventCount++;
}
public void RmvAction(Action<T, U, W, X> action)
{
_actions -= action;
EventCount--;
}
public void Clear()
{
EventCount = 0;
_actions = null;
}
public void Free()
{
MemoryPool.Release(this);
}
}
public class EventInfo<T, U, W, X, Y> : IEventInfo, IMemory
{
private Action<T, U, W, X, Y> _actions;
public Action<T, U, W, X, Y> Actions => _actions;
public int EventCount { set; get; }
public void AddAction(Action<T, U, W, X, Y> action)
{
_actions += action;
EventCount++;
}
public void RmvAction(Action<T, U, W, X, Y> action)
{
_actions -= action;
EventCount--;
}
public void Clear()
{
EventCount = 0;
_actions = null;
}
public void Free()
{
MemoryPool.Release(this);
}
}
#endregion
public class GameEvent : IMemory
{
public void Destroy()
{
Clear();
}
/// <summary>
/// Dictionary Key->Int.32 Value->EventInfo,调用频率高建议使用int事件减少字典内String的哈希碰撞
/// </summary>
private Dictionary<int, IEventInfo> _eventDic = new Dictionary<int, IEventInfo>();
/// <summary>
/// Dictionary Key->string Value->EventInfo,调用频率不高的时候可以使用
/// </summary>
private Dictionary<string, IEventInfo> m_eventStrDic = new Dictionary<string, IEventInfo>();
#region AddEventListener
public void AddEventListener<T>(int eventId, Action<T> action)
{
if (_eventDic.ContainsKey(eventId))
{
var eventInfo = _eventDic[eventId] as EventInfo<T>;
if (eventInfo != null)
{
eventInfo.AddAction(action);
}
else
{
throw new Exception("The Same GameEventId AddEventListener Need Same Args");
}
}
else
{
var eventInfo = MemoryPool.Acquire<EventInfo<T>>();
eventInfo.AddAction(action);
_eventDic.Add(eventId, eventInfo);
}
}
public void AddEventListener<T, U>(int eventId, Action<T, U> action)
{
if (_eventDic.ContainsKey(eventId))
{
var eventInfo = _eventDic[eventId] as EventInfo<T, U>;
if (eventInfo != null)
{
eventInfo.AddAction(action);
}
else
{
throw new Exception("The Same GameEventId AddEventListener Need Same Args");
}
}
else
{
var eventInfo = MemoryPool.Acquire<EventInfo<T, U>>();
eventInfo.AddAction(action);
_eventDic.Add(eventId, eventInfo);
}
}
public void AddEventListener<T, U, W>(int eventId, Action<T, U, W> action)
{
if (_eventDic.ContainsKey(eventId))
{
var eventInfo = _eventDic[eventId] as EventInfo<T, U, W>;
if (eventInfo != null)
{
eventInfo.AddAction(action);
}
else
{
throw new Exception("The Same GameEventId AddEventListener Need Same Args");
}
}
else
{
var eventInfo = MemoryPool.Acquire<EventInfo<T, U, W>>();
eventInfo.AddAction(action);
_eventDic.Add(eventId, eventInfo);
}
}
public void AddEventListener<T, U, W, X>(int eventId, Action<T, U, W, X> action)
{
if (_eventDic.ContainsKey(eventId))
{
var eventInfo = _eventDic[eventId] as EventInfo<T, U, W, X>;
if (eventInfo != null)
{
eventInfo.AddAction(action);
}
else
{
throw new Exception("The Same GameEventId AddEventListener Need Same Args");
}
}
else
{
var eventInfo = MemoryPool.Acquire<EventInfo<T, U, W, X>>();
eventInfo.AddAction(action);
_eventDic.Add(eventId, eventInfo);
}
}
public void AddEventListener<T, U, W, X, Y>(int eventId, Action<T, U, W, X, Y> action)
{
if (_eventDic.ContainsKey(eventId))
{
var eventInfo = _eventDic[eventId] as EventInfo<T, U, W, X, Y>;
if (eventInfo != null)
{
eventInfo.AddAction(action);
}
else
{
throw new Exception("The Same GameEventId AddEventListener Need Same Args");
}
}
else
{
var eventInfo = MemoryPool.Acquire<EventInfo<T, U, W, X, Y>>();
eventInfo.AddAction(action);
_eventDic.Add(eventId, eventInfo);
}
}
public void AddEventListener(int eventId, Action action)
{
if (_eventDic.ContainsKey(eventId))
{
var eventInfo = _eventDic[eventId] as EventInfo;
if (eventInfo != null)
{
eventInfo.AddAction(action);
}
else
{
throw new Exception("The Same GameEventId AddEventListener Need Same Args");
}
}
else
{
var eventInfo = MemoryPool.Acquire<EventInfo>();
eventInfo.AddAction(action);
_eventDic.Add(eventId, eventInfo);
}
}
#endregion
#region RemoveEventListener
public void RemoveEventListener<T>(int eventId, Action<T> action)
{
if (action == null)
{
return;
}
if (_eventDic.ContainsKey(eventId))
{
var eventInfo = _eventDic[eventId] as EventInfo<T>;
if (eventInfo != null)
{
eventInfo.RmvAction(action);
if (eventInfo.EventCount <= 0)
{
_eventDic.Remove(eventId);
MemoryPool.Release(eventInfo);
}
}
else
{
throw new Exception("The Same GameEventId RemoveEventListener Need Same Args");
}
}
}
public void RemoveEventListener<T, U>(int eventId, Action<T, U> action)
{
if (action == null)
{
return;
}
if (_eventDic.ContainsKey(eventId))
{
var eventInfo = _eventDic[eventId] as EventInfo<T, U>;
if (eventInfo != null)
{
eventInfo.RmvAction(action);
if (eventInfo.EventCount <= 0)
{
_eventDic.Remove(eventId);
MemoryPool.Release(eventInfo);
}
}
else
{
throw new Exception("The Same GameEventId RemoveEventListener Need Same Args");
}
}
}
public void RemoveEventListener<T, U, W>(int eventId, Action<T, U, W> action)
{
if (action == null)
{
return;
}
if (_eventDic.ContainsKey(eventId))
{
var eventInfo = _eventDic[eventId] as EventInfo<T, U, W>;
if (eventInfo != null)
{
eventInfo.RmvAction(action);
if (eventInfo.EventCount <= 0)
{
_eventDic.Remove(eventId);
MemoryPool.Release(eventInfo);
}
}
else
{
throw new Exception("The Same GameEventId RemoveEventListener Need Same Args");
}
}
}
public void RemoveEventListener<T, U, W, X>(int eventId, Action<T, U, W, X> action)
{
if (action == null)
{
return;
}
if (_eventDic.ContainsKey(eventId))
{
var eventInfo = _eventDic[eventId] as EventInfo<T, U, W, X>;
if (eventInfo != null)
{
eventInfo.RmvAction(action);
if (eventInfo.EventCount <= 0)
{
_eventDic.Remove(eventId);
MemoryPool.Release(eventInfo);
}
}
else
{
throw new Exception("The Same GameEventId RemoveEventListener Need Same Args");
}
}
}
public void RemoveEventListener<T, U, W, X, Y>(int eventId, Action<T, U, W, X, Y> action)
{
if (action == null)
{
return;
}
if (_eventDic.ContainsKey(eventId))
{
var eventInfo = _eventDic[eventId] as EventInfo<T, U, W, X, Y>;
if (eventInfo != null)
{
eventInfo.RmvAction(action);
if (eventInfo.EventCount <= 0)
{
_eventDic.Remove(eventId);
MemoryPool.Release(eventInfo);
}
}
else
{
throw new Exception("The Same GameEventId RemoveEventListener Need Same Args");
}
}
}
public void RemoveEventListener(int eventId, Action action)
{
if (action == null)
{
return;
}
if (_eventDic.ContainsKey(eventId))
{
var eventInfo = _eventDic[eventId] as EventInfo;
if (eventInfo != null)
{
eventInfo.RmvAction(action);
if (eventInfo.EventCount <= 0)
{
_eventDic.Remove(eventId);
MemoryPool.Release(eventInfo);
}
}
else
{
throw new Exception("The Same GameEventId RemoveEventListener Need Same Args");
}
}
}
#endregion
#region Send
public void Send<T>(int eventId, T info)
{
if (_eventDic.ContainsKey(eventId))
{
var eventInfo = _eventDic[eventId] as EventInfo<T>;
if (eventInfo != null)
{
eventInfo.Actions?.Invoke(info);
}
}
}
public void Send<T, U>(int eventId, T info, U info2)
{
if (_eventDic.ContainsKey(eventId))
{
var eventInfo = _eventDic[eventId] as EventInfo<T, U>;
if (eventInfo != null)
{
eventInfo.Actions?.Invoke(info, info2);
}
}
}
public void Send<T, U, W>(int eventId, T info, U info2, W info3)
{
if (_eventDic.ContainsKey(eventId))
{
var eventInfo = _eventDic[eventId] as EventInfo<T, U, W>;
if (eventInfo != null)
{
eventInfo.Actions?.Invoke(info, info2, info3);
}
}
}
public void Send<T, U, W, X>(int eventId, T info, U info2, W info3, X info4)
{
if (_eventDic.ContainsKey(eventId))
{
var eventInfo = _eventDic[eventId] as EventInfo<T, U, W, X>;
if (eventInfo != null)
{
eventInfo.Actions?.Invoke(info, info2, info3, info4);
}
}
}
public void Send<T, U, W, X, Y>(int eventId, T info, U info2, W info3, X info4, Y Info5)
{
if (_eventDic.ContainsKey(eventId))
{
var eventInfo = _eventDic[eventId] as EventInfo<T, U, W, X, Y>;
if (eventInfo != null)
{
eventInfo.Actions?.Invoke(info, info2, info3, info4, Info5);
}
}
}
public void Send(int eventId)
{
if (_eventDic.ContainsKey(eventId))
{
var eventInfo = _eventDic[eventId] as EventInfo;
if (eventInfo != null)
{
eventInfo.Actions?.Invoke();
}
}
}
#endregion
#region StringEvent
#region AddEventListener
public void AddEventListener<T>(string eventId, Action<T> action)
{
if (m_eventStrDic.ContainsKey(eventId))
{
var eventInfo = m_eventStrDic[eventId] as EventInfo<T>;
if (eventInfo != null)
{
eventInfo.AddAction(action);
}
else
{
throw new Exception("The Same GameEventId AddEventListener Need Same Args");
}
}
else
{
var eventInfo = MemoryPool.Acquire<EventInfo<T>>();
eventInfo.AddAction(action);
m_eventStrDic.Add(eventId, eventInfo);
}
}
public void AddEventListener<T, U>(string eventId, Action<T, U> action)
{
if (m_eventStrDic.ContainsKey(eventId))
{
var eventInfo = m_eventStrDic[eventId] as EventInfo<T, U>;
if (eventInfo != null)
{
eventInfo.AddAction(action);
}
else
{
throw new Exception("The Same GameEventId AddEventListener Need Same Args");
}
}
else
{
var eventInfo = MemoryPool.Acquire<EventInfo<T, U>>();
eventInfo.AddAction(action);
m_eventStrDic.Add(eventId, eventInfo);
}
}
public void AddEventListener<T, U, W>(string eventId, Action<T, U, W> action)
{
if (m_eventStrDic.ContainsKey(eventId))
{
var eventInfo = m_eventStrDic[eventId] as EventInfo<T, U, W>;
if (eventInfo != null)
{
eventInfo.AddAction(action);
}
else
{
throw new Exception("The Same GameEventId AddEventListener Need Same Args");
}
}
else
{
var eventInfo = MemoryPool.Acquire<EventInfo<T, U, W>>();
eventInfo.AddAction(action);
m_eventStrDic.Add(eventId, eventInfo);
}
}
public void AddEventListener(string eventId, Action action)
{
if (m_eventStrDic.ContainsKey(eventId))
{
var eventInfo = m_eventStrDic[eventId] as EventInfo;
if (eventInfo != null)
{
eventInfo.AddAction(action);
}
else
{
throw new Exception("The Same GameEventId AddEventListener Need Same Args");
}
}
else
{
var eventInfo = MemoryPool.Acquire<EventInfo>();
eventInfo.AddAction(action);
m_eventStrDic.Add(eventId, eventInfo);
}
}
#endregion
#region RemoveEventListener
public void RemoveEventListener<T>(string eventId, Action<T> action)
{
if (action == null)
{
return;
}
if (m_eventStrDic.ContainsKey(eventId))
{
var eventInfo = m_eventStrDic[eventId] as EventInfo<T>;
if (eventInfo != null)
{
eventInfo.RmvAction(action);
if (eventInfo.EventCount <= 0)
{
m_eventStrDic.Remove(eventId);
MemoryPool.Release(eventInfo);
}
}
else
{
throw new Exception("The Same GameEventId RemoveEventListener Need Same Args");
}
}
}
public void RemoveEventListener<T, U>(string eventId, Action<T, U> action)
{
if (action == null)
{
return;
}
if (m_eventStrDic.ContainsKey(eventId))
{
var eventInfo = m_eventStrDic[eventId] as EventInfo<T, U>;
if (eventInfo != null)
{
eventInfo.RmvAction(action);
if (eventInfo.EventCount <= 0)
{
m_eventStrDic.Remove(eventId);
MemoryPool.Release(eventInfo);
}
}
else
{
throw new Exception("The Same GameEventId RemoveEventListener Need Same Args");
}
}
}
public void RemoveEventListener<T, U, W>(string eventId, Action<T, U, W> action)
{
if (action == null)
{
return;
}
if (m_eventStrDic.ContainsKey(eventId))
{
var eventInfo = m_eventStrDic[eventId] as EventInfo<T, U, W>;
if (eventInfo != null)
{
eventInfo.RmvAction(action);
if (eventInfo.EventCount <= 0)
{
m_eventStrDic.Remove(eventId);
MemoryPool.Release(eventInfo);
}
}
else
{
throw new Exception("The Same GameEventId RemoveEventListener Need Same Args");
}
}
}
public void RemoveEventListener(string eventId, Action action)
{
if (action == null)
{
return;
}
if (m_eventStrDic.ContainsKey(eventId))
{
var eventInfo = m_eventStrDic[eventId] as EventInfo;
if (eventInfo != null)
{
eventInfo.RmvAction(action);
if (eventInfo.EventCount <= 0)
{
m_eventStrDic.Remove(eventId);
MemoryPool.Release(eventInfo);
}
}
else
{
throw new Exception("The Same GameEventId RemoveEventListener Need Same Args");
}
}
}
#endregion
#region Send
public void Send<T>(string eventId, T info)
{
if (m_eventStrDic.ContainsKey(eventId))
{
var eventInfo = (m_eventStrDic[eventId] as EventInfo<T>);
if (eventInfo != null)
{
eventInfo.Actions?.Invoke(info);
}
}
}
public void Send<T, U>(string eventId, T info, U info2)
{
if (m_eventStrDic.ContainsKey(eventId))
{
var eventInfo = (m_eventStrDic[eventId] as EventInfo<T, U>);
if (eventInfo != null)
{
eventInfo.Actions?.Invoke(info, info2);
}
}
}
public void Send<T, U, W>(string eventId, T info, U info2, W info3)
{
if (m_eventStrDic.ContainsKey(eventId))
{
var eventInfo = (m_eventStrDic[eventId] as EventInfo<T, U, W>);
if (eventInfo != null)
{
eventInfo.Actions?.Invoke(info, info2, info3);
}
}
}
public void Send(string eventId)
{
if (m_eventStrDic.ContainsKey(eventId))
{
var eventInfo = (m_eventStrDic[eventId] as EventInfo);
if (eventInfo != null)
{
eventInfo.Actions?.Invoke();
}
}
}
#endregion
#endregion
#region Clear
public void Clear()
{
var etr = _eventDic.GetEnumerator();
while (etr.MoveNext())
{
var eventInfo = etr.Current.Value;
eventInfo.Free();
}
etr.Dispose();
var etrStr = m_eventStrDic.GetEnumerator();
while (etrStr.MoveNext())
{
var eventInfo = etrStr.Current.Value;
eventInfo.Free();
}
etrStr.Dispose();
_eventDic.Clear();
m_eventStrDic.Clear();
}
#endregion
}
}

View File

@@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: 8bb87835faef0ad4f9bf016b5df511f0
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -1,215 +0,0 @@
using System;
namespace TEngine.Runtime
{
/// <summary>
/// 总观察者 - 总事件中心系统
/// </summary>
public class GameEventMgr : TSingleton<GameEventMgr>
{
private GameEvent _gameEvent;
protected override void Init()
{
base.Init();
_gameEvent = MemoryPool.Acquire<GameEvent>();
}
public override void Release()
{
MemoryPool.Release(_gameEvent);
}
public override void Active()
{
base.Active();
}
#region AddEventListener
public void AddEventListener<T>(int eventId, Action<T> action)
{
_gameEvent.AddEventListener(eventId, action);
}
public void AddEventListener<T, U>(int eventId, Action<T, U> action)
{
_gameEvent.AddEventListener(eventId, action);
}
public void AddEventListener<T, U, W>(int eventId, Action<T, U, W> action)
{
_gameEvent.AddEventListener(eventId, action);
}
public void AddEventListener<T, U, W, X>(int eventId, Action<T, U, W, X> action)
{
_gameEvent.AddEventListener(eventId, action);
}
public void AddEventListener<T, U, W, X, Y>(int eventId, Action<T, U, W, X, Y> action)
{
_gameEvent.AddEventListener(eventId, action);
}
public void AddEventListener(int eventId, Action action)
{
_gameEvent.AddEventListener(eventId, action);
}
#endregion
#region RemoveEventListener
public void RemoveEventListener<T>(int eventId, Action<T> action)
{
_gameEvent.RemoveEventListener(eventId, action);
}
public void RemoveEventListener<T, U>(int eventId, Action<T, U> action)
{
_gameEvent.RemoveEventListener(eventId, action);
}
public void RemoveEventListener<T, U, W>(int eventId, Action<T, U, W> action)
{
_gameEvent.RemoveEventListener(eventId, action);
}
public void RemoveEventListener<T, U, W, X>(int eventId, Action<T, U, W, X> action)
{
_gameEvent.RemoveEventListener(eventId, action);
}
public void RemoveEventListener<T, U, W, X, Y>(int eventId, Action<T, U, W, X, Y> action)
{
_gameEvent.RemoveEventListener(eventId, action);
}
public void RemoveEventListener(int eventId, Action action)
{
_gameEvent.RemoveEventListener(eventId, action);
}
#endregion
#region Send
public void Send<T>(int eventId, T info)
{
_gameEvent.Send(eventId, info);
}
public void Send<T, U>(int eventId, T info, U info2)
{
_gameEvent.Send(eventId, info, info2);
}
public void Send<T, U, W>(int eventId, T info, U info2, W info3)
{
_gameEvent.Send(eventId, info, info2, info3);
}
public void Send<T, U, W, X>(int eventId, T info, U info2, W info3, X info4)
{
_gameEvent.Send(eventId, info, info2, info3, info4);
}
public void Send<T, U, W, X, Y>(int eventId, T info, U info2, W info3, X info4, Y info5)
{
_gameEvent.Send(eventId, info, info2, info3, info4, info5);
}
public void Send(int eventId)
{
_gameEvent.Send(eventId);
}
#endregion
#region StringEvent
#region AddEventListener
public void AddEventListener<T>(string eventId, Action<T> action)
{
_gameEvent.AddEventListener(eventId, action);
}
public void AddEventListener<T, U>(string eventId, Action<T, U> action)
{
_gameEvent.AddEventListener(eventId, action);
}
public void AddEventListener<T, U, W>(string eventId, Action<T, U, W> action)
{
_gameEvent.AddEventListener(eventId, action);
}
public void AddEventListener(string eventId, Action action)
{
_gameEvent.AddEventListener(eventId, action);
}
#endregion
#region RemoveEventListener
public void RemoveEventListener<T>(string eventId, Action<T> action)
{
_gameEvent.RemoveEventListener(eventId, action);
}
public void RemoveEventListener<T, U>(string eventId, Action<T, U> action)
{
_gameEvent.RemoveEventListener(eventId, action);
}
public void RemoveEventListener<T, U, W>(string eventId, Action<T, U, W> action)
{
_gameEvent.RemoveEventListener(eventId, action);
}
public void RemoveEventListener(string eventId, Action action)
{
_gameEvent.RemoveEventListener(eventId, action);
}
#endregion
#region Send
public void Send<T>(string eventId, T info)
{
_gameEvent.Send(eventId, info);
}
public void Send<T, U>(string eventId, T info, U info2)
{
_gameEvent.Send(eventId, info, info2);
}
public void Send<T, U, W>(string eventId, T info, U info2, W info3)
{
_gameEvent.Send(eventId, info, info2, info3);
}
public void Send(string eventId)
{
_gameEvent.Send(eventId);
}
#endregion
#endregion
#region Clear
public void Clear()
{
_gameEvent.Clear();
}
#endregion
}
}

View File

@@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: c42c5b68ecfcd7849bc964b670a1217e
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -138,27 +138,27 @@ namespace TEngine.Runtime
private void OnNetworkConnected(INetworkChannel channel, object obj)
{
GameEventMgr.Instance.Send(NetWorkEventId.NetworkConnectedEvent,channel, obj);
GameEvent.Send(NetWorkEventId.NetworkConnectedEvent,channel, obj);
}
private void OnNetworkClosed(INetworkChannel channel)
{
GameEventMgr.Instance.Send(NetWorkEventId.NetworkClosedEvent,channel);
GameEvent.Send(NetWorkEventId.NetworkClosedEvent,channel);
}
private void OnNetworkMissHeartBeat(INetworkChannel channel, int missCount)
{
GameEventMgr.Instance.Send(NetWorkEventId.NetworkMissHeartBeatEvent,channel,missCount);
GameEvent.Send(NetWorkEventId.NetworkMissHeartBeatEvent,channel,missCount);
}
private void OnNetworkError(INetworkChannel channel, NetworkErrorCode errorCode, string message)
{
GameEventMgr.Instance.Send(NetWorkEventId.NetworkErrorEvent,channel,errorCode,message);
GameEvent.Send(NetWorkEventId.NetworkErrorEvent,channel,errorCode,message);
}
private void OnNetworkCustomError(INetworkChannel channel,object message)
{
GameEventMgr.Instance.Send(NetWorkEventId.NetworkCustomErrorEvent,channel,message);
GameEvent.Send(NetWorkEventId.NetworkCustomErrorEvent,channel,message);
}
/// <summary>

View File

@@ -24,11 +24,11 @@ namespace TEngine.Runtime
{
m_NetworkChannel = networkChannel;
GameEventMgr.Instance.AddEventListener<INetworkChannel,object>(NetWorkEventId.NetworkConnectedEvent,OnNetworkConnected);
GameEventMgr.Instance.AddEventListener<INetworkChannel>(NetWorkEventId.NetworkClosedEvent,OnNetworkClosed);
GameEventMgr.Instance.AddEventListener<INetworkChannel,int>(NetWorkEventId.NetworkMissHeartBeatEvent,OnNetworkMissHeartBeat);
GameEventMgr.Instance.AddEventListener<INetworkChannel,NetworkErrorCode,string>(NetWorkEventId.NetworkErrorEvent,OnNetworkError);
GameEventMgr.Instance.AddEventListener<INetworkChannel,object>(NetWorkEventId.NetworkCustomErrorEvent,OnNetworkCustomError);
GameEvent.AddEventListener<INetworkChannel,object>(NetWorkEventId.NetworkConnectedEvent,OnNetworkConnected);
GameEvent.AddEventListener<INetworkChannel>(NetWorkEventId.NetworkClosedEvent,OnNetworkClosed);
GameEvent.AddEventListener<INetworkChannel,int>(NetWorkEventId.NetworkMissHeartBeatEvent,OnNetworkMissHeartBeat);
GameEvent.AddEventListener<INetworkChannel,NetworkErrorCode,string>(NetWorkEventId.NetworkErrorEvent,OnNetworkError);
GameEvent.AddEventListener<INetworkChannel,object>(NetWorkEventId.NetworkCustomErrorEvent,OnNetworkCustomError);
m_NetworkChannel.RegisterHandler((int)ActionCode.HeartBeat,HandleHeartBeat);
}
@@ -98,11 +98,11 @@ namespace TEngine.Runtime
/// </summary>
public void Shutdown()
{
GameEventMgr.Instance.RemoveEventListener<INetworkChannel,object>(NetWorkEventId.NetworkConnectedEvent,OnNetworkConnected);
GameEventMgr.Instance.RemoveEventListener<INetworkChannel>(NetWorkEventId.NetworkClosedEvent,OnNetworkClosed);
GameEventMgr.Instance.RemoveEventListener<INetworkChannel,int>(NetWorkEventId.NetworkMissHeartBeatEvent,OnNetworkMissHeartBeat);
GameEventMgr.Instance.RemoveEventListener<INetworkChannel,NetworkErrorCode,string>(NetWorkEventId.NetworkErrorEvent,OnNetworkError);
GameEventMgr.Instance.RemoveEventListener<INetworkChannel,object>(NetWorkEventId.NetworkCustomErrorEvent,OnNetworkCustomError);
GameEvent.RemoveEventListener<INetworkChannel,object>(NetWorkEventId.NetworkConnectedEvent,OnNetworkConnected);
GameEvent.RemoveEventListener<INetworkChannel>(NetWorkEventId.NetworkClosedEvent,OnNetworkClosed);
GameEvent.RemoveEventListener<INetworkChannel,int>(NetWorkEventId.NetworkMissHeartBeatEvent,OnNetworkMissHeartBeat);
GameEvent.RemoveEventListener<INetworkChannel,NetworkErrorCode,string>(NetWorkEventId.NetworkErrorEvent,OnNetworkError);
GameEvent.RemoveEventListener<INetworkChannel,object>(NetWorkEventId.NetworkCustomErrorEvent,OnNetworkCustomError);
m_NetworkChannel = null;
}

View File

@@ -8,17 +8,19 @@ namespace TEngine.Runtime
{
public override GameObject Load(string path)
{
return Resources.Load<GameObject>(path);
return Resources.Load<GameObject>(RegularPath(path));
}
public override GameObject Load(string path, Transform parent)
{
var obj = Load(path);
var obj = Load(RegularPath(path));
if (obj == null)
{
return null;
}
obj = UnityEngine.Object.Instantiate(obj);
if (parent != null)
{
obj.transform.SetParent(parent);
@@ -27,19 +29,42 @@ namespace TEngine.Runtime
return obj;
}
public static string RegularPath(string path)
{
if (string.IsNullOrEmpty(path))
{
return string.Empty;
}
var splits = path.Split('.');
if (splits.Length > 1)
{
string ret = string.Empty;
for (int i = 0; i < splits.Length-1; i++)
{
ret += splits[i];
}
return ret;
}
else
{
return path;
}
}
public override T Load<T>(string path)
{
return Resources.Load<T>(path);
return Resources.Load<T>(RegularPath(path));
}
public override void LoadAsync(string path, Action<GameObject> callBack)
{
MonoUtility.StartCoroutine(ReallyLoadAsync(path, callBack));
MonoUtility.StartCoroutine(ReallyLoadAsync(RegularPath(path), callBack));
}
private IEnumerator ReallyLoadAsync<T>(string path, Action<T> callback = null) where T : UnityEngine.Object
{
ResourceRequest request = Resources.LoadAsync<T>(path);
ResourceRequest request = Resources.LoadAsync<T>(RegularPath(path));
yield return request;
@@ -55,7 +80,7 @@ namespace TEngine.Runtime
public override void LoadAsync<T>(string path, Action<T> callBack, bool withSubAsset = false)
{
MonoUtility.StartCoroutine(ReallyLoadAsync<T>(path, callBack));
MonoUtility.StartCoroutine(ReallyLoadAsync<T>(RegularPath(path), callBack));
}
}
}

View File

@@ -1091,7 +1091,7 @@ namespace TEngine.Runtime.Entity
/// <param name="userData">用户自定义数据。</param>
private void OnShowEntitySuccess(IEntity entity, float duration, object userData)
{
GameEventMgr.Instance.Send(EntityEvent.ShowEntitySuccess,entity,duration,userData);
GameEvent.Send(EntityEvent.ShowEntitySuccess,entity,duration,userData);
}
/// <summary>
@@ -1109,7 +1109,7 @@ namespace TEngine.Runtime.Entity
entityAssetName,
entityGroupName,
errorMessage);
GameEventMgr.Instance.Send(EntityEvent.ShowEntityFailure,entityId,entityAssetName,entityGroupName,errorMessage,userData);
GameEvent.Send(EntityEvent.ShowEntityFailure,entityId,entityAssetName,entityGroupName,errorMessage,userData);
}
/// <summary>
@@ -1122,7 +1122,7 @@ namespace TEngine.Runtime.Entity
/// <param name="userData">用户自定义数据。</param>
private void OnShowEntityUpdate(int entityId, string entityAssetName, string entityGroupName, float progress, object userData)
{
GameEventMgr.Instance.Send(EntityEvent.ShowEntityUpdate,entityId,entityAssetName,entityGroupName,progress,userData);
GameEvent.Send(EntityEvent.ShowEntityUpdate,entityId,entityAssetName,entityGroupName,progress,userData);
}
/// <summary>
@@ -1137,7 +1137,7 @@ namespace TEngine.Runtime.Entity
/// <param name="userData">用户自定义数据。</param>
private void OnShowEntityDependencyAsset(int entityId, string entityAssetName, string entityGroupName, string dependencyAssetName, int loadedCount, int totalCount, object userData)
{
GameEventMgr.Instance.Send(EntityEvent.ShowEntityDependency,entityId,entityAssetName,entityGroupName,userData);
GameEvent.Send(EntityEvent.ShowEntityDependency,entityId,entityAssetName,entityGroupName,userData);
}
/// <summary>
@@ -1149,7 +1149,7 @@ namespace TEngine.Runtime.Entity
/// <param name="userData">用户自定义数据。</param>
private void OnHideEntityComplete(int entityId, string entityAssetName, IEntityGroup entityGroup, object userData)
{
GameEventMgr.Instance.Send(EntityEvent.HideEntityComplete,entityId,entityAssetName,entityGroup,userData);
GameEvent.Send(EntityEvent.HideEntityComplete,entityId,entityAssetName,entityGroup,userData);
}
public override void OnUpdate(float elapseSeconds, float realElapseSeconds)

View File

@@ -9,6 +9,7 @@ namespace TEngine.Runtime.HotUpdate
#pragma warning disable CS0162
public class LoadMgr : TSingleton<LoadMgr>
{
public static int DownLoadFinish = StringId.StringToHash("DownLoadResult.AllDownLoaded");
/// <summary>
/// 资源版本号
/// </summary>
@@ -31,7 +32,7 @@ namespace TEngine.Runtime.HotUpdate
if (result == (int)DownLoadResult.AllDownLoaded)
{
_UnPackCallback(100, status, 100);
GameEventMgr.Instance.Send("DownLoadResult.AllDownLoaded", true);
GameEvent.Send(DownLoadFinish, true);
_StopLoadingCheck();
}
else

View File

@@ -19,7 +19,7 @@ namespace TEngine.Runtime.UIModule
{
if (m_eventMgr == null)
{
m_eventMgr = GameEventMgr.Instance;
m_eventMgr = MemoryPool.Acquire<GameEventMgr>();
}
return m_eventMgr;
@@ -61,42 +61,29 @@ namespace TEngine.Runtime.UIModule
#region Event
private Dictionary<int, Delegate> m_eventTable = new Dictionary<int, Delegate>();
protected void ClearAllRegisterEvent()
{
var element = m_eventTable.GetEnumerator();
while (element.MoveNext())
{
var m_event = element.Current.Value;
//GameEventMgr.Instance.RemoveEventListener(element.Current.Key, m_event);
}
m_eventTable.Clear();
MemoryPool.Release(m_eventMgr);
}
protected void AddUIEvent(int eventType, Action handler)
{
m_eventTable.Add(eventType, handler);
EventMgr.AddEventListener(eventType, handler);
EventMgr.AddUIEvent(eventType, handler);
}
protected void AddUIEvent<T>(int eventType, Action<T> handler)
{
m_eventTable.Add(eventType, handler);
EventMgr.AddEventListener(eventType, handler);
EventMgr.AddUIEvent(eventType, handler);
}
protected void AddUIEvent<T, U>(int eventType, Action<T, U> handler)
{
m_eventTable.Add(eventType, handler);
EventMgr.AddEventListener(eventType, handler);
EventMgr.AddUIEvent(eventType, handler);
}
protected void AddUIEvent<T, U, V>(int eventType, Action<T, U, V> handler)
{
m_eventTable.Add(eventType, handler);
EventMgr.AddEventListener(eventType, handler);
EventMgr.AddUIEvent(eventType, handler);
}
#endregion

View File

@@ -143,6 +143,8 @@ namespace TEngine.Runtime.UIModule
m_destroyed = true;
ClearAllRegisterEvent();
DestroyAllChild();
OnDestroy();