public enum EUIMgrEvent
{
onUILogInOpend,
onUILogInClose,
onUIHintOpend,
onUIHintClose,
onUIWorkOpend,
onUIWorkClose,
onUIShopOpend,
onUIShopClose
}
先创建几个事件类型,然后对事件类型进行添加,触发,删除等操作..........
1.创建字典保存事件
Dictionary<EUIMgrEvent, Action<object, object[]>> _phList = new Dictionary<EUIMgrEvent, Action<object, object[]>>();
2.添加事件
public void addEventHandler(EUIMgrEvent eventID,Action<object, object[]> handler)
{
if (_phList == null) return;
if (_phList.ContainsKey(eventID))
{
Debug.LogError(string.Format("error: event[ {0} ] already exists", eventID));
return;
}
_phList[eventID] = handler;
}
3.删除事件
public void delEventHandler(EUIMgrEvent eventID)
{
if (_phList == null) return;
if (!_phList.ContainsKey(eventID))
{
Debug.LogError(string.Format("error: event[ {0} ] not exists", eventID));
return;
}
_phList.Remove(eventID);
}
4.触发事件
public void fireEvent(EUIMgrEvent eventID,object sender ,object[] args)
{
if (_phList == null) return;
if (!_phList.ContainsKey(eventID)){
return;
}
var action = _phList[eventID];
if (action == null) return;
action(sender, args);
}
5.重置事件系统
public void Reset()
{
if (_phList == null) return;
_phList.Clear();
_phList = null;
}