usingSystem.Collections.Generic;usingSystem; Public classmessagecenter{Private StaticMessagecenter instance; Public Staticmessagecenter Instance {Get{if(Instance = =NULL) Instance =NewMessagecenter ();returninstance;} } Action<Object>Action; Private Staticdictionary<string, list<action<Object>>> dic =Newdictionary<string, list<action<Object>>>(); /// <summary> ///Increased monitoring/// </summary> /// <param name= "str" ></param> /// <param name= "action" ></param> Public Static voidAddListener (stringstr,action<Object>action) { if(DIC. ContainsKey (str)) {DIC[STR]. ADD (action); } Else{List<Action<Object>> list =Newlist<action<Object>>(); List. ADD (action); Dic. ADD (str, list); } } /// <summary> ///Remove a listener/// </summary> /// <param name= "str" ></param> Public Static voidRemoveListener (stringstr) { if(!DiC. ContainsKey (str)) {return; } for(intI=0; I<dic[str]. count;i++) {Dic[str]. Clear (); } } /// <summary> ///Send Message/// </summary> /// <param name= "str" ></param> /// <param name= "obj" ></param> Public Static voidSendMessage (stringStrObjectobj) { if(DIC. ContainsKey (str)) { for(intI=0; I<dic[str]. count;i++) {dic[str][i] (obj); } } } /// <summary> ///Clear the Monitor/// </summary> Public Static voidClear () {dic. Clear (); }}
Unity Message Center