消息事件系统由事件注册方注册事件,事件响应方响应事件,避免了脚本间频繁相互引用造成的耦合,下面是我实习期间练手用的例子,也是参考了大神们的代码,还要多看多学才是。
首先是事件管理器,内部提供事件的注册,移除方法和响应方法。
public class EventManager
{
#region 单例
private static EventManager instance = null;
public static EventManager Instance
{
get
{
if (instance == null)
{
instance = new EventManager();
}
return instance;
}
}
private EventManager() { }
#endregion
public delegate void EventDelegate<T>(T e) where T : GameEvent;
private Dictionary<Type, Delegate> delegates = new Dictionary<Type, Delegate>();
/// <summary>
/// 添加事件响应
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="del"></param>
public void AddListener<T>(EventDelegate<T> del) where T : GameEvent
{
if (delegates.ContainsKey(typeof(T)))
{
Delegate tempDel = delegates[typeof(T)];
delegates[typeof(T)] = System.Delegate.Combine(tempDel, del);
}
else
{
delegates[typeof(T)] = del;
}
}
/// <summary>
/// 移除事件
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="del"></param>
public void RemoveListener<T>(EventDelegate<T> del) where T : GameEvent
{
if (delegates.ContainsKey(typeof(T)))
{
var currentDel = System.Delegate.Remove(delegates[typeof(T)], del);
if (currentDel == null)
{
delegates.Remove(typeof(T));
}
else
{
delegates[typeof(T)] = currentDel;
}
}
}
/// <summary>
/// 激活事件
/// </summary>
/// <typeparam name="T">事件类型</typeparam>
/// <param name="e">事件</param>
public void Raise<T>(T e) where T : GameEvent
{
if (e == null)
{
Debug.Log("Invalid event argument: " + e.GetType().ToString());
return;
}
foreach (EventDelegate<T> del in delegates[e.GetType()].GetInvocationList())
{
del.Invoke(e);
}
}
}
另外定义自己的事件类如下,这里我以一个生产小兵的事件为例(需要参数的可在类内部定义)
public class GameEvent
{
}
public class CreatSoldierEvent : GameEvent
{
}
public class AddCoinEvent : GameEvent
{
public int number;
public AddCoinEvent(int n)
{
number = n;
}
}
public class GameOverEvent : GameEvent
{
}
使用时,以资源管理类为例,资源管理器注册对生产小兵的事件监听,一旦其他方法触发生产小兵则调用AddSoldier()方法去增加资源管理器内对小兵数量的记录。
public class Resource : MonoBehaviour
{
#region 单例
public static Resource instance;
#endregion
public int soldierNum;
public int coinNum;
public int population;
private void Awake()
{
instance = this;
soldierNum = 0;
coinNum = 0;
population = 0;
}
void OnEnable()
{
EventManager.Instance.AddListener<CreatSoldierEvent>(AddSoldier);//注册事件
}
private void AddSoldier(CreatSoldierEvent e)
{
soldierNum++;
}
public void AddCoin(int n)
{
coinNum += n;
}
public void UseCoin(int n)
{
coinNum -= n;
}
void OnDisable()
{
EventManager.Instance.RemoveListener<CreatSoldierEvent>(soldierDead);//移除事件
}
private void soldierDead(CreatSoldierEvent e)
{
soldierNum--;
}
}
在我的兵营脚本中触发生产小兵事件。
public void CreateSoldier()
{
if (Resource.instance.soldierNum < Resource.instance.population)
{
Debug.Log("CreatSoldier");
Resource.instance.UseCoin(5);
pos.z += 2;
if (flag % 2 == 0)
{
soldier.transform.position = pos;
GameObject tempSoldier = (GameObject)Instantiate(soldier);
}
else
{
acherSoldier.transform.position = pos;
GameObject cloneAcherSoldier = Instantiate(acherSoldier);
}
pos.z -= 2;
flag++;
EventManager.Instance.Raise(new CreatSoldierEvent());//触发事件
}
}
以上便是一个简单的消息事件系统的流程,第一次写这种记录,能给看到的人提供灵感是最好啦