[TWLFramework] MessageCenter

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

public class MessageCenter : Singleton<MessageCenter>
{

private Dictionary<string, List<MessageEvent>> _dictMessageEvent = null;
public override void Init()
{
_dictMessageEvent = new Dictionary<string, List<MessageEvent>>();
}
//add listener
public void AddListener(string msgName, MessageEvent msgEvent)
{
List<MessageEvent> list = null;
if (_dictMessageEvent.ContainsKey(msgName))
{
list = _dictMessageEvent[msgName];
}
else {
list = new List<MessageEvent>();
_dictMessageEvent.Add(msgName, list);
}
if (!list.Contains(msgEvent))
{
list.Add(msgEvent);
}
}
//remove listener
public void RemoveListener(string msgName, MessageEvent msgEvent)
{
if (_dictMessageEvent.ContainsKey(msgName))
{
List<MessageEvent> list = _dictMessageEvent[msgName];
if (list.Contains(msgEvent))
{
list.Remove(msgEvent);
}
if (list.Count <= 0)
{
_dictMessageEvent.Remove(msgName);
}
}
}
//remove all listener
public void RemoveAllListener()
{
_dictMessageEvent.Clear();
}
#region send message
public void SendMessage(Message message)
{
DoMessageDispatcher(message);
}

public void SendMessage(string name, object sender)
{
SendMessage(new Message(name, sender));
}
public void SendMessage(string name, object sender, object content)
{
SendMessage(new Message(name, sender, content));
}
public void SendMessage(string name, object sender, object content, params object[] dictParams)
{
SendMessage(new Message(name, sender, content, dictParams));
}
private void DoMessageDispatcher(Message msg)
{
if (_dictMessageEvent == null || !_dictMessageEvent.ContainsKey(msg.Name))
{
return;
}
List<MessageEvent> list = _dictMessageEvent[msg.Name];
for (int i = 0; i < list.Count; i++)
{
MessageEvent msgEvent = list[i];
if (msgEvent != null)
{
msgEvent(msg);
}
}

}

#endregion

}

时间: 2024-12-17 00:27:50

[TWLFramework] MessageCenter的相关文章

[TWLFramework] Message

using UnityEngine;using System.Collections;using System.Collections.Generic;using System;/* 消息体 直接遍历 key访问 message[key] send() remove() add() 构造函数参数=>sender type(name) content*/ public class Message : IEnumerable<KeyValuePair<string, object>&g

[TWLFramework] Singleton

using UnityEngine;using System.Collections; //单例模式 public class Singleton<T> where T : class, new(){ protected static T _instance = null; public static T Instance { get { if (_instance == null) { _instance = new T(); } return _instance; } } protecte

[TWLFramework] BaseUI

using UnityEngine;using System.Collections; public abstract class BaseUI : MonoBehaviour{ //缓存 gameobject 和 transform private GameObject _cacheGameObject; public GameObject CacheGameObject { get { if (_cacheGameObject == null) { _cacheGameObject = th

[TWLFramework] BasePanel

using UnityEngine;using System.Collections; public abstract class BasePanel : MonoBehaviour{ //panel State protected E_PanelState _panelState; public E_PanelState PanelState { get { return _panelState; } set { E_PanelState old = this._panelState; thi

[TWLFramework] UIManager

using UnityEngine;using System.Collections;using System.Collections.Generic; public class UIManager : Singleton<UIManager>{ //UI gameobjects public Dictionary<E_UIType, GameObject> _dictUIs = new Dictionary<E_UIType, GameObject>(); //UI

[TWLFramework] 全局委托 全局枚举

using UnityEngine;using System.Collections; #region 全局委托 public delegate void MessageEvent(Message message); #endregion #region 全局枚举//panel Typepublic enum E_UIType{ None, FaceUI_Login, FaceUI_SelectRole,}//UI Typepublic enum E_PanelType{ None, E_Mia

零配置socket消息通讯容器EC

EC全称是elastic communication,是基于c#实现的Socket网络通讯服务容器,支持windows .Net和mono.通过EC容器可以让开发人员在不了解Socket网络通讯知识和不进行任何配置的情况即可把逻辑应用部署到一个网络通讯服务中.EC的目标让开发人员在编写传统Socket网络通讯服务的时候就象编写asp.net应用一样简单,只需要定义消息和控制器方法即可实施一个基于网络Socket通讯应用服务程序. 以下通过一个HelloWord例程来表述一下EC在这方面的简单灵活

消息框架的一种实现

自从在Android中用上了消息框架,屡试不爽.不管是主线程发任务到后台线程,还是后台线程返回结果到主线程,甚至是完全在主线程中的调用,都用发消息-监听消息-收消息这种方式处理,真是解耦利器. 之前写过的两篇文章:用消息机制获取网络数据 和 用消息机制解耦Activity跳转. 之前在工程中都用的是开源的EventBus,很好用,但是因为是定制好的,缺少很多灵活性,监听方法不可改名,不可从父类override,不可以通过泛型参数声明,等等. 自己尝试用Handler实现了一个消息框架,还没完全搞

打开待办前,将NC界面切换到任务中心

1 打开待办前,将NC界面切换到任务中心 2 Object messagecenter = WorkbenchEnvironment.getInstance() 3 .getClientCache(NCMessageMainUI.class.getName()); 4 Object shell = WorkbenchEnvironment.getInstance().getClientCache( 5 MessageCenterShell.class.getName()); 6 if (shel