写一个简单的消息机制

 发送消息

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class sender : MonoBehaviour,IMsgSender,IMsgReceiver {

	// Use this for initialization
	void Awake () {
        this.RegisterLogicMsg("2",ReceiveMsg);
	}
	
	// Update is called once per frame
	void Update () {
        if (Input.GetKeyDown(KeyCode.A))
        {
            this.SendLogicMsg("1",this.GetType().Name,":我好了,弟弟你呢");
        }
	}
    private void ReceiveMsg(object[] args)
    {
        foreach (var item in args)
        {
            Debug.Log(item);
        }
    }
}

 接收消息

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class receiver : MonoBehaviour,IMsgReceiver ,IMsgSender{

	// Use this for initialization
	void Awake () {
        this.RegisterLogicMsg("1",msgReceive);
	}

    private void msgReceive(object[] args)
    {
        foreach (var item in args)
        {
            Debug.Log(item);
        }
        this.SendLogicMsg("2",this.GetType().Name,":我也好了");
    }
}

先看接口

 

啥也没有,那么这些方法哪里来的呢, 很简单,看到上面的调用就知道了,用的this扩展

扩展方法规定类必须是一个静态类,里面包含的所有方法都必须是静态方法。

直接看代码注释吧,不复杂

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public static class MsgDispatche {

    /// <summary>
    /// 消息捕捉
    /// </summary>
    private class MsgHandle
    {

        public readonly IMsgReceiver Receiver;//拿到接收者
        public readonly Action<object[]> Callback;//回调

        public MsgHandle(IMsgReceiver Receiver,Action<object[]> callback)
        {
            this.Receiver = Receiver;
            this.Callback = callback;
        }
    }
    //用字符维护一组消息
    static readonly Dictionary<string, List<MsgHandle>> mMsgHandlerDict = new Dictionary<string, List<MsgHandle>>();
    //msdn是这样规定扩展方法的:“扩展方法被定义为静态方法,
    //但它们是通过实例方法语法进行调用的。 
    //它们的第一个参数指定该方法作用于哪个类型,并且该参数以 this 修饰符为前缀。”
    public static void RegisterLogicMsg(this IMsgReceiver self,string name,Action<object[]> callback)
    {
        if (!mMsgHandlerDict.ContainsKey(name))
        {
            mMsgHandlerDict[name] = new List<MsgHandle>();
        }

        var handler = mMsgHandlerDict[name];
        
        //防止重复注册
        foreach (var handle in handler)
        {
            if (handle.Receiver == self && handle.Callback == callback)
            {
                Debug.LogError("RegisterMsg:" + name + " ayready Register");
                return;
            }
        }
        handler.Add(new MsgHandle(self, callback));
        
    }

    public static void SendLogicMsg(this IMsgSender self,string name,params object[] parmList)
    {
        var handler = mMsgHandlerDict[name];
        var handlerCount = handler.Count;
        //从后前取不会导致索引变化
        for (int i = handlerCount-1; i >=0 ; i--)
        {
            var handle = handler[i];
            if (handle.Receiver!=null)
            {
                handle.Callback(parmList);
            }
            else
            {
                handler.Remove(handle);
            }
        }
    }
}

功能很单一,可以根据自己需求进一步扩展

猜你喜欢

转载自blog.csdn.net/lvcoc/article/details/88057320