Skip to content

基于unity一个简单的消息机制,拿来即用

Notifications You must be signed in to change notification settings

lfzl000/SimpleMsgDispatcher

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 

Repository files navigation

简易消息机制

使用说明

注册消息

步骤:

  1. 引入命名空间 ZLMsg
  2. 实现接口 IMsgReceiver
  3. 在 MsgName 定义一个消息名

注册消息代码示例:

using ZLMsg;
using UnityEngine;

public class Receiver : MonoBehaviour, IMsgReceiver
{
    private void Awake()
    {
        this.RegisterLogicMsg(MsgName.MSG_TESTMSGNAME, ReceiveMsg);
    }

    private void ReceiveMsg(IMsgParam args)
    {
        MsgParam<string> msgParam = args as MsgParam<string>;
        Debug.Log(msgParam.param);
    }

    private void OnDestroy()
    {
        this.UnRegisterLogicMsg(MsgName.MSG_TESTMSGNAME, ReceiveMsg);
    }
}

定义消息名称:

namespace ZLMsg
{
    //不要用0
    public class MsgName
    {
        public const int MSG_TESTMSGNAME = 1000;
    }
}

发送消息

步骤:

  1. 引入命名空间 ZLMsg
  2. 实现接口 IMsgSender

发送消息代码示例:

using ZLMsg;
using UnityEngine;

public class Sender : MonoBehaviour, IMsgSender
{
    private MsgParam<string> msgParam;
    private void Start()
    {
        msgParam = new MsgParam<string>();
        msgParam.SetParam("Hello World");
    }

    private void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            this.SendLogicMsg(MsgName.MSG_TESTMSGNAME, msgParam);
        }
    }
}

About

基于unity一个简单的消息机制,拿来即用

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages