# SimpleMsgDispatcher **Repository Path**: zlr710/SimpleMsgDispatcher ## Basic Information - **Project Name**: SimpleMsgDispatcher - **Description**: No description available - **Primary Language**: C# - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2019-12-04 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 简易消息机制 ## 使用说明 ### 注册消息 步骤: 1. 引入命名空间 ZLMsg 2. 实现接口 IMsgReceiver 3. 在 MsgName 定义一个消息名 注册消息代码示例: ``` csharp using ZLMsg; using UnityEngine; public class Receiver : MonoBehaviour, IMsgReceiver { private void Awake() { this.RegisterLogicMsg(MsgName.MSG_TESTMSGNAME, ReceiveMsg); } private void ReceiveMsg(IMsgParam args) { MsgParam msgParam = args as MsgParam; Debug.Log(msgParam.param); } private void OnDestroy() { this.UnRegisterLogicMsg(MsgName.MSG_TESTMSGNAME, ReceiveMsg); } } ``` 定义消息名称: ``` csharp namespace ZLMsg { //不要用0 public class MsgName { public const int MSG_TESTMSGNAME = 1000; } } ``` ### 发送消息 步骤: 1. 引入命名空间 ZLMsg 2. 实现接口 IMsgSender 发送消息代码示例: ``` csharp using ZLMsg; using UnityEngine; public class Sender : MonoBehaviour, IMsgSender { private MsgParam msgParam; private void Start() { msgParam = new MsgParam(); msgParam.SetParam("Hello World"); } private void Update() { if (Input.GetMouseButtonDown(0)) { this.SendLogicMsg(MsgName.MSG_TESTMSGNAME, msgParam); } } } ```