Unity 游戏框架搭建:第十三、十四个示例整理

本文介绍了一个Unity项目中消息机制的整合实现,通过MonoBehaviourSimplify类简化消息注册、发送及注销流程,有效管理游戏对象的消息交互。

第十三个示例整理

代码如下:
    
using System ; using System . Collections ; using System . Collections . Generic ; using UnityEngine ; namespace QFramework { public abstract partial class MonoBehaviourSimplify { List < MsgRecord > mMsgRecorder = new List < MsgRecord > ( ) ; private class MsgRecord { private static readonly Stack < MsgRecord > mMsgRecordPool = new Stack < MsgRecord > ( ) ; public static MsgRecord Allocate ( string msgName , Action < object > onMsgReceived ) { MsgRecord retMsgRecord = null ; retMsgRecord = mMsgRecordPool . Count > 0 ? mMsgRecordPool . Pop ( ) : new MsgRecord ( ) ; retMsgRecord . Name = msgName ; retMsgRecord . OnMsgReceived = onMsgReceived ; return retMsgRecord ; } public void Recycle ( ) { Name = null ; OnMsgReceived = null ; mMsgRecordPool . Push ( this ) ; } public string Name ; public Action < object > OnMsgReceived ; } protected void RegisterMsg ( string msgName , Action < object > onMsgReceived ) { MsgDispatcher . Register ( msgName , onMsgReceived ) ; mMsgRecorder . Add ( MsgRecord . Allocate ( msgName , onMsgReceived ) ) ; } private void OnDestroy ( ) { OnBeforeDestroy ( ) ; foreach ( var msgRecord in mMsgRecorder ) { MsgDispatcher . UnRegister ( msgRecord . Name , msgRecord . OnMsgReceived ) ; msgRecord . Recycle ( ) ; } mMsgRecorder . Clear ( ) ; } protected abstract void OnBeforeDestroy ( ) ; } public class MsgDistapcherInMonoBehaviourSimplify : MonoBehaviourSimplify { # if UNITY_EDITOR [ UnityEditor . MenuItem ( "QFramework/13.消息机制集成到 MonoBehaviourSimplify" , false , 14 ) ] private static void MenuClicked ( ) { MsgDispatcher . UnRegisterAll ( "Do" ) ; UnityEditor . EditorApplication . isPlaying = true ; new GameObject ( "MsgReceiverObj" ) . AddComponent < MsgDistapcherInMonoBehaviourSimplify > ( ) ; } # endif private void Awake ( ) { RegisterMsg ( "Do" , DoSomething ) ; RegisterMsg ( "Do" , DoSomething ) ; RegisterMsg ( "DO1" , _ => { } ) ; RegisterMsg ( "DO2" , _ => { } ) ; RegisterMsg ( "DO3" , _ => { } ) ; } private IEnumerator Start ( ) { MsgDispatcher . Send ( "Do" , "hello" ) ; yield return new WaitForSeconds ( 1.0f ) ; MsgDispatcher . Send ( "Do" , "hello1" ) ; } void DoSomething ( object data ) { // do something Debug . LogFormat ( "Received Do msg:{0}" , data ) ; } protected override void OnBeforeDestroy ( ) { } } }
在之前已经整理过 MonoBehaviourSimplify 了,所以示例中 MonoBehaviourSimplify 合并就可以了。而下边的 MsgDistapcherInMonoBehaviourSimplify,这个是目前框架的使用示例,所以就名字改成 FrameworkExample,中文名字也叫框架示例。
整理后代码如下: Assets/QFramework/Framework/MonoBehaviourSimplify.cs
    
using System ; using System . Collections ; using System . Collections . Generic ; using UnityEngine ; namespace QFramework { public abstract partial class MonoBehaviourSimplify : MonoBehaviour { # region GameObjectSimplify public void Show ( ) { GameObjectSimplify . Show ( gameObject ) ; } public void Hide ( ) { GameObjectSimplify . Hide ( gameObject ) ; } # endregion # region TransformSimplify public void Identity ( ) { TransformSimplify . Identity ( transform ) ; } # endregion # region Timer public void Delay ( float seconds , Action onFinished ) { StartCoroutine ( DelayCoroutine ( seconds , onFinished ) ) ; } private static IEnumerator DelayCoroutine ( float seconds , Action onFinished ) { yield return new WaitForSeconds ( seconds ) ; onFinished ( ) ; } # endregion # region MsgDispatcher List < MsgRecord > mMsgRecorder = new List < MsgRecord > ( ) ; private class MsgRecord { private static readonly Stack < MsgRecord > mMsgRecordPool = new Stack < MsgRecord > ( ) ; public static MsgRecord Allocate ( string msgName , Action < object > onMsgReceived ) { MsgRecord retMsgRecord = null ; retMsgRecord = mMsgRecordPool . Count > 0 ? mMsgRecordPool . Pop ( ) : new MsgRecord ( ) ; retMsgRecord . Name = msgName ; retMsgRecord . OnMsgReceived = onMsgReceived ; return retMsgRecord ; } public void Recycle ( ) { Name = null ; OnMsgReceived = null ; mMsgRecordPool . Push ( this ) ; } public string Name ; public Action < object > OnMsgReceived ; } protected void RegisterMsg ( string msgName , Action < object > onMsgReceived ) { MsgDispatcher . Register ( msgName , onMsgReceived ) ; mMsgRecorder . Add ( MsgRecord . Allocate ( msgName , onMsgReceived ) ) ; } private void OnDestroy ( ) { OnBeforeDestroy ( ) ; foreach ( var msgRecord in mMsgRecorder ) { MsgDispatcher . UnRegister ( msgRecord . Name , msgRecord . OnMsgReceived ) ; msgRecord . Recycle ( ) ; } mMsgRecorder . Clear ( ) ; } protected abstract void OnBeforeDestroy ( ) ; # endregion MsgDispatcher } }
大家注意下,笔者加上了 #region 这个宏,主要就是一种注释,这种注释可以把代码括起来。圈定一个范围。在一个类里,东西非常多的时候,非常适合。
Assets/QFramework/Example/10.框架示例/FrameworkExample.cs
    
using System . Collections ; using UnityEngine ; namespace QFramework { public class FrameworkExample : MonoBehaviourSimplify { # if UNITY_EDITOR [ UnityEditor . MenuItem ( "QFramework/Example/10.框架示例" , false , 11 ) ] private static void MenuClicked ( ) { MsgDispatcher . UnRegisterAll ( "Do" ) ; UnityEditor . EditorApplication . isPlaying = true ; new GameObject ( "MsgReceiverObj" ) . AddComponent < FrameworkExample > ( ) ; } # endif private void Awake ( ) { RegisterMsg ( "Do" , DoSomething ) ; RegisterMsg ( "Do" , DoSomething ) ; RegisterMsg ( "DO1" , _ => { } ) ; RegisterMsg ( "DO2" , _ => { } ) ; RegisterMsg ( "DO3" , _ => { } ) ; } private IEnumerator Start ( ) { MsgDispatcher . Send ( "Do" , "hello" ) ; yield return new WaitForSeconds ( 1.0f ) ; MsgDispatcher . Send ( "Do" , "hello1" ) ; } void DoSomething ( object data ) { // do something Debug . LogFormat ( "Received Do msg:{0}" , data ) ; } protected override void OnBeforeDestroy ( ) { } } }
编译之后,菜单如下:
目录如下: !](http://file.liangxiegame.com/d56fd7d6-1e4e-4eb8-8b2d-ab19bb28a450.png)
第十三个示例整理完毕

第十四个示例

代码如下
    
using System ; using UnityEngine ; namespace QFramework { public partial class MonoBehaviourSimplify { protected void UnRegisterMsg ( string msgName ) { var selectedRecords = mMsgRecorder . FindAll ( recorder => recorder . Name == msgName ) ; selectedRecords . ForEach ( selectRecord => { MsgDispatcher . UnRegister ( selectRecord . Name , selectRecord . OnMsgReceived ) ; mMsgRecorder . Remove ( selectRecord ) ; } ) ; selectedRecords . Clear ( ) ; } protected void UnRegisterMsg ( string msgName , Action < object > onMsgReceived ) { var selectedRecords = mMsgRecorder . FindAll ( recorder => recorder . Name == msgName && recorder . OnMsgReceived == onMsgReceived ) ; selectedRecords . ForEach ( selectRecord => { MsgDispatcher . UnRegister ( selectRecord . Name , selectRecord . OnMsgReceived ) ; mMsgRecorder . Remove ( selectRecord ) ; } ) ; selectedRecords . Clear ( ) ; } protected void SendMsg ( string msgName , object data ) { MsgDispatcher . Send ( msgName , data ) ; } } public class UnifyAPIStyle : MonoBehaviourSimplify { # if UNITY_EDITOR [ UnityEditor . MenuItem ( "QFramework/14.统一 API 风格" , false , 14 ) ] private static void MenuClicked ( ) { MsgDispatcher . UnRegisterAll ( "OK" ) ; UnityEditor . EditorApplication . isPlaying = true ; new GameObject ( "MsgReceiverObj" ) . AddComponent < UnifyAPIStyle > ( ) ; } # endif private void Awake ( ) { RegisterMsg ( "OK" , data => { Debug . Log ( data ) ; UnRegisterMsg ( "OK" ) ; } ) ; } private void Start ( ) { SendMsg ( "OK" , "hello" ) ; SendMsg ( "OK" , "hello" ) ; } protected override void OnBeforeDestroy ( ) { } } }
同样是 MonoBehaviourSimplify ,所以可以合并到之前的 MonoBehaviourSimplify 文件里。而这个示例呢,是一个消息注销的部分呢示例,也算是,框架的示例一部分,所以这个 Example 要和框架示例进行合并。
合并之后代码如下: Assets/QFramework/Framework/MonoBehaviourSimplify.cs
    
using System ; using System . Collections ; using System . Collections . Generic ; using UnityEngine ; namespace QFramework { public abstract partial class MonoBehaviourSimplify : MonoBehaviour { # region GameObjectSimplify public void Show ( ) { GameObjectSimplify . Show ( gameObject ) ; } public void Hide ( ) { GameObjectSimplify . Hide ( gameObject ) ; } # endregion # region TransformSimplify public void Identity ( ) { TransformSimplify . Identity ( transform ) ; } # endregion # region Timer public void Delay ( float seconds , Action onFinished ) { StartCoroutine ( DelayCoroutine ( seconds , onFinished ) ) ; } private static IEnumerator DelayCoroutine ( float seconds , Action onFinished ) { yield return new WaitForSeconds ( seconds ) ; onFinished ( ) ; } # endregion # region MsgDispatcher List < MsgRecord > mMsgRecorder = new List < MsgRecord > ( ) ; private class MsgRecord { private static readonly Stack < MsgRecord > mMsgRecordPool = new Stack < MsgRecord > ( ) ; public static MsgRecord Allocate ( string msgName , Action < object > onMsgReceived ) { MsgRecord retMsgRecord = null ; retMsgRecord = mMsgRecordPool . Count > 0 ? mMsgRecordPool . Pop ( ) : new MsgRecord ( ) ; retMsgRecord . Name = msgName ; retMsgRecord . OnMsgReceived = onMsgReceived ; return retMsgRecord ; } public void Recycle ( ) { Name = null ; OnMsgReceived = null ; mMsgRecordPool . Push ( this ) ; } public string Name ; public Action < object > OnMsgReceived ; } protected void RegisterMsg ( string msgName , Action < object > onMsgReceived ) { MsgDispatcher . Register ( msgName , onMsgReceived ) ; mMsgRecorder . Add ( MsgRecord . Allocate ( msgName , onMsgReceived ) ) ; } protected void UnRegisterMsg ( string msgName ) { var selectedRecords = mMsgRecorder . FindAll ( recorder => recorder . Name == msgName ) ; selectedRecords . ForEach ( selectRecord => { MsgDispatcher . UnRegister ( selectRecord . Name , selectRecord . OnMsgReceived ) ; mMsgRecorder . Remove ( selectRecord ) ; } ) ; selectedRecords . Clear ( ) ; } protected void UnRegisterMsg ( string msgName , Action < object > onMsgReceived ) { var selectedRecords = mMsgRecorder . FindAll ( recorder => recorder . Name == msgName && recorder . OnMsgReceived == onMsgReceived ) ; selectedRecords . ForEach ( selectRecord => { MsgDispatcher . UnRegister ( selectRecord . Name , selectRecord . OnMsgReceived ) ; mMsgRecorder . Remove ( selectRecord ) ; } ) ; selectedRecords . Clear ( ) ; } protected void SendMsg ( string msgName , object data ) { MsgDispatcher . Send ( msgName , data ) ; } private void OnDestroy ( ) { OnBeforeDestroy ( ) ; foreach ( var msgRecord in mMsgRecorder ) { MsgDispatcher . UnRegister ( msgRecord . Name , msgRecord . OnMsgReceived ) ; msgRecord . Recycle ( ) ; } mMsgRecorder . Clear ( ) ; } protected abstract void OnBeforeDestroy ( ) ; # endregion MsgDispatcher } }
Assets/QFramework/Example/10.框架示例/FrameworkExample.cs
    
using System . Collections ; using UnityEngine ; namespace QFramework { public class FrameworkExample : MonoBehaviourSimplify { # if UNITY_EDITOR [ UnityEditor . MenuItem ( "QFramework/Example/10.框架示例" , false , 11 ) ] private static void MenuClicked ( ) { MsgDispatcher . UnRegisterAll ( "Do" ) ; MsgDispatcher . UnRegisterAll ( "OK" ) ; UnityEditor . EditorApplication . isPlaying = true ; new GameObject ( "MsgReceiverObj" ) . AddComponent < FrameworkExample > ( ) ; } # endif private void Awake ( ) { RegisterMsg ( "Do" , DoSomething ) ; RegisterMsg ( "Do" , DoSomething ) ; RegisterMsg ( "DO1" , _ => { } ) ; RegisterMsg ( "DO2" , _ => { } ) ; RegisterMsg ( "DO3" , _ => { } ) ; RegisterMsg ( "OK" , data => { Debug . Log ( data ) ; UnRegisterMsg ( "OK" ) ; } ) ; } private IEnumerator Start ( ) { SendMsg ( "OK" , "hello" ) ; SendMsg ( "OK" , "hello" ) ; MsgDispatcher . Send ( "Do" , "hello" ) ; yield return new WaitForSeconds ( 1.0f ) ; MsgDispatcher . Send ( "Do" , "hello1" ) ; } void DoSomething ( object data ) { // do something Debug . LogFormat ( "Received Do msg:{0}" , data ) ; } protected override void OnBeforeDestroy ( ) { } } }
目录如下:

菜单如下:

呼,我们终于整理完了。
转载请注明地址:凉鞋的笔记: liangxiegame.com 订阅全套专栏或参加小班: liangxiegame.com
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值