unity中事件分发系统 EventDispatcher

这个挺好的。值得一看

出处:http://blog.csdn.net/u010019717

author:孙广东      时间:2015.3.21     23:00

不使用C#中的event关键字: 只是使用delegate和hashtable 进行事件的分发。

           基本库如下:

[csharp]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. namespace EventDispatcher  
  2. {  
  3.     /// <summary>  
  4.     /// IEvent接口  
  5.     /// </summary>  
  6.     public interface IEvent  
  7.     {  
  8.         /// <summary>  
  9.         /// Gets or sets the type.  
  10.         /// </summary>  
  11.         /// <value>  
  12.         /// type类型.  
  13.         /// </value>  
  14.         string type { getset;}       
  15.           
  16.         /// <summary>  
  17.         /// Gets or sets the target.  
  18.         /// </summary>  
  19.         /// <value>  
  20.         /// target目标.  
  21.         /// </value>  
  22.         object target { getset; }  
  23.     }  
  24. }  
[csharp]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. namespace EventDispatcher  
  2. {  
  3.     /// <summary>  
  4.     /// 事件事件接口  
  5.     /// </summary>  
  6.     public class Event : IEvent  
  7.     {  
  8.         // GETTER / SETTER  
  9.         /// <summary>  
  10.         /// The _type_string.  
  11.         /// </summary>  
  12.         private string _type_string;  
  13.         string IEvent.type {   
  14.             get  
  15.             {  
  16.                 return _type_string;  
  17.             }  
  18.             set  
  19.             {  
  20.                 _type_string = value;  
  21.                   
  22.             }  
  23.         }  
  24.           
  25.         /// <summary>  
  26.         /// The _target_object.  
  27.         /// </summary>  
  28.         private object _target_object;  
  29.         object IEvent.target {   
  30.             get  
  31.             {  
  32.                 return _target_object;  
  33.             }  
  34.             set  
  35.             {  
  36.                 _target_object = value;  
  37.                   
  38.             }  
  39.         }  
  40.   
  41.         ///<summary>  
  42.         ///  Constructor  
  43.         ///</summary>  
  44.         public Event (string aType_str )  
  45.         {  
  46.             //  
  47.             _type_string = aType_str;  
  48.         }  
  49.   
  50.         /// <summary>  
  51.         /// Deconstructor  
  52.         /// </summary>  
  53.         //~Event ( )  
  54.         //{  
  55.         //  Debug.Log ("Event.deconstructor()");  
  56.         //}  
  57.     }  
  58. }  
[csharp]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. namespace EventDispatcher  
  2. {  
  3.     /// <summary>  
  4.     /// IEventDispatcher 事件分发接口,添加移除分发  
  5.     /// </summary>  
  6.     public interface IEventDispatcher  
  7.     {  
  8.         /// <summary>  
  9.         /// 添加事件监听  
  10.         /// </summary>  
  11.         /// <returns><c>true</c>, 如果事件成功被监听, <c>false</c> 失败 </returns>  
  12.         /// <param name="aEventType_string">A event type_string.</param>  
  13.         /// <param name="aEventDelegate">一个事件委托</param>  
  14.         bool addEventListener(string aEventType_string, EventDelegate aEventDelegate);  
  15.   
  16.         /// <summary>  
  17.         /// 添加事件监听  
  18.         /// </summary>  
  19.         /// <returns><c>true</c>, 如果事件成功被监听, <c>false</c> 失败 </returns>  
  20.         /// <param name="aEventType_string">A event type_string.</param>  
  21.         /// <param name="aEventDelegate">一个事件委托</param>  
  22.         /// <param name="eventDispatcherAddMode">Event dispatcher add mode.</param>  
  23.         bool addEventListener(string aEventType_string, EventDelegate aEventDelegate, EventDispatcherAddMode eventDispatcherAddMode);  
  24.   
  25.   
  26.         /// <summary>  
  27.         /// 是否有这个事件监听  
  28.         /// </summary>  
  29.         /// <returns><c>true</c>, 这个监听已经有了, <c>false</c> 没有.</returns>  
  30.         /// <param name="aEventType_string">A event type_string.</param>  
  31.         /// <param name="aEventDelegate">一个事件委托</param>  
  32.         bool hasEventListener(string aEventType_string, EventDelegate aEventDelegate);  
  33.   
  34.         /// <summary>  
  35.         /// 移除事件监听  
  36.         /// </summary>  
  37.         /// <returns><c>true</c>, 被成功移除 <c>false</c> 失败</returns>  
  38.         /// <param name="aEventType_string">A event type_string.</param>  
  39.         /// <param name="aEventDelegate">一个事件委托</param>  
  40.         bool removeEventListener(string aEventType_string, EventDelegate aEventDelegate);  
  41.   
  42.         /// <summary>  
  43.         /// 移除所有事件监听  
  44.         /// </summary>  
  45.         /// <returns><c>true</c>, 成功, <c>false</c> 失败 </returns>  
  46.         bool removeAllEventListeners();  
  47.   
  48.         /// <summary>  
  49.         /// 分发广播事件  
  50.         /// </summary>  
  51.         /// <returns><c>true</c>, 成功, <c>false</c> 失败</returns>  
  52.         /// <param name="aIEvent">A I event.</param>  
  53.         bool dispatchEvent(IEvent aIEvent);  
  54.     }  
  55. }  
[csharp]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. // C# unity事件管理器 在哈希表中使用字符串,对委托和事件管理   
  2. // 不知道他们何时何地declared/defined 也允许使用事件。  
  3.   
  4. using UnityEngine;  
  5. using System.Collections;  
  6.   
  7. namespace EventDispatcher  
  8. {  
  9.     /// <summary>  
  10.     /// Event delegateN事件委托  
  11.     /// </summary>  
  12.     public delegate void EventDelegate ( IEvent iEvent );  
  13.       
  14.     /// <summary>  
  15.     /// Event listening mode.  
  16.     /// </summary>  
  17.     public enum EventDispatcherAddMode  
  18.     {  
  19.         DEFAULT,  
  20.         SINGLE_SHOT  
  21.     }  
  22.   
  23.     public class EventDispatcher : IEventDispatcher   
  24.     {  
  25.         /// <summary>  
  26.         /// The mom_serialized object.  
  27.         /// </summary>  
  28.         private Hashtable _eventListenerDatas_hashtable = new Hashtable();  
  29.        
  30.         /// <summary>  
  31.         /// The _target_object.  
  32.         /// </summary>  
  33.         private object _target_object;  
  34.   
  35.         ///<summary>  
  36.         ///  Constructor  
  37.         ///</summary>  
  38.         public EventDispatcher (object aTarget_object )  
  39.         {  
  40.             //  
  41.             _target_object = aTarget_object;  
  42.         }  
  43.           
  44.         /// <summary>  
  45.         /// Deconstructor  
  46.         /// </summary>  
  47.         //~EventDispatcher ( )  
  48.         //{  
  49.         //  Debug.Log ("EventDispatcher.deconstructor()");  
  50.               
  51.         //}  
  52.           
  53.           
  54.         /// <summary>  
  55.         /// Adds the event listener.  
  56.         /// </summary>  
  57.         /// <returns>  
  58.         /// The event listener.  
  59.         /// </returns>  
  60.         /// <param name='aEventName_string'>  
  61.         /// If set to <c>true</c> a event name_string.  
  62.         /// </param>  
  63.         /// <param name='aEventDelegate'>  
  64.         /// If set to <c>true</c> a event delegate.  
  65.         /// </param>  
  66.         public bool addEventListener(string aEventName_string, EventDelegate aEventDelegate)  
  67.         {  
  68.             return addEventListener (aEventName_string, aEventDelegate, EventDispatcherAddMode.DEFAULT);      
  69.         }  
  70.               
  71.         /// <summary>  
  72.         /// Adds the event listener.  
  73.         /// </summary>  
  74.         /// <returns>  
  75.         /// The event listener.  
  76.         /// </returns>  
  77.         /// <param name='aEventName_string'>  
  78.         /// If set to <c>true</c> a event type_string.  
  79.         /// </param>  
  80.         /// <param name='aEventDelegate'>  
  81.         /// If set to <c>true</c> a event delegate.  
  82.         /// </param>  
  83.         /// <param name='aEventDispatcherAddMode'>  
  84.         /// If set to <c>true</c> event listening mode.  
  85.         /// </param>  
  86.         public bool addEventListener(string aEventName_string, EventDelegate aEventDelegate, EventDispatcherAddMode aEventDispatcherAddMode)  
  87.         {  
  88.             //  
  89.             bool wasSuccessful_boolean = false;  
  90.               
  91.             //  
  92.             object aIEventListener = _getArgumentsCallee(aEventDelegate);             
  93.               
  94.             //  
  95.             if (aIEventListener != null && aEventName_string != null) {  
  96.                   
  97.                 //  OUTER  
  98.                 string keyForOuterHashTable_string = _getKeyForOuterHashTable (aEventName_string);  
  99.                 if (!_eventListenerDatas_hashtable.ContainsKey(keyForOuterHashTable_string) ) {  
  100.                     _eventListenerDatas_hashtable.Add(keyForOuterHashTable_string, new Hashtable());  
  101.                 }  
  102.            
  103.                 //  INNER  
  104.                 Hashtable inner_hashtable = _eventListenerDatas_hashtable[keyForOuterHashTable_string] as Hashtable;  
  105.                 EventListenerData eventListenerData = new EventListenerData (aIEventListener, aEventName_string, aEventDelegate, aEventDispatcherAddMode);  
  106.                 //  
  107.                 string keyForInnerHashTable_string = _getKeyForInnerHashTable (eventListenerData);  
  108.                 if (inner_hashtable.Contains(keyForInnerHashTable_string)) {  
  109.                       
  110.                     //THIS SHOULD *NEVER* HAPPEN - REMOVE AFTER TESTED WELL  
  111.                     Debug.Log("TODO (FIX THIS): Event Manager: Listener: " + keyForInnerHashTable_string + " is already in list for event: " + keyForOuterHashTable_string);  
  112.   
  113.                 } else {  
  114.            
  115.                     //  ADD  
  116.                     inner_hashtable.Add(keyForInnerHashTable_string, eventListenerData);  
  117.                     wasSuccessful_boolean = true;  
  118.                     //Debug.Log ("  ADDED AT: " + keyForInnerHashTable_string + " = " +  eventListenerData);  
  119.                 }  
  120.                   
  121.             }  
  122.             return wasSuccessful_boolean;  
  123.         }  
  124.           
  125.           
  126.         /// <summary>  
  127.         /// Hases the event listener.  
  128.         /// </summary>  
  129.         /// <returns>  
  130.         /// The event listener.  
  131.         /// </returns>  
  132.         /// <param name='aIEventListener'>  
  133.         /// If set to <c>true</c> a I event listener.  
  134.         /// </param>  
  135.         /// <param name='aEventName_string'>  
  136.         /// If set to <c>true</c> a event name_string.  
  137.         /// </param>  
  138.         /// <param name='aEventDelegate'>  
  139.         /// If set to <c>true</c> a event delegate.  
  140.         /// </param>  
  141.         public bool hasEventListener(string aEventName_string, EventDelegate aEventDelegate)  
  142.         {  
  143.             //  
  144.             bool hasEventListener_boolean = false;  
  145.               
  146.             //  
  147.             object aIEventListener = _getArgumentsCallee(aEventDelegate);             
  148.               
  149.             //  OUTER  
  150.             string keyForOuterHashTable_string = _getKeyForOuterHashTable (aEventName_string);  
  151.             if (_eventListenerDatas_hashtable.ContainsKey(keyForOuterHashTable_string)) {  
  152.               
  153.                 //  INNER  
  154.                 Hashtable inner_hashtable = _eventListenerDatas_hashtable[keyForOuterHashTable_string] as Hashtable;  
  155.                 string keyForInnerHashTable_string = _getKeyForInnerHashTable (new EventListenerData (aIEventListener, aEventName_string, aEventDelegate, EventDispatcherAddMode.DEFAULT));  
  156.                 //  
  157.                 if (inner_hashtable.Contains(keyForInnerHashTable_string)) {  
  158.                     hasEventListener_boolean = true;  
  159.                 }  
  160.             }  
  161.        
  162.             return hasEventListener_boolean;  
  163.         }  
  164.           
  165.         /// <summary>  
  166.         /// Removes the event listener.  
  167.         /// </summary>  
  168.         /// <returns>  
  169.         /// The event listener.  
  170.         /// </returns>  
  171.         /// <param name='aIEventListener'>  
  172.         /// If set to <c>true</c> a I event listener.  
  173.         /// </param>  
  174.         /// <param name='aEventName_string'>  
  175.         /// If set to <c>true</c> a event name_string.  
  176.         /// </param>  
  177.         /// <param name='aEventDelegate'>  
  178.         /// If set to <c>true</c> a event delegate.  
  179.         /// </param>  
  180.         public bool removeEventListener(string aEventName_string, EventDelegate aEventDelegate)  
  181.         {  
  182.             //  
  183.             bool wasSuccessful_boolean = false;  
  184.               
  185.             //  
  186.             if (hasEventListener (aEventName_string, aEventDelegate)) {  
  187.                 //  OUTER  
  188.                 string keyForOuterHashTable_string = _getKeyForOuterHashTable (aEventName_string);  
  189.                 Hashtable inner_hashtable = _eventListenerDatas_hashtable[keyForOuterHashTable_string] as Hashtable;  
  190.                 //  
  191.                 object aIEventListener = _getArgumentsCallee(aEventDelegate);     
  192.                 //  INNER  
  193.                 string keyForInnerHashTable_string = _getKeyForInnerHashTable (new EventListenerData (aIEventListener, aEventName_string, aEventDelegate, EventDispatcherAddMode.DEFAULT));  
  194.                 inner_hashtable.Remove(keyForInnerHashTable_string);  
  195.                 wasSuccessful_boolean = true;  
  196.             }   
  197.               
  198.             return wasSuccessful_boolean;  
  199.               
  200.         }  
  201.           
  202.         /// <summary>  
  203.         /// Removes all event listeners.  
  204.         /// </summary>  
  205.         /// <returns>  
  206.         /// The all event listeners.  
  207.         /// </returns>  
  208.         public bool removeAllEventListeners()  
  209.         {  
  210.             //  
  211.             bool wasSuccessful_boolean = false;  
  212.               
  213.             //TODO, IS IT A MEMORY LEAK TO JUST RE-CREATE THE TABLE? ARE THE INNER HASHTABLES LEAKING?  
  214.             _eventListenerDatas_hashtable = new Hashtable();  
  215.               
  216.             return wasSuccessful_boolean;  
  217.         }  
  218.           
  219.        
  220.         /// <summary>  
  221.         /// Dispatchs the event.  
  222.         /// </summary>  
  223.         /// <returns>  
  224.         /// The event.  
  225.         /// </returns>  
  226.         /// <param name='aIEvent'>  
  227.         /// If set to <c>true</c> a I event.  
  228.         /// </param>  
  229.         public bool dispatchEvent(IEvent aIEvent)  
  230.         {  
  231.               
  232.             //  
  233.             bool wasSuccessful_boolean = false;  
  234.               
  235.             //  
  236.             _doAddTargetValueToIEvent (aIEvent);  
  237.               
  238.             //  OUTER  
  239.             string keyForOuterHashTable_string = _getKeyForOuterHashTable (aIEvent.type);  
  240.             int dispatchedCount_int = 0;  
  241.             if (_eventListenerDatas_hashtable.ContainsKey(keyForOuterHashTable_string)) {  
  242.        
  243.                 //  INNER  
  244.                 Hashtable inner_hashtable = _eventListenerDatas_hashtable[keyForOuterHashTable_string] as Hashtable;  
  245.                 IEnumerator innerHashTable_ienumerator = inner_hashtable.GetEnumerator();  
  246.                 DictionaryEntry dictionaryEntry;  
  247.                 EventListenerData eventListenerData;  
  248.                 ArrayList toBeRemoved_arraylist = new ArrayList ();  
  249.                 //  
  250.                 while (innerHashTable_ienumerator.MoveNext()) {  
  251.                       
  252.                     dictionaryEntry = (DictionaryEntry)innerHashTable_ienumerator.Current;  
  253.                     eventListenerData = dictionaryEntry.Value as EventListenerData;  
  254.                       
  255.                     //***DO THE DISPATCH***  
  256.                     //Debug.Log ("DISPATCH : ");  
  257.                     //Debug.Log ("  n    : " + eventListenerData.eventName );  
  258.                     //Debug.Log ("  from : " + aIEvent.target );  
  259.                     //Debug.Log ("  to   : " + eventListenerData.eventListener );  
  260.                     //Debug.Log ("  del  : " + eventListenerData.eventDelegate + " " + (eventListenerData.eventDelegate as System.Delegate).Method.DeclaringType.Name + " " + (eventListenerData.eventDelegate as System.Delegate).Method.Name.ToString());  
  261.                     eventListenerData.eventDelegate (aIEvent);  
  262.                       
  263.                       
  264.                     //TODO - THIS IS PROBABLY FUNCTIONAL BUT NOT OPTIMIZED, MY APPROACH TO HOW/WHY SINGLE SHOTS ARE REMOVED  
  265.                     //REMOVE IF ONESHOT  
  266.                     if (eventListenerData.eventListeningMode == EventDispatcherAddMode.SINGLE_SHOT) {  
  267.                           
  268.                         toBeRemoved_arraylist.Add (eventListenerData);  
  269.                     }  
  270.                       
  271.                       
  272.                     //MARK SUCCESS, BUT ALSO CONTINUE LOOPING TOO  
  273.                     wasSuccessful_boolean = true;  
  274.                     dispatchedCount_int++;  
  275.                 }  
  276.                   
  277.                   
  278.                 //CLEANUP ANY ONE-SHOT, SINGLE-USE   
  279.                 EventListenerData toBeRemoved_eventlistenerdata;  
  280.                 for (int count_int = toBeRemoved_arraylist.Count -1; count_int >= 0; count_int --) {  
  281.                     toBeRemoved_eventlistenerdata = toBeRemoved_arraylist[count_int] as EventListenerData;  
  282.                     removeEventListener (toBeRemoved_eventlistenerdata.eventName, toBeRemoved_eventlistenerdata.eventDelegate);  
  283.                 }  
  284.        
  285.                   
  286.             }  
  287.               
  288.               
  289.             return wasSuccessful_boolean;  
  290.         }  
  291.           
  292.         /// <summary>  
  293.         /// _dos the add target value to I event.  
  294.         /// </summary>  
  295.         /// <param name='aIEvent'>  
  296.         /// A I event.  
  297.         /// </param>  
  298.         /// <exception cref='System.NotImplementedException'>  
  299.         /// Is thrown when a requested operation is not implemented for a given type.  
  300.         /// </exception>  
  301.         public void _doAddTargetValueToIEvent (IEvent aIEvent)  
  302.         {  
  303.             aIEvent.target = _target_object;  
  304.         }  
  305.   
  306.         public void OnApplicationQuit()  
  307.         {  
  308.             //TODO, DO THIS CLEANUP HERE, OR OBLIGATE API-USER TO DO IT??  
  309.             _eventListenerDatas_hashtable.Clear();  
  310.         }  
  311.           
  312.           
  313.         private string _getKeyForOuterHashTable (string aEventName_string)  
  314.         {  
  315.             //SIMPLY USING THE EVENT NAME - METHOD USED HERE, IN CASE I WANT TO TWEAK THIS MORE...  
  316.             return aEventName_string;  
  317.         }  
  318.           
  319.         private string _getKeyForInnerHashTable (EventListenerData aEventListenerData)  
  320.         {  
  321.             //VERY UNIQUE - NICE!  
  322.             return aEventListenerData.eventListener.GetType().FullName + "_" + aEventListenerData.eventListener.GetType().GUID + "_" + aEventListenerData.eventName + "_" + (aEventListenerData.eventDelegate as System.Delegate).Method.Name.ToString();  
  323.           
  324.         }  
  325.   
  326.         public object _getArgumentsCallee (EventDelegate aEventDelegate)  
  327.         {  
  328.             return (aEventDelegate as System.Delegate).Target;  
  329.         }  
  330.     }  
  331. }  
[csharp]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. namespace EventDispatcher  
  2. {  
  3.     /// <summary>  
  4.     /// EventListenerData  
  5.     /// </summary>  
  6.     public class EventListenerData  
  7.     {  
  8.         /// <summary>  
  9.         /// The _event listener.  
  10.         /// </summary>  
  11.         private object _eventListener;  
  12.         public object eventListener   
  13.         {   
  14.             get  
  15.             {  
  16.                 return _eventListener;  
  17.             }  
  18.             set  
  19.             {  
  20.                 _eventListener = value;  
  21.                   
  22.             }  
  23.         }  
  24.           
  25.         /// <summary>  
  26.         /// 事件名  
  27.         /// </summary>  
  28.         private string _eventName_string;  
  29.         public string eventName   
  30.         {   
  31.             get  
  32.             {  
  33.                 return _eventName_string;  
  34.             }  
  35.             set  
  36.             {  
  37.                 _eventName_string = value;  
  38.                   
  39.             }  
  40.         }  
  41.           
  42.           
  43.           
  44.         /// <summary>  
  45.         /// 事件委托  
  46.         /// </summary>  
  47.         private EventDelegate _eventDelegate;  
  48.         public EventDelegate eventDelegate   
  49.         {   
  50.             get  
  51.             {  
  52.                 return _eventDelegate;  
  53.             }  
  54.             set  
  55.             {  
  56.                 _eventDelegate = value;  
  57.                   
  58.             }  
  59.         }  
  60.           
  61.         private EventDispatcherAddMode _eventListeningMode;  
  62.         public EventDispatcherAddMode eventListeningMode   
  63.         {   
  64.             get  
  65.             {  
  66.                 return _eventListeningMode;  
  67.             }  
  68.             set  
  69.             {  
  70.                 _eventListeningMode = value;  
  71.                   
  72.             }  
  73.         }  
  74.   
  75.         ///<summary>  
  76.         ///  Constructor  
  77.         ///</summary>  
  78.         public EventListenerData (object aEventListener, string aEventName_string, EventDelegate aEventDelegate, EventDispatcherAddMode aEventListeningMode )  
  79.         {  
  80.             _eventListener      = aEventListener;  
  81.             _eventName_string   = aEventName_string;  
  82.             _eventDelegate      = aEventDelegate;  
  83.             _eventListeningMode = aEventListeningMode;  
  84.         }  
  85.           
  86.         /// <summary>  
  87.         /// Deconstructor  
  88.         /// </summary>  
  89.         //~EventListenerData ( )  
  90.         //{  
  91.             //Debug.Log ("EventListenerData.deconstructor()");  
  92.               
  93.         //}  
  94.     }  
  95. }  


使用的例子:新建两个游戏对象然后挂上脚本!

[csharp]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. namespace EventDispatcher  
  2. {  
  3.     /// <summary>  
  4.     /// Test event.  
  5.     /// </summary>  
  6.     public class SampleEvent : Event  
  7.     {  
  8.         // GETTER / SETTER  
  9.         /// <summary>  
  10.         /// An example of event-specific data you can add in.  
  11.         /// </summary>  
  12.         private string _customValue_string;  
  13.         public string customValue   
  14.         {  
  15.             get {  
  16.                 return _customValue_string;  
  17.             }  
  18.             set {  
  19.                 _customValue_string = value;  
  20.             }  
  21.         }  
  22.   
  23.         /// <summary>  
  24.         /// The Event Type Name  
  25.         /// </summary>  
  26.         public static string SAMPLE_EVENT = "SAMPLE_EVENT";  
  27.           
  28.         /// <summary>  
  29.         /// Initializes a new instance of the <see cref="com.rmc.projects.event_dispatcher.SampleEvent"/> class.  
  30.         /// </summary>  
  31.         /// <param name="aType_str">A type_str.</param>  
  32.         public SampleEvent (string aType_str ) : base (aType_str)  
  33.         {  
  34.               
  35.         }  
  36.           
  37.         /// <summary>  
  38.         /// Releases unmanaged resources and performs other cleanup operations before the  
  39.         /// <see cref="com.rmc.projects.event_dispatcher.SampleEvent"/> is reclaimed by garbage collection.  
  40.         /// </summary>  
  41.         //~SampleEvent ( )  
  42.         //{  
  43.         //  Debug.Log ("SampleEvent.deconstructor()");  
  44.           
  45.         //}  
  46.   
  47.     }  
  48. }  
[csharp]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. using UnityEngine;  
  2.   
  3. namespace EventDispatcher  
  4. {  
  5.     public class SampleObservedComponent : MonoBehaviour   
  6.     {  
  7.         /// <summary>  
  8.         /// The event dispatcher.  
  9.         /// </summary>  
  10.         public EventDispatcher eventDispatcher;  
  11.   
  12.         /// <summary>  
  13.         /// Initializes a new instance of the <see cref="com.rmc.projects.event_dispatcher.SampleObservedComponent"/> class.  
  14.         /// </summary>  
  15.         public SampleObservedComponent ()  
  16.         {  
  17.               
  18.             eventDispatcher = new EventDispatcher (this);  
  19.         }  
  20.   
  21.         ///<summary>  
  22.         /// Use this for initialization  
  23.         ///</summary>  
  24.         public void Start ()   
  25.         {  
  26.             SampleEvent sampleEvent = new SampleEvent (SampleEvent.SAMPLE_EVENT);  
  27.             sampleEvent.customValue = "foo";  
  28.             Debug.Log ("Dispatching: SampleEvent " + sampleEvent);  
  29.             eventDispatcher.dispatchEvent (sampleEvent);  
  30.         }  
  31.   
  32.         /// <summary>  
  33.         /// Raises the destroy event.  
  34.         /// </summary>  
  35.         public void OnDestroy ()  
  36.         {  
  37.             //  CLEANUP MEMORY  
  38.             eventDispatcher.removeAllEventListeners();  
  39.             eventDispatcher = null;  
  40.         }  
  41.     }  
  42. }  
[csharp]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. using UnityEngine;  
  2.   
  3. namespace EventDispatcher  
  4. {  
  5.     public class SampleObserverComponent : MonoBehaviour   
  6.     {  
  7.         /// <summary>  
  8.         /// The sample observed game object.  
  9.         /// </summary>  
  10.         public SampleObservedComponent sampleObservedGameObject;  
  11.   
  12.         public void Start ()   
  13.         {  
  14.             /* 
  15.              * 注意: 这里observer和observed是 MonoBehavior 子类 
  16.              **/  
  17.             sampleObservedGameObject.eventDispatcher.addEventListener (SampleEvent.SAMPLE_EVENT, _onSampleEvent);  
  18.               
  19.         }  
  20.   
  21.         void Update ()   
  22.         {  
  23.             //Debug.Log (sampleObservedGameObject.eventDispatcher);  
  24.         }  
  25.   
  26.         /// <summary>  
  27.         /// Raises the destroy event.  
  28.         /// </summary>  
  29.         public void OnDestroy ()  
  30.         {  
  31.             //  CLEANUP MEMORY  
  32.             sampleObservedGameObject.eventDispatcher.removeEventListener (SampleEvent.SAMPLE_EVENT, _onSampleEvent);  
  33.               
  34.         }  
  35.         //--------------------------------------  
  36.         //  Events   
  37.         //--------------------------------------  
  38.         /// <summary>  
  39.         /// _ons the sample event.  
  40.         /// </summary>  
  41.         /// <param name="aIEvent">A I event.</param>  
  42.         public void _onSampleEvent (IEvent aIEvent)  
  43.         {  
  44.             Debug.Log ("\tListening: _onSampleEvent() aIEvent: " + aIEvent + " with customValue: " + (aIEvent as SampleEvent).customValue);  
  45.         }  
  46.     }  
  47. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值