【Unity游戏开发】用C#和Lua实现Unity中的事件分发机制EventDispatcher

时间:2022-11-27 17:27:34

一、简介

  最近马三换了一家大公司工作,公司制度规范了一些,因此平时的业余时间多了不少。但是人却懒了下来,最近这一个月都没怎么研究新技术,博客写得也是拖拖拉拉,周六周天就躺尸在家看帖子、看小说,要么就是吃鸡,唉!真是罪过罪过。希望能从这篇博客开始有些改善吧,尽量少玩耍,还是多学习吧~

  好了扯得有点远了,来说说我们今天博客的主题——“用C#和Lua实现Unity中的事件分发机制”,事件分发机制或者叫事件监听派发系统,在每个游戏框架中都是不可或缺的一个模块。我们可以用它来解耦,监听网络消息,或者做一些异步的操作,好处多多(其实是别人的框架都有这个,所以我们的框架也必须有这玩意~)。今天马三就和大家一起,分别使用C#和Lua实现两种可以用在Unity游戏开发中的事件分发处理机制,希望能对大家有些帮助吧~

二、C#版的事件分发机制

  首先我们来实现C#版本的事件分发机制,目前这套流程已经集成到了马三自己的 ColaFrameWork框架 中了。这套框架还在架构阶段,里面很多东西都不完善,马三也是会随时把自己的一些想法放到里面,大家感兴趣的话也可以帮忙维护一下哈!

  一般来说事件订阅、派发这种机制都是使用观察者模式来实现的,本篇博客也不例外,正是利用了这种思想。为了解耦和面向接口编程,我们制定了一个接口IEventHandler,凡是观察者都需要实现这个接口,而GameEventMgr事件中心维护了一个IEventHandler列表,保存着一系列的观察者,并在需要的时候进行一系列的动作。这样操作正是遵循了依赖倒置的设计原则:“高层模块不应该依赖于低层模块,两者都应该依赖于抽象概念;”、“抽象接口不应该依赖于实现,而实现应该依赖于抽象接口”。下面的代码定义了IEventHandler接口和一些委托还有事件传递时需要携带的参数。

 using System.Collections;
using System.Collections.Generic;
using EventType = ColaFrame.EventType; /// <summary>
/// 接收消息后触发的回调
/// </summary>
/// <param name="data"></param>
public delegate void MsgHandler(EventData data); /// <summary>
/// 事件处理器的接口
/// </summary>
public interface IEventHandler
{
bool HandleMessage(GameEvent evt); bool IsHasHandler(GameEvent evt);
} /// <summary>
/// 事件消息传递的数据
/// </summary>
public class EventData
{
public string Cmd;
public List<object> ParaList;
} /// <summary>
/// 游戏中的事件
/// </summary>
public class GameEvent
{
/// <summary>
/// 事件类型
/// </summary>
public EventType EventType { get; set; }
/// <summary>
/// 携带参数
/// </summary>
public object Para { get; set; }
} namespace ColaFrame
{
/// <summary>
/// 事件的类型
/// </summary>
public enum EventType : byte
{
/// <summary>
/// 系统的消息
/// </summary>
SystemMsg = ,
/// <summary>
/// 来自服务器推送的消息
/// </summary>
ServerMsg = ,
/// <summary>
/// UI界面消息
/// </summary>
UIMsg = ,
}
}

  然后我们再来看一下最核心的事件中心处理器GameEventMgr是如何实现的,还是先上一下全部的代码 GameEventMgr.cs:

 using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using EventType = ColaFrame.EventType; /// <summary>
/// 事件消息管理中心
/// </summary>
public class GameEventMgr
{
/// <summary>
/// 存储Hander的字典
/// </summary>
private Dictionary<int, List<IEventHandler>> handlerDic; private static GameEventMgr instance = null; private GameEventMgr()
{
handlerDic = new Dictionary<int, List<IEventHandler>>();
} public static GameEventMgr GetInstance()
{
if (null == instance)
{
instance = new GameEventMgr();
}
return instance;
} /// <summary>
/// 对外提供的注册监听的接口
/// </summary>
/// <param name="handler"></param>监听者(处理回调)
/// <param name="eventTypes"></param>想要监听的事件类型
public void RegisterHandler(IEventHandler handler, params EventType[] eventTypes)
{
for (int i = ; i < eventTypes.Length; i++)
{
RegisterHandler(eventTypes[i], handler);
}
} /// <summary>
/// 内部实际调用的注册监听的方法
/// </summary>
/// <param name="type"></param>要监听的事件类型
/// <param name="handler"></param>监听者(处理回调)
private void RegisterHandler(EventType type, IEventHandler handler)
{
if (null != handler)
{
if (!handlerDic.ContainsKey((int)type))
{
handlerDic.Add((int)type, new List<IEventHandler>());
}
if (!handlerDic[(int)type].Contains(handler))
{
handlerDic[(int)type].Add(handler);
}
}
} /// <summary>
/// 反注册事件监听的接口,对所有类型的事件移除指定的监听
/// </summary>
/// <param name="handler"></param>
public void UnRegisterHandler(IEventHandler handler)
{
using (var enumerator = handlerDic.GetEnumerator())
{
List<IEventHandler> list;
while (enumerator.MoveNext())
{
list = enumerator.Current.Value;
list.Remove(handler);
}
}
} /// <summary>
/// 反注册事件监听的接口,移除指定类型事件的监听
/// </summary>
/// <param name="handler"></param>
/// <param name="types"></param>
public void UnRegisterHandler(IEventHandler handler, params EventType[] types)
{
EventType type;
for (int i = ; i < types.Length; i++)
{
type = types[i];
if (handlerDic.ContainsKey((int)type) && handlerDic[(int)type].Contains(handler))
{
handlerDic[(int)type].Remove(handler);
}
}
} /// <summary>
/// 分发事件
/// </summary>
/// <param name="gameEvent"></param>想要分发的事件
public void DispatchEvent(GameEvent gameEvent)
{
bool eventHandle = false; List<IEventHandler> handlers;
if (null != gameEvent && handlerDic.TryGetValue((int)gameEvent.EventType, out handlers))
{
for (int i = ; i < handlers.Count; i++)
{
try
{
eventHandle = handlers[i].HandleMessage(gameEvent) || eventHandle;
}
catch (Exception e)
{
Debug.LogError(e);
}
} if (!eventHandle)
{
if (null != gameEvent)
{
switch (gameEvent.EventType)
{
case EventType.ServerMsg:
break;
default:
Debug.LogError("消息未处理,类型:" + gameEvent.EventType);
break;
}
}
}
}
} /// <summary>
/// 分发事件
/// </summary>
/// <param name="evt"></param>分发的消息名称
/// <param name="eventType"></param>消息事件类型
/// <param name="para"></param>参数
public void DispatchEvent(string evt, EventType eventType = EventType.UIMsg, params object[] para)
{
GameEvent gameEvent = new GameEvent();
gameEvent.EventType = eventType;
EventData eventData = new EventData();
eventData.Cmd = evt;
if (null != para)
{
eventData.ParaList = new List<object>();
for (int i = ; i < para.Length; i++)
{
eventData.ParaList.Add(para[i]);
}
}
gameEvent.Para = eventData; this.DispatchEvent(gameEvent);
} /// <summary>
/// 分发事件
/// </summary>
/// <param name="evt"></param>分发的消息名称
/// <param name="eventType"></param>消息事件类型
public void DispatchEvent(string evt, EventType eventType = EventType.UIMsg)
{
GameEvent gameEvent = new GameEvent();
gameEvent.EventType = eventType;
EventData eventData = new EventData();
eventData.Cmd = evt;
eventData.ParaList = null;
gameEvent.Para = eventData; this.DispatchEvent(gameEvent);
}
}

我们在其内部维护了一个handlerDic字典,它的key是int类型的,对应的其实就是我们在上面定义的EventType 这个枚举,它的value是一个元素为IEventHandler类型的列表,也就是说我们按照不同的事件类型,将监听者分为了几类进行处理。监听者是可以监听多个消息类型的,也就是说一个监听者实例可以存在于多个列表中,这样并不会产生冲突。我们就从RegisterHandler(IEventHandler handler, params EventType[] eventType)这个对外提供的注册监听的接口入手,逐步的分析一下它的工作流程:

  1. 调用RegisterHandler方法,传入监听者和需要监听的事件类型(可以是数组,支持多个事件类型),然后遍历事件类型,依次调用RegisterHandler(EventType type, IEventHandler handler)接口,将监听者逐个的注册到每个事件类型对应的监听者列表中;
  2. 当需要分发事件的时候,调用DispatchEvent方法,传入一个GameEvent类型的参数gameEvent,它包含了需要派发的事件属于什么类型,和对应的事件消息需要传递的参数,其中这个参数又包含了字符串具体的事件名称和一个参数列表;
  3. 在DispatchEvent中,会根据事件类型来判断内部字段中是否有注册了该事件的监听者,如果有就取到存有这个监听者的列表;
  4. 然后依次遍历每个监听者,调用其HandleMessage方法,进行具体消息的处理,该函数还会返回一个bool值,表示是否处理了该消息。如果遍历了所有的监听者以后,发现没有处理该消息的监听者,则会打印一个错误消息进行提示;
  5. DispatchEvent(string evt, EventType eventType = EventType.UIMsg, params object[] para)和DispatchEvent(string evt, EventType eventType = EventType.UIMsg)这两个接口是对DispatchEvent接口的进一步封装,方便用户进行无参消息派发和含参数消息派发;

最后我们再来看一下具体的监听者应该如何实现IEventHandler接口,以 ColaFrameWork框架 中的UI基类——UIBase举例,在UIBase内部维护了一个Dictionary<string, MsgHandler> msgHanderDic 结构,用它来保存具体的事件名称对应的回调函数,然后再去具体地实现HandleMessage和IsHasHandler接口中的抽象方法,代码如下:

 /// <summary>
/// 处理消息的函数的实现
/// </summary>
/// <param name="gameEvent"></param>事件
/// <returns></returns>是否处理成功
public bool HandleMessage(GameEvent evt)
{
bool handled = false;
if (EventType.UIMsg == evt.EventType)
{
if (null != msgHanderDic)
{
EventData eventData = evt.Para as EventData;
if (null != eventData && msgHanderDic.ContainsKey(eventData.Cmd))
{
msgHanderDic[eventData.Cmd](eventData);
handled = true;
}
}
}
return handled;
} /// <summary>
/// 是否处理了该消息的函数的实现
/// </summary>
/// <returns></returns>是否处理
public bool IsHasHandler(GameEvent evt)
{
bool handled = false;
if (EventType.UIMsg == evt.EventType)
{
if (null != msgHanderDic)
{
EventData eventData = evt.Para as EventData;
if (null != eventData && msgHanderDic.ContainsKey(eventData.Cmd))
{
handled = true;
}
}
}
return handled;
}

为了使用更加简洁方便,我们还可以再封装一些函数出来,以便随时注册一个消息和取消注册一个消息,主要是RegisterEvent和UnRegisterEvent接口,代码如下:

 /// <summary>
/// 初始化注册消息监听
/// </summary>
protected void InitRegisterHandler()
{
msgHanderDic = null;
GameEventMgr.GetInstance().RegisterHandler(this, EventType.UIMsg);
msgHanderDic = new Dictionary<string, MsgHandler>()
{
};
} /// <summary>
/// 取消注册该UI监听的所有消息
/// </summary>
protected void UnRegisterHandler()
{
GameEventMgr.GetInstance().UnRegisterHandler(this); if (null != msgHanderDic)
{
msgHanderDic.Clear();
msgHanderDic = null;
}
} /// <summary>
/// 注册一个UI界面上的消息
/// </summary>
/// <param name="evt"></param>
/// <param name="msgHandler"></param>
public void RegisterEvent(string evt, MsgHandler msgHandler)
{
if (null != msgHandler && null != msgHanderDic)
{
if (!msgHanderDic.ContainsKey(evt))
{
msgHanderDic.Add(Name + evt, msgHandler);
}
else
{
Debug.LogWarning(string.Format("消息{0}重复注册!", evt));
}
}
} /// <summary>
/// 取消注册一个UI界面上的消息
/// </summary>
/// <param name="evt"></param>
public void UnRegisterEvent(string evt)
{
if (null != msgHanderDic)
{
msgHanderDic.Remove(Name + evt);
}
}

关于C#版的事件分发机制大概就介绍到这里了,马三在这里只是大概地讲了下思路,更细致的原理和使用方法大家可以去马三的 ColaFrameWork框架 中找一下相关代码。

三、Lua版的事件分发机制

  Lua版本的事件分发机制相对C#版的来说就简单了很多,Lua中没有接口的概念,因此实现方式和C#版的也大有不同,不过总的来说还是对外暴露出以下几个接口:

  • Instance():获取单例
  • RegisterEvent():注册一个事件
  • UnRegisterEvent():反注册一个事件
  • DispatchEvent():派发事件
  • AddEventListener():增加监听者
  • RemoveEventListener():移除监听者

  照例还是先上一下核心代码EventMgr.lua,然后再逐步解释:

 require("Class")
local bit = require "bit" EventMgr = {
--实例对象
_instance = nil,
--观察者列表
_listeners = nil
}
EventMgr.__index = EventMgr
setmetatable(EventMgr, Class) -- 构造器
function EventMgr:new()
local t = {}
t = Class:new()
setmetatable(t, EventMgr)
return t
end -- 获取单例接口
function EventMgr:Instance()
if EventMgr._instance == nil then
EventMgr._instance = EventMgr:new()
EventMgr._listeners = {}
end
return EventMgr._instance
end function EventMgr:RegisterEvent(moduleId, eventId, func)
local key = bit.lshift(moduleId, ) + eventId
self:AddEventListener(key, func, nil)
end function EventMgr:UnRegisterEvent(moduleId, eventId, func)
local key = bit.lshift(moduleId, ) + eventId
self:RemoveEventListener(key, func)
end function EventMgr:DispatchEvent(moduleId, eventId, param)
local key = bit.lshift(moduleId, ) + eventId
local listeners = self._listeners[key]
if nil == listeners then
return
end
for _, v in ipairs(listeners) do
if v.p then
v.f(v.p, param)
else
v.f(param)
end
end
end function EventMgr:AddEventListener(eventId, func, param)
local listeners = self._listeners[eventId]
-- 获取key对应的监听者列表,结构为{func,para},如果没有就新建
if listeners == nil then
listeners = {}
self._listeners[eventId] = listeners -- 保存监听者
end
--过滤掉已经注册过的消息,防止重复注册
for _, v in pairs(listeners) do
if (v and v.f == func) then
return
end
end
--if func == nil then
-- print("func is nil!")
--end
--加入监听者的回调和参数
table.insert(listeners, { f = func, p = param })
end function EventMgr:RemoveEventListener(eventId, func)
local listeners = self._listeners[eventId]
if nil == listeners then
return
end
for k, v in pairs(listeners) do
if (v and v.f == func) then
table.remove(listeners, k)
return
end
end
end

  在实际使用的时候主要是调用 RegisterEvent、UnRegisterEvent 和 DispatchEvent这三个接口。RegisterEvent用来注册一个事件,UnRegisterEvent 用来反注册一个事件,DispatchEvent用来派发事件。先从RegisterEvent接口说起,它需要传入3个参数,分别是ModuleId,EventId和回调函数func。ModuleId就是我们不同模块的id,他是一个模块的唯一标识,在实际应用中我们可以定义一个全局的枚举来标识这些模块ID。EventId是不同的消息的标识,它也是数字类型的枚举值,并且因为有了模块ID的存在,不同模块可以使用相同的EventId,这并不会导致消息的冲突。在RegisterEvent内部操作中,我们首先对ModuleId进行了左移16位的操作,然后再加上EventID组成我们的消息key,左移16位可以避免ModuleID直接与EventId组合后会产生Key冲突的问题,一般来说左移16位已经可以满足定义很多模块和事件id的需求了。然后调用 self:AddEventListener(key, func, nil) 方法,将计算出来的key和回调函数进行注册。在EventMgr的内部其实还是维护了一个监听者列表,注册消息的时候,就是把回调和参数添加到监听者列表中。反注册消息就是把对应key的回调从监听者列表中移除。派发事件的时候就是遍历key所对应的监听者列表,然后依次执行里面的回调函数。好了,接着说AddEventListener这个函数的操作,它首先会去获取key对应的监听者列表,结构为{func,para},如果没有就新建一个table,并把它保存为key所对应的监听者列表。得到这个监听者列表以后,我们首先会对其进行遍历,如果里面已经包含func回调函数的话,就直接return掉,过滤掉已经注册过的消息,防止重复注册。如果通过了上一步检查的话,就执行 table.insert(listeners, { f = func, p = param })操作,加入监听者的回调和参数。对于UnRegisterEvent方法,我们依然会计算出key,然后调用 RemoveEventListener 操作,把监听者从监听者列表中移除。在使用DispatchEvent接口进行事件派发的时候,我们依然会先计算出Key,然后取出key对应的监听者列表。接着依次遍历这些监听者,然后执行其中保存着的回调函数,并且把需要传递的事件参数传递进去。具体的使用方法,可以参考下面的Main.lua:

 require("EventMgr")

 local function TestCallback_1()
print("Callback_1")
end local function TestCallback_2(param)
print("Callback_2")
print(param.id)
print(param.pwd)
end local EventMgr = EventMgr:Instance()
EventMgr:RegisterEvent(, , TestCallback_1)
EventMgr:RegisterEvent(, , TestCallback_2)
EventMgr:DispatchEvent(, )
EventMgr:DispatchEvent(, , { id = "abc", pwd = "" })

支持含参数事件分发和无参数事件分发,上面代码的执行结果如下,可以发现成功地监听了注册的消息,并且也获取到了传递过来的参数:

【Unity游戏开发】用C#和Lua实现Unity中的事件分发机制EventDispatcher

 图1:代码执行结果

四、总结

通过本篇博客,马三和大家一起学习了如何在Unity中使用C#和Lua分别实现事件分发机制,希望本篇博客能为大家的工作过程中带来一些帮助与启发。

本篇博客中的样例工程已经同步至Github:https://github.com/XINCGer/Unity3DTraining/tree/master/lua/LuaEventMgr,欢迎大家Fork!

马三的开源Unity客户端框架 ColaFramework框架:https://github.com/XINCGer/ColaFrameWork

如果觉得本篇博客对您有帮助,可以扫码小小地鼓励下马三,马三会写出更多的好文章,支持微信和支付宝哟!

 【Unity游戏开发】用C#和Lua实现Unity中的事件分发机制EventDispatcher      【Unity游戏开发】用C#和Lua实现Unity中的事件分发机制EventDispatcher

作者:马三小伙儿
出处:https://www.cnblogs.com/msxh/p/9539231.html 
请尊重别人的劳动成果,让分享成为一种美德,欢迎转载。另外,文章在表述和代码方面如有不妥之处,欢迎批评指正。留下你的脚印,欢迎评论!

我的博客即将搬运同步至腾讯云+社区,邀请大家一同入驻:https://cloud.tencent.com/developer/support-plan?invite_code=3s3rkmf0oback