Unity 游戏框架:UI 管理神器 UI Kit (8)

我们先来看看 QMgrID 是什么?

public class QMsgSpan { public const int Count = 3000; } public partial class QMgrID { public const int Framework = 0; public const int UI = Framework + QMsgSpan.Count; // 3000 public const int Audio = UI + QMsgSpan.Count; // 6000 public const int Network = Audio + QMsgSpan.Count; public const int UIFilter = Network + QMsgSpan.Count; public const int Game = UIFilter + QMsgSpan.Count; public const int PCConnectMobile = Game + QMsgSpan.Count; public const int FrameworkEnded = PCConnectMobile + QMsgSpan.Count; public const int FrameworkMsgModuleCount = 7; } }

是一个长度为 3000 的区间,那么从 3000 ~ 5999 则是 UI 模块的消息。

而 6000 ~ 8999 之间的则是 Audio 消息。不难理解。

这样就实现了一个模块化的消息频段。

UIGamePausePanel 发送消息的顺序为:

UIGamePausePanel -> UIMgr -> UIGamePanel。

这里还没有介绍 UIMgr 为什么可以中转消息。

我们看了 QMgrBehaviour 与 QMonoBehaivour 实现就知道了:

QMgrBehaviour.cs

/* Copyright (c) 2018.3 liangxie */ namespace QFramework { using System; using System.Collections.Generic; /// <summary> /// manager基类 /// </summary> public abstract class QMgrBehaviour : QMonoBehaviour,IManager { private readonly QEventSystem mEventSystem = NonPublicObjectPool<QEventSystem>.Instance.Allocate(); #region IManager public virtual void Init() {} #endregion protected int mMgrId = 0; protected abstract void SetupMgrId (); protected override void SetupMgr () { mCurMgr = this; } protected QMgrBehaviour() { SetupMgrId (); } public void RegisterEvents<T>(IEnumerable<T> eventIds,OnEvent process) where T: IConvertible { foreach (var eventId in eventIds) { RegisterEvent(eventId,process); } } public void RegisterEvent<T>(T msgId,OnEvent process) where T:IConvertible { mEventSystem.Register (msgId, process); } public void UnRegisterEvents(List<ushort> msgs,OnEvent process) { for (int i = 0;i < msgs.Count;i++) { UnRegistEvent(msgs[i],process); } } public void UnRegistEvent(int msgEvent,OnEvent process) { mEventSystem.UnRegister (msgEvent, process); } public override void SendMsg(QMsg msg) { if (msg.ManagerID == mMgrId) { Process(msg.EventID, msg); } else { QMsgCenter.Instance.SendMsg (msg); } } public override void SendEvent<T>(T eventId) { SendMsg(QMsg.Allocate(eventId)); } // 来了消息以后,通知整个消息链 protected override void ProcessMsg(int eventId,QMsg msg) { mEventSystem.Send(msg.EventID,msg); } } }

QMonoBehaviour.cs

/* Copyright (c) 2017 liangxie */ namespace QFramework { using UnityEngine; using System; using System.Collections.Generic; public abstract class QMonoBehaviour : MonoBehaviour { protected bool mReceiveMsgOnlyObjActive = true; public void Process (int eventId, params object[] param) { if (mReceiveMsgOnlyObjActive && gameObject.activeInHierarchy || !mReceiveMsgOnlyObjActive) { QMsg msg = param[0] as QMsg; ProcessMsg(eventId, msg); msg.Processed = true; if (msg.ReuseAble) { msg.Recycle2Cache(); } } } protected virtual void ProcessMsg (int eventId,QMsg msg) {} protected abstract void SetupMgr (); private QMgrBehaviour mPrivateMgr = null; protected QMgrBehaviour mCurMgr { get { if (mPrivateMgr == null ) { SetupMgr (); } if (mPrivateMgr == null) { Debug.LogError ("not set mgr yet"); } return mPrivateMgr; } set { mPrivateMgr = value; } } public virtual void Show() { gameObject.SetActive (true); OnShow (); } protected virtual void OnShow() {} public virtual void Hide() { OnHide (); gameObject.SetActive (false); Log.I("On Hide:{0}",name); } protected virtual void OnHide() {} protected void RegisterEvents<T>(params T[] eventIDs) where T : IConvertible { foreach (var eventId in eventIDs) { RegisterEvent(eventId); } } protected void RegisterEvent<T>(T eventId) where T : IConvertible { mEventIds.Add(eventId.ToUInt16(null)); mCurMgr.RegisterEvent(eventId, Process); } protected void UnRegisterEvent<T>(T eventId) where T : IConvertible { mEventIds.Remove(eventId.ToUInt16(null)); mCurMgr.UnRegistEvent(eventId.ToInt32(null), Process); } protected void UnRegisterAllEvent() { if (null != mPrivateEventIds) { mCurMgr.UnRegisterEvents(mEventIds, Process); } } public virtual void SendMsg(QMsg msg) { mCurMgr.SendMsg(msg); } public virtual void SendEvent<T>(T eventId) where T : IConvertible { mCurMgr.SendEvent(eventId); } private List<ushort> mPrivateEventIds = null; private List<ushort> mEventIds { get { if (null == mPrivateEventIds) { mPrivateEventIds = new List<ushort>(); } return mPrivateEventIds; } } protected virtual void OnDestroy() { OnBeforeDestroy(); mCurMgr = null; if (Application.isPlaying) { UnRegisterAllEvent(); } } protected virtual void OnBeforeDestroy(){} } }

QMgrBehaivour 维护了一个 QEventSytem 成员。用来在模块内进行消息的中转的。

那么如何实现跨模块之间的消息发送呢?

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wsppfj.html