各类委托:用于添加不同类型方法
public delegate void CallBack();
public delegate void CallBack(T arg);
public delegate void CallBack<T, X>(T arg1, X arg2);
public delegate void CallBack<T, X, Y>(T arg1, X arg2, Y arg3);
public delegate void CallBack<T, X, Y, Z>(T arg1, X arg2, Y arg3, Z arg4);
public delegate void CallBack<T, X, Y, Z, W>(T arg1, X arg2, Y arg3, Z arg4, W arg5);
事件类型枚举:
public enum EventDefine
{
ShowGamePanel,
DecidePath,
AddScore,
UpdateScoreText,
PlayerMove,
AddDiamond,
UpdateDiamondText,
ShowGameOverPale,
ShowShopPanle,
ShowMainPanle,
ChageSkin,
ShowResetPanel,
ShowRankPanel,
PlayBuyyonSound,
}
事件中心类:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EventCenter
{
//存储的 事件表(事件类型,对应方法类型的委托链)
private static Dictionary<EventDefine, Delegate> m_EventTable = new Dictionary<EventDefine, Delegate>();

//在委托链上添加广播该事件时要调用的方法
private static void OnListenerAdding(EventDefine eventType, Delegate callBack)
{
if (!m_EventTable.ContainsKey(eventType))
{
m_EventTable.Add(eventType, null);
}
Delegate d = m_EventTable[eventType];
if (d != null && d.GetType() != callBack.GetType())
{
throw new Exception(string.Format("尝试为事件{0}添加不同类型的委托,当前事件所对应的委托是{1},要添加的委托类型为{2}", eventType, d.GetType(), callBack.GetType()));
}
}
//把监听该事件时所调用的方法从委托链上移除
private static void OnListenerRemoving(EventDefine eventType, Delegate callBack)
{
if (m_EventTable.ContainsKey(eventType))
{
Delegate d = m_EventTable[eventType];
if (d == null)
{
throw new Exception(string.Format("移除监听错误:事件{0}没有对应的委托", eventType));
}
else if (d.GetType() != callBack.GetType())
{
throw new Exception(string.Format("移除监听错误:尝试为事件{0}移除不同类型的委托,当前委托类型为{1},要移除的委托类型为{2}", eventType, d.GetType(), callBack.GetType()));
}
}
else
{
throw new Exception(string.Format("移除监听错误:没有事件码{0}", eventType));
}
}
//把监听该事件时所调用的方法从委托链上移除
private static void OnListenerRemoved(EventDefine eventType)
{
if (m_EventTable[eventType] == null)
{
m_EventTable.Remove(eventType);
}
}
//no parameters
public static void AddListener(EventDefine eventType, CallBack callBack)
{
OnListenerAdding(eventType, callBack);
m_EventTable[eventType] = (CallBack)m_EventTable[eventType] + callBack;
}
//Single parameters
public static void AddListener(EventDefine eventType, CallBack callBack)
{
OnListenerAdding(eventType, callBack);
m_EventTable[eventType] = (CallBack)m_EventTable[eventType] + callBack;
}
//two parameters
public static void AddListener<T, X>(EventDefine eventType, CallBack<T, X> callBack)
{
OnListenerAdding(eventType, callBack);
m_EventTable[eventType] = (CallBack<T, X>)m_EventTable[eventType] + callBack;
}

//no parameters
public static void RemoveListener(EventDefine eventType, CallBack callBack)
{
    OnListenerRemoving(eventType, callBack);
    m_EventTable[eventType] = (CallBack)m_EventTable[eventType] - callBack;
    OnListenerRemoved(eventType);
}
//single parameters
public static void RemoveListener<T>(EventDefine eventType, CallBack<T> callBack)
{
    OnListenerRemoving(eventType, callBack);
    m_EventTable[eventType] = (CallBack<T>)m_EventTable[eventType] - callBack;
    OnListenerRemoved(eventType);
}
//two parameters
public static void RemoveListener<T, X>(EventDefine eventType, CallBack<T, X> callBack)
{
    OnListenerRemoving(eventType, callBack);
    m_EventTable[eventType] = (CallBack<T, X>)m_EventTable[eventType] - callBack;
    OnListenerRemoved(eventType);
}

}
//no parameters
public static void Broadcast(EventDefine eventType)
{
Delegate d;
if (m_EventTable.TryGetValue(eventType, out d))
{
CallBack callBack = d as CallBack;
if (callBack != null)
{
callBack();
}
else
{
throw new Exception(string.Format("广播事件错误:事件{0}对应委托具有不同的类型", eventType));
}
}
}
//single parameters
public static void Broadcast(EventDefine eventType, T arg)
{
Delegate d;
if (m_EventTable.TryGetValue(eventType, out d))
{
CallBack callBack = d as CallBack;
if (callBack != null)
{
callBack(arg);
}
else
{
throw new Exception(string.Format("广播事件错误:事件{0}对应委托具有不同的类型", eventType));
}
}
}


UI脚本中代码:
void Start()
{
EventCenter.AddListener(EventDefine.ShowShopPanle, Show);
}
private void OnDestroy()
{
EventCenter.RemoveListener (EventDefine.ShowShopPanle, Show);
}

private void Show()
{
    gameObject.SetActive(true);
}