游戏框架征服Unity3dunity3D技术分享

Unity 游戏框架搭建 (五) 简易消息机制

2017-07-07  本文已影响176人  凉鞋的笔记

什么是消息机制?






23333333,让我先笑一会。

为什么用消息机制?

  三个字,解!!!!耦!!!!合!!!!。

我的框架中的消息机制用例:

1.接收者

using UnityEngine;
using System.Collections;

using QFramework;

/// <summary>
/// 1.接收者需要实现IMsgReceiver接口。
/// 2.使用this.RegisterLogicMsg注册消息和回调函数。
/// </summary>
public class Receiver : MonoBehaviour,IMsgReceiver {

    void Awake()
    {
        this.RegisterLogicMsg ("Receiver Show Sth", ReceiverMsg);

//      this.UnRegisterLogicMsg ("Receiver Show Sth", ReceiverMsg);

    }


    void ReceiverMsg(params object[] paramList)
    {
        foreach (var sth in paramList) {
            QPrint.Warn (sth.ToString());
        }
    }
}

2.发送者

using UnityEngine;
using System.Collections;
using QFramework;

/// <summary>
/// 1.发送者需要,实现IMsgSender接口
/// 2.调用this.SendLogicMsg发送Receiver Show Sth消息,并传入两个参数
/// </summary>
public class Sender : MonoBehaviour,IMsgSender {

    // Update is called once per frame
    void Update () {
        this.SendLogicMsg ("Receiver Show Sth","你好","世界");
    }
}

3.运行结果


上一篇下一篇

猜你喜欢

热点阅读