unity的消息收发机制(观察者模式)

34 篇文章 0 订阅

using SpatialMap_SparseSpatialMap;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;


public static class EventMsgMgr
{
    public delegate void EventFunc(params object[] objs);

    private static Dictionary<string, EventFunc> _messageHanlde = new Dictionary<string, EventFunc>();

    private static Dictionary<string, object> sendHanlde = new Dictionary<string, object>();

    private static List<string> sendHanldeName = new List<string>();

    public static void Init()
    {
        PropCollection.Instance.StartCoroutine(EventMsgMgr.SendEvent());
    }

    //注册消息
    public static void RegisterEvent(string eventID, EventFunc func)
    {
        if (_messageHanlde.ContainsKey(eventID))
        {
            _messageHanlde[eventID] -= func;
            _messageHanlde[eventID] += func;
        }
        else
        {
            _messageHanlde.Add(eventID, func);
        }
    }

    //注销消息
    public static void RemoveEvent(string eventID, EventFunc func)
    {
        if (_messageHanlde.ContainsKey(eventID))
        {
            _messageHanlde[eventID] -= func;
            if (null == _messageHanlde[eventID]) _messageHanlde.Remove(eventID);
        }
    }

    //注销所有消息
    public static void AllRemoveEvent()
    {
        _messageHanlde.Clear();
    }

    //发送消息
    //public static bool SendEvent(string eventID, params object[] objs)
    //{
    //    EventFunc func;
    //    if (_messageHanlde.TryGetValue(eventID, out func))
    //    {
    //        func(objs);
    //        return true;
    //    }
    //    return false;
    //}

    // 发送消息
    public static void SendEvent(string eventID, object objs)
    {
        if (sendHanlde.ContainsKey(eventID))
        {
            sendHanlde[eventID] = objs;
        }
        else
        {
            EventFunc func;
            if (_messageHanlde.TryGetValue(eventID, out func))
            {
                try
                {
                    func(objs);
                }
                catch (Exception e)
                {
                    Debug.LogError("发送" + eventID + "失败:" + e.ToString());
                }
            }
            else
            {
                sendHanldeName.Add(eventID);
                sendHanlde.Add(eventID, objs);
            }
        }
    }

    //static string _key = "";
    static string key = "";

    static WaitForFixedUpdate waitTimer = new WaitForFixedUpdate();

    // 发送消息
    public static IEnumerator SendEvent()
    {
        while (true)
        {
            if (sendHanldeName.Count > 0)
            {
                try
                {
                    for (int i = 0; i < sendHanldeName.Count; i++)
                    {
                        key = sendHanldeName[i];
                        object value = sendHanlde[sendHanldeName[i]];

                        //if (key.Contains("/"))
                        //    _key = key.Split('/')[0];
                        //else
                        //    _key = key;
                        EventFunc func;
                        if (_messageHanlde.TryGetValue(key, out func))
                        {
                            //_key = key;
                            sendHanlde.Remove(key);
                            sendHanldeName.Remove(key);
                            func(value);
                            break;
                        }
                    }
                }
                catch (Exception e)
                {
                    Debug.LogError("发送" + key + "失败:" + e.ToString());
                }
            }
            yield return waitTimer;

        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值