Giter VIP home page Giter VIP logo

simplemsgdispatcher's Introduction

简易消息机制

使用说明

注册消息

步骤:

  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);
        }
    }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.