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;
}
}
}
04-10
144
02-02
1929
07-14
544