/// <summary>
/// 委托字典
/// </summary>
public class DicDelegate
{
//定义委托
public delegate void Callback ( );
public delegate void Callback<T> ( T arg1 );
public delegate void Callback<T , U> ( T arg1 , U arg2 );
public delegate void Callback<T , U , V> ( T arg1 , U arg2 , V arg3 );
public delegate void Callback<T , U , V , X> ( T arg1 , U arg2 , V arg3 , X arg4 );
//定义字典,key是事件类型,value是个委托类型
public static Dictionary<GameEvent , Delegate > mEventTable = new Dictionary< GameEvent , Delegate> ( );
//╓=======================添加委托====↓↓↓↓=====================================╖
/// <summary>
/// 添加这个类型的键,检查委托类型,
/// </summary>
/// <param name="eventType"></param>
/// <param name="listenerBeingAdded"></param>
static void AddKeyCheackValue ( GameEvent eventType , Delegate handler )
{
//类型添加到字典, 字典没有这个类型的键,就在添加
if ( !mEventTable.ContainsKey ( eventType ) )
{
mEventTable.Add ( eventType , null );
}
//检查委托类型,要添加委托和字典里面的委托类型是否一致 ,如果字典里这个类型已经有委托了,并且类型不一样,就抛出异常
Delegate d = mEventTable [ eventType ];
if ( d!= null && d.GetType ( ) != handler.GetType ( ) )
{
Debug.LogError ( "this Error ." );
}
}
static public void AddListener ( GameEvent eventType , Callback handler )
{
//添加这个类型的键,检查委托类型
AddKeyCheackValue ( eventType , handler );
//添加委托
mEventTable [ eventType ] = ( Callback ) mEventTable [ eventType ] + handler;
}
static public void AddListener<T> ( GameEvent eventType , Callback <T> handler )
{
AddKeyCheackValue ( eventType , handler );
mEventTable [ eventType ] = ( Callback<T> ) mEventTable [ eventType ] + handler;
}
static public void AddListener<T , U> ( GameEvent eventType , Callback <T , U> handler )
{
AddKeyCheackValue ( eventType , handler );
mEventTable [ eventType ] = ( Callback<T , U> ) mEventTable [ eventType ] + handler;
}
static public void AddListener<T , U , V> ( GameEvent eventType , Callback <T , U , V> handler )
{
AddKeyCheackValue ( eventType , handler );
mEventTable [ eventType ] = ( Callback<T , U , V> ) mEventTable [ eventType ] + handler;
}
static public void AddListener<T , U , V , X> ( GameEvent eventType , Callback <T , U , V , X> handler )
{
AddKeyCheackValue ( eventType , handler );
mEventTable [ eventType ] = ( Callback<T , U , V , X> ) mEventTable [ eventType ] + handler;
}
//╘=======================添加委托=↑↑↑↑========================================╛
//╓=======================注销委托====↓↓↓↓=====================================╖
/// <summary>
/// 检查要将要移除的键和值的类型
/// </summary>
/// <param name="eventType"></param>
/// <param name="handler"></param>
static void CheakKeyValue ( GameEvent eventType , Delegate handler )
{
if ( mEventTable.ContainsKey ( eventType ) ) //字典有这个类型的键
{
Delegate d = mEventTable [ eventType ];
if ( d!= null ) //这个键没有值
{
Debug.LogError ( "this Error ." );
}
else if ( d.GetType ( ) != handler.GetType ( ) ) //这个键的值类型不对
{
Debug.LogError ( "this Error ." );
}
}
else//字典没有这个类型的键
{
Debug.LogError ( "this Error ." );
}
}
static public void RemoveListener ( GameEvent eventType , Callback handler )
{
CheakKeyValue ( eventType , handler );
mEventTable [ eventType ] = ( Callback ) mEventTable [ eventType ] - handler; //从委托上解除这个函数
if ( mEventTable [ eventType ] == null ) //如果委托上所有绑定的函数没有了,就从字典移除这个键
{
mEventTable.Remove ( eventType );
}
}
static public void RemoveListener<T> ( GameEvent eventType , Callback <T> handler )
{
CheakKeyValue ( eventType , handler );
mEventTable [ eventType ] = ( Callback<T> ) mEventTable [ eventType ] - handler;
if ( mEventTable [ eventType ] == null ) //如果委托上所有绑定的函数没有了,就从字典移除这个键
{
mEventTable.Remove ( eventType );
}
}
static public void RemoveListener<T , U> ( GameEvent eventType , Callback <T , U> handler )
{
CheakKeyValue ( eventType , handler );
mEventTable [ eventType ] = ( Callback<T , U> ) mEventTable [ eventType ] - handler;
if ( mEventTable [ eventType ] == null ) //如果委托上所有绑定的函数没有了,就从字典移除这个键
{
mEventTable.Remove ( eventType );
}
}
static public void RemoveListener<T , U , V> ( GameEvent eventType , Callback <T , U , V> handler )
{
CheakKeyValue ( eventType , handler );
mEventTable [ eventType ] = ( Callback<T , U , V> ) mEventTable [ eventType ] - handler;
if ( mEventTable [ eventType ] == null ) //如果委托上所有绑定的函数没有了,就从字典移除这个键
{
mEventTable.Remove ( eventType );
}
}
static public void RemoveListener<T , U , V , X> ( GameEvent eventType , Callback <T , U , V , X> handler )
{
CheakKeyValue ( eventType , handler );
mEventTable [ eventType ] = ( Callback<T , U , V , X> ) mEventTable [ eventType ] - handler;
if ( mEventTable [ eventType ] == null ) //如果委托上所有绑定的函数没有了,就从字典移除这个键
{
mEventTable.Remove ( eventType );
}
}
//╘=======================注销委托=↑↑↑↑========================================╛
//╓=======================委托调用====↓↓↓↓=====================================╖
//No parameters
static public void Use ( GameEvent eventType )
{
Delegate d;
if ( mEventTable.TryGetValue ( eventType , out d ) ) //从字典取到这个键的值
{
Callback callback = d as Callback ;
if ( callback != null ) //该委托有绑定函数就执行
{
callback ( );
}
else
{
Debug.LogError ( eventType );
}
}
}
static public void Use<T> ( GameEvent eventType , T arg1 )
{
Delegate d;
if ( mEventTable.TryGetValue ( eventType , out d ) )
{
Callback<T> callback = d as Callback <T>;
if ( callback != null )
{
callback ( arg1 );
}
else
{
Debug.LogError ( eventType );
}
}
}
//Two parameters
static public void Use<T , U> ( GameEvent eventType , T arg1 , U arg2 )
{
Delegate d;
if ( mEventTable.TryGetValue ( eventType , out d ) )
{
Callback<T, U> callback = d as Callback <T , U>;
if ( callback != null )
{
callback ( arg1 , arg2 );
}
else
{
Debug.LogError ( eventType );
}
}
}
//Three parameters
static public void Use<T , U , V> ( GameEvent eventType , T arg1 , U arg2 , V arg3 )
{
Delegate d;
if ( mEventTable.TryGetValue ( eventType , out d ) )
{
Callback<T, U, V> callback = d as Callback <T , U , V>;
if ( callback != null )
{
callback ( arg1 , arg2 , arg3 );
}
else
{
Debug.LogError ( eventType );
}
}
}
//Four parameters
static public void Use<T , U , V , X> ( GameEvent eventType , T arg1 , U arg2 , V arg3 , X arg4 )
{
Delegate d;
if ( mEventTable.TryGetValue ( eventType , out d ) )
{
Callback<T, U, V, X> callback = d as Callback <T , U , V , X>;
if ( callback != null )
{
callback ( arg1 , arg2 , arg3 , arg4 );
}
else
{
Debug.LogError ( eventType );
}
}
}
//╘=======================委托调用=↑↑↑↑========================================╛