美文网首页游戏框架征服Unity3dunity3D技术分享
Unity 游戏框架搭建 (五) 简易消息机制

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

作者: 凉鞋的笔记 | 来源:发表于2017-07-07 13:20 被阅读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.运行结果


相关文章

网友评论

    本文标题:Unity 游戏框架搭建 (五) 简易消息机制

    本文链接:https://www.haomeiwen.com/subject/dbblhxtx.html