出处:http://blog.csdn.net/u010019717
author:孙广东 时间:2015.3.21 23:00
不使用C#中的event关键字: 只是使用delegate和hashtable 进行事件的分发。
基本库如下:
namespace EventDispatcher
{
/// <summary>
/// IEvent接口
/// </summary>
public interface IEvent
{
/// <summary>
/// Gets or sets the type.
/// </summary>
/// <value>
/// type类型.
/// </value>
string type { get; set;}
/// <summary>
/// Gets or sets the target.
/// </summary>
/// <value>
/// target目标.
/// </value>
object target { get; set; }
}
}
namespace EventDispatcher
{
/// <summary>
/// 事件事件接口
/// </summary>
public class Event : IEvent
{
// GETTER / SETTER
/// <summary>
/// The _type_string.
/// </summary>
private string _type_string;
string IEvent.type {
get
{
return _type_string;
}
set
{
_type_string = value;
}
}
/// <summary>
/// The _target_object.
/// </summary>
private object _target_object;
object IEvent.target {
get
{
return _target_object;
}
set
{
_target_object = value;
}
}
///<summary>
/// Constructor
///</summary>
public Event (string aType_str )
{
//
_type_string = aType_str;
}
/// <summary>
/// Deconstructor
/// </summary>
//~Event ( )
//{
// Debug.Log ("Event.deconstructor()");
//}
}
}
namespace EventDispatcher
{
/// <summary>
/// IEventDispatcher 事件分发接口,添加移除分发
/// </summary>
public interface IEventDispatcher
{
/// <summary>
/// 添加事件监听
/// </summary>
/// <returns><c>true</c>, 如果事件成功被监听, <c>false</c> 失败 </returns>
/// <param name="aEventType_string">A event type_string.</param>
/// <param name="aEventDelegate">一个事件委托</param>
bool addEventListener(string aEventType_string, EventDelegate aEventDelegate);
/// <summary>
/// 添加事件监听
/// </summary>
/// <returns><c>true</c>, 如果事件成功被监听, <c>false</c> 失败 </returns>
/// <param name="aEventType_string">A event type_string.</param>
/// <param name="aEventDelegate">一个事件委托</param>
/// <param name="eventDispatcherAddMode">Event dispatcher add mode.</param>
bool addEventListener(string aEventType_string, EventDelegate aEventDelegate, EventDispatcherAddMode eventDispatcherAddMode);
/// <summary>
/// 是否有这个事件监听
/// </summary>
/// <returns><c>true</c>, 这个监听已经有了, <c>false</c> 没有.</returns>
/// <param name="aEventType_string">A event type_string.</param>
/// <param name="aEventDelegate">一个事件委托</param>
bool hasEventListener(string aEventType_string, EventDelegate aEventDelegate);
/// <summary>
/// 移除事件监听
/// </summary>
/// <returns><c>true</c>, 被成功移除 <c>false</c> 失败</returns>
/// <param name="aEventType_string">A event type_string.</param>
/// <param name="aEventDelegate">一个事件委托</param>
bool removeEventListener(string aEventType_string, EventDelegate aEventDelegate);
/// <summary>
/// 移除所有事件监听
/// </summary>
/// <returns><c>true</c>, 成功, <c>false</c> 失败 </returns>
bool removeAllEventListeners();
/// <summary>
/// 分发广播事件
/// </summary>
/// <returns><c>true</c>, 成功, <c>false</c> 失败</returns>
/// <param name="aIEvent">A I event.</param>
bool dispatchEvent(IEvent aIEvent);
}
}
// C# unity事件管理器 在哈希表中