事件避免装箱拆箱,有参无参重载的优化
This commit is contained in:
parent
204ebeea77
commit
21c0b35c85
@ -4,33 +4,79 @@ using UnityEngine;
|
||||
using UnityEngine.Events;
|
||||
using UnityEngine.Windows;
|
||||
|
||||
public interface IEventInfo//利用一个空的接口来承载泛型类,里氏转换原则
|
||||
{
|
||||
|
||||
}
|
||||
public class EventInfo<T> : IEventInfo
|
||||
{
|
||||
public UnityAction<T> action;
|
||||
public EventInfo(UnityAction<T> action)
|
||||
{
|
||||
action += action;
|
||||
}
|
||||
}
|
||||
|
||||
public class EventInfo : IEventInfo
|
||||
{
|
||||
|
||||
public UnityAction action;
|
||||
public EventInfo(UnityAction action)
|
||||
{
|
||||
this.action += action;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 事件中心
|
||||
/// 1.字典 2.委托 3.观察者设计模式
|
||||
/// 4.泛型
|
||||
/// </summary>
|
||||
public class EventCenter : BaseManager<EventCenter>
|
||||
{
|
||||
//key ---事件名字(比如:怪物死亡,玩家死亡,通关 等等)
|
||||
//value --- 对应 监听这个事件 对应的委托函数
|
||||
private Dictionary<string , UnityAction<object>> eventDic = new Dictionary<string , UnityAction<object>>();
|
||||
private Dictionary<string , IEventInfo> eventDic = new Dictionary<string , IEventInfo>();
|
||||
|
||||
/// <summary>
|
||||
/// 添加事件监听
|
||||
/// </summary>
|
||||
/// <param name="name">事件的名字</param>
|
||||
/// <param name="action">准备用来处理时间的委托函数</param>
|
||||
public void AddEventListener(string name,UnityAction<object> action)
|
||||
public void AddEventListener<T>(string name,UnityAction<T> action)
|
||||
{
|
||||
//有没有对应的事件监听
|
||||
//有的情况
|
||||
if(eventDic.ContainsKey(name))
|
||||
{
|
||||
eventDic[name] += action;//已经有事件就往后排
|
||||
(eventDic[name] as EventInfo<T>).action += action;//已经有事件就往后排
|
||||
}
|
||||
//没有的情况
|
||||
else
|
||||
{
|
||||
eventDic.Add(name, action);//没有就加一个
|
||||
eventDic.Add(name, new EventInfo<T>(action));//没有就加一个
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 重载加入事件,不需要参数,没有泛型
|
||||
/// </summary>
|
||||
/// <param name="name"></param>
|
||||
/// <param name="action"></param>
|
||||
public void AddEventListener(string name, UnityAction action)
|
||||
{
|
||||
//有没有对应的事件监听
|
||||
//有的情况
|
||||
if (eventDic.ContainsKey(name))
|
||||
{
|
||||
(eventDic[name] as EventInfo).action += action;//已经有事件就往后排
|
||||
}
|
||||
//没有的情况
|
||||
else
|
||||
{
|
||||
eventDic.Add(name, new EventInfo(action));//没有就加一个
|
||||
}
|
||||
}
|
||||
|
||||
@ -40,11 +86,24 @@ public class EventCenter : BaseManager<EventCenter>
|
||||
/// </summary>
|
||||
/// <param name="name"></param>
|
||||
/// <param name="action"></param>
|
||||
public void RemoveEventListener(string name, UnityAction<object> action)
|
||||
public void RemoveEventListener<T>(string name, UnityAction<T> action)
|
||||
{
|
||||
if(eventDic.ContainsKey(name))
|
||||
{
|
||||
eventDic[name] -= action;
|
||||
(eventDic[name] as EventInfo<T>).action -= action;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 重载事件移除,没有泛型
|
||||
/// </summary>
|
||||
/// <param name="name"></param>
|
||||
/// <param name="action"></param>
|
||||
public void RemoveEventListener(string name, UnityAction action)
|
||||
{
|
||||
if (eventDic.ContainsKey(name))
|
||||
{
|
||||
(eventDic[name] as EventInfo).action -= action;
|
||||
}
|
||||
}
|
||||
|
||||
@ -52,11 +111,34 @@ public class EventCenter : BaseManager<EventCenter>
|
||||
/// 事件触发
|
||||
/// </summary>
|
||||
/// <param name="name">哪一个名字的事件触发了</param>
|
||||
public void EvennTrigger(string name,object info)
|
||||
public void EventTrigger<T>(string name,T info)
|
||||
{
|
||||
if(eventDic.ContainsKey(name))
|
||||
{
|
||||
eventDic[name].Invoke(info);//有就触发事件
|
||||
if((eventDic[name] as EventInfo<T>).action != null)
|
||||
{
|
||||
(eventDic[name] as EventInfo<T>).action.Invoke(info);
|
||||
}
|
||||
|
||||
// eventDic[name].Invoke(info);//有就触发事件
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 重载事件触发,没有泛型
|
||||
/// </summary>
|
||||
/// <param name="name"></param>
|
||||
/// <param name="info"></param>
|
||||
public void EventTrigger(string name)
|
||||
{
|
||||
if (eventDic.ContainsKey(name))
|
||||
{
|
||||
if ((eventDic[name] as EventInfo).action != null)
|
||||
{
|
||||
(eventDic[name] as EventInfo).action.Invoke();
|
||||
}
|
||||
|
||||
// eventDic[name].Invoke(info);//有就触发事件
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -22,6 +22,6 @@ public class Monster : MonoBehaviour
|
||||
{
|
||||
Debug.Log("怪物死亡");
|
||||
//触发事件
|
||||
EventCenter.GetInstance().EvennTrigger("MonsterDead",this);
|
||||
EventCenter.GetInstance().EventTrigger<Monster>("MonsterDead",this);
|
||||
}
|
||||
}
|
||||
|
@ -6,11 +6,22 @@ public class Other : MonoBehaviour
|
||||
{
|
||||
void Start()
|
||||
{
|
||||
EventCenter.GetInstance().AddEventListener("MonsterDead", OtherWaitMonsterDeadDo);
|
||||
EventCenter.GetInstance().AddEventListener<Monster>("MonsterDead", OtherWaitMonsterDeadDo);
|
||||
|
||||
EventCenter.GetInstance().AddEventListener("Win", Win);
|
||||
|
||||
// EventCenter.GetInstance().EventTrigger<Monster>("MonsterDead", 怪物参数);
|
||||
EventCenter.GetInstance().EventTrigger("Win");
|
||||
|
||||
}
|
||||
|
||||
public void OtherWaitMonsterDeadDo(object info)
|
||||
public void OtherWaitMonsterDeadDo(Monster info)
|
||||
{
|
||||
Debug.Log("其他各个对象要做的事");
|
||||
}
|
||||
public void Win()
|
||||
{
|
||||
Debug.Log("无参事件中心");
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -4,7 +4,7 @@ using UnityEngine;
|
||||
|
||||
public class Player : MonoBehaviour
|
||||
{
|
||||
|
||||
/*
|
||||
void Start()
|
||||
{
|
||||
EventCenter.GetInstance().AddEventListener("MonsterDead",MonsterDeadDo);
|
||||
@ -13,5 +13,16 @@ public class Player : MonoBehaviour
|
||||
public void MonsterDeadDo(object info)
|
||||
{
|
||||
Debug.Log("鯤소삿돤쉽쟨" +(info as Monster).name1);
|
||||
}*/
|
||||
|
||||
|
||||
void Start()
|
||||
{
|
||||
EventCenter.GetInstance().AddEventListener<Monster>("MonsterDead", MonsterDeadDo);
|
||||
}
|
||||
|
||||
public void MonsterDeadDo(Monster info)
|
||||
{
|
||||
Debug.Log("鯤소삿돤쉽쟨" + info.name1);
|
||||
}
|
||||
}
|
||||
|
@ -5,12 +5,22 @@ using UnityEngine;
|
||||
public class Task : MonoBehaviour
|
||||
{
|
||||
// Start is called before the first frame update
|
||||
void Start()
|
||||
/* void Start()
|
||||
{
|
||||
EventCenter.GetInstance().AddEventListener("MonsterDead", TaskWaitMonsterDeadDo);
|
||||
}
|
||||
|
||||
public void TaskWaitMonsterDeadDo(object info)
|
||||
{
|
||||
Debug.Log("ÈÎÎñ¼Ç¼");
|
||||
}*/
|
||||
|
||||
void Start()
|
||||
{
|
||||
EventCenter.GetInstance().AddEventListener<Monster>("MonsterDead", TaskWaitMonsterDeadDo);
|
||||
}
|
||||
|
||||
public void TaskWaitMonsterDeadDo(Monster info)
|
||||
{
|
||||
Debug.Log("ÈÎÎñ¼Ç¼");
|
||||
}
|
||||
|
@ -48,7 +48,7 @@ public class ScenesMgr : BaseManager<ScenesMgr>
|
||||
while(!ao.isDone)
|
||||
{
|
||||
//事件中心 向外分发 进度情况 外面想用就用
|
||||
EventCenter.GetInstance().EvennTrigger("进度条更新",ao.progress);
|
||||
EventCenter.GetInstance().EventTrigger("进度条更新",ao.progress);
|
||||
//这里面去更新进度条
|
||||
yield return ao.progress;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user