U3D游戏开发框架(八)——消息系统
一:目的为什么要使用消息系统呢,假如我们的游戏中现在有一个敌人和一个玩家,当敌人击中玩家后玩家需要执行自身的扣血操作,那么就需要在敌人类中引用玩家脚本,这样就造成了代码的耦合,消息系统在开发中是一个非常重要的系统,他的作用就是解耦合Unity提供的消息机制SendMessage可以非常方便我们的脚本开发,但它实现的是一种伪监听者模式,利用的是反射机制,比较低效,几乎不会有人在项目中使用Unity内
·
一:目的
为什么要使用消息系统呢?消息系统的作用就是解耦合
消息系统就例如我们约好几个小伙伴出去玩,但是现在临时有事取消计划,如果没有群聊那么你需要挨个通知每一个人,但是有了群聊,只要在群里发一条消息就能通知到大家,前提是大家都进了这个群,才能收到这个消息。每个人进群就相当于绑定监听消息的方法,退群就相当于移除监听消息的方法,你在群里发一条消息就相当于发送消息的方法
Unity提供了一种消息机制:SendMessage,但它实现的是一种伪监听者模式,利用的是反射机制,性能较低,几乎不会有人在项目中使用SendMessage,一般都是去实现一套自己的消息系统
二:解决的问题及优点
——解耦合,使模块与模块之间解耦,与消息中心耦合
三:使用方法
——MsgConst中添加事件常量
——在类初始化和销毁时分别注册和注销事件
——发送事件使用MsgSystem.Dispatch
using UnityEngine;
public class : MonoBehaviour
{
private void Awake()
{
MsgSystem.AddListener(MsgConst.Test, Test_NoParam);
MsgSystem.AddListener<int>(MsgConst.Test, Test_OneParam);
MsgSystem.AddListener<int, long>(MsgConst.Test, Test_TwoParam);
}
void Test_NoParam()
{
Debug.Log("no param");
}
void Test_OneParam(int i)
{
Debug.Log("one param");
}
void Test_TwoParam(int i, long j)
{
Debug.Log("two param");
}
private void OnDestroy()
{
MsgSystem.RemoveListener(MsgConst.Test, Test_NoParam);
MsgSystem.RemoveListener<int>(MsgConst.Test, Test_OneParam);
MsgSystem.RemoveListener<int, long>(MsgConst.Test, Test_TwoParam);
}
}
四:代码实现
using System.Collections.Generic;
using System;
using System.Text;
using UnityEngine;
/// <summary>
/// 事件系统
/// </summary>
public static class MsgSystem
{
//存所有事件的字典
static Dictionary<string, List<Delegate>> m_EventDict = new Dictionary<string, List<Delegate>>();
/// <summary>
/// 添加监听
/// </summary>
static void AddListener(string key, Delegate callBack)
{
List<Delegate> eventList;
if (m_EventDict.TryGetValue(key, out eventList))
{
eventList.Add(callBack);
}
else
{
eventList = new List<Delegate>();
eventList.Add(callBack);
m_EventDict.Add(key, eventList);
}
}
/// <summary>
/// 移除监听
/// </summary>
static void RemoveListener(string key, Delegate callBack)
{
List<Delegate> eventList;
if (m_EventDict.TryGetValue(key, out eventList))
{
eventList.Remove(callBack);
}
if (eventList.Count == 0)
{
m_EventDict.Remove(key);
}
}
/// <summary>
/// 移除所有监听
/// </summary>
public static void RemoveAllListener()
{
m_EventDict.Clear();
}
#region 添加监听
public static void AddListener(string key, Action callBack)
{
AddListener(key, (Delegate)callBack);
}
public static void AddListener<T1>(string key, Action<T1> callBack)
{
AddListener(key, (Delegate)callBack);
}
public static void AddListener<T1, T2>(string key, Action<T1, T2> callBack)
{
AddListener(key, (Delegate)callBack);
}
public static void AddListener<T1, T2, T3>(string key, Action<T1, T2, T3> callBack)
{
AddListener(key, (Delegate)callBack);
}
public static void AddListener<T1, T2, T3, T4>(string key, Action<T1, T2, T3, T4> callBack)
{
AddListener(key, (Delegate)callBack);
}
public static void AddListener<T1, T2, T3, T4, T5>(string key, Action<T1, T2, T3, T4, T5> callBack)
{
AddListener(key, (Delegate)callBack);
}
#endregion
#region 移除监听
public static void RemoveListener(string key, Action callBack)
{
RemoveListener(key, (Delegate)callBack);
}
public static void RemoveListener<T1>(string key, Action<T1> callBack)
{
RemoveListener(key, (Delegate)callBack);
}
public static void RemoveListener<T1, T2>(string key, Action<T1, T2> callBack)
{
RemoveListener(key, (Delegate)callBack);
}
public static void RemoveListener<T1, T2, T3>(string key, Action<T1, T2, T3> callBack)
{
RemoveListener(key, (Delegate)callBack);
}
public static void RemoveListener<T1, T2, T3, T4>(string key, Action<T1, T2, T3, T4> callBack)
{
RemoveListener(key, (Delegate)callBack);
}
public static void RemoveListener<T1, T2, T3, T4, T5>(string key, Action<T1, T2, T3, T4, T5> callBack)
{
RemoveListener(key, (Delegate)callBack);
}
#endregion
#region 分发消息
public static void Dispatch(string key)
{
List<Delegate> eventList;
if (m_EventDict.TryGetValue(key, out eventList))
{
for (int i = 0; i < eventList.Count; i++)
{
if (eventList[i] is Action)
{
Action callBack = eventList[i] as Action;
callBack?.Invoke();
}
}
}
}
public static void Dispatch<T1>(string key, T1 arg1)
{
List<Delegate> eventList;
if (m_EventDict.TryGetValue(key, out eventList))
{
for (int i = 0; i < eventList.Count; i++)
{
if (eventList[i] is Action<T1>)
{
Action<T1> callBack = eventList[i] as Action<T1>;
callBack?.Invoke(arg1);
}
}
}
}
public static void Dispatch<T1, T2>(string key, T1 arg1, T2 arg2)
{
List<Delegate> eventList;
if (m_EventDict.TryGetValue(key, out eventList))
{
for (int i = 0; i < eventList.Count; i++)
{
if (eventList[i] is Action<T1, T2>)
{
Action<T1, T2> callBack = eventList[i] as Action<T1, T2>;
callBack?.Invoke(arg1, arg2);
}
}
}
}
public static void Dispatch<T1, T2, T3>(string key, T1 arg1, T2 arg2, T3 arg3)
{
List<Delegate> eventList;
if (m_EventDict.TryGetValue(key, out eventList))
{
for (int i = 0; i < eventList.Count; i++)
{
if (eventList[i] is Action<T1, T2, T3>)
{
Action<T1, T2, T3> callBack = eventList[i] as Action<T1, T2, T3>;
callBack?.Invoke(arg1, arg2, arg3);
}
}
}
}
public static void Dispatch<T1, T2, T3, T4>(string key, T1 arg1, T2 arg2, T3 arg3, T4 arg4)
{
List<Delegate> eventList;
if (m_EventDict.TryGetValue(key, out eventList))
{
for (int i = 0; i < eventList.Count; i++)
{
if (eventList[i] is Action<T1, T2, T3, T4>)
{
Action<T1, T2, T3, T4> callBack = eventList[i] as Action<T1, T2, T3, T4>;
callBack?.Invoke(arg1, arg2, arg3, arg4);
}
}
}
}
public static void Dispatch<T1, T2, T3, T4, T5>(string key, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5)
{
List<Delegate> eventList;
if (m_EventDict.TryGetValue(key, out eventList))
{
for (int i = 0; i < eventList.Count; i++)
{
if (eventList[i] is Action<T1, T2, T3, T4, T5>)
{
Action<T1, T2, T3, T4, T5> callBack = eventList[i] as Action<T1, T2, T3, T4, T5>;
callBack?.Invoke(arg1, arg2, arg3, arg4, arg5);
}
}
}
}
#endregion
public static void Log()
{
var sb = new StringBuilder();
sb.Append("----------输出所有注册事件------------");
sb.Append("\n");
foreach (var pair in m_EventDict)
{
sb.AppendFormat($"key = {pair.Key}");
sb.Append("\n");
sb.AppendFormat($"event = {pair.Value.Count}");
sb.Append("\n");
foreach (var callback in pair.Value)
{
sb.AppendFormat($"cs : {callback.Method.DeclaringType} , event name : {callback.Method.Name}");
sb.Append("\n");
}
sb.Append("\n");
}
sb.Append("\n");
sb.Append("---------------------------------------");
Debug.Log(sb);
}
}
/// <summary>
/// 事件常量
/// </summary>
public class EventConst
{
}
更多推荐
已为社区贡献2条内容
所有评论(0)