123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- using System;
- public class UISystemEvent
- {
- public static Dictionary<UIEvent, UICallBack> s_allUIEvents = new Dictionary<UIEvent, UICallBack>();
- public static Dictionary<string, Dictionary<UIEvent, UICallBack>> s_singleUIEvents = new Dictionary<string, Dictionary<UIEvent, UICallBack>>();
- /// <summary>
- /// 每个UI都会派发的事件
- /// </summary>
- /// <param name="Event">事件类型</param>
- /// <param name="callback">回调函数</param>
- public static void RegisterAllUIEvent(UIEvent UIEvent, UICallBack CallBack)
- {
- if (s_allUIEvents.ContainsKey(UIEvent))
- {
- s_allUIEvents[UIEvent] += CallBack;
- }
- else
- {
- s_allUIEvents.Add(UIEvent,CallBack);
- }
- }
- public static void RemoveAllUIEvent(UIEvent UIEvent, UICallBack l_CallBack)
- {
- if (s_allUIEvents.ContainsKey(UIEvent))
- {
- s_allUIEvents[UIEvent] -= l_CallBack;
- }
- else
- {
- Debug.LogError("RemoveAllUIEvent don't exits: " + UIEvent);
- }
- }
- /// <summary>
- /// 注册单个UI派发的事件
- /// </summary>
- /// <param name="Event">事件类型</param>
- /// <param name="callback"回调函数></param>
- public static void RegisterEvent(string UIName,UIEvent UIEvent, UICallBack CallBack)
- {
- if (s_singleUIEvents.ContainsKey(UIName))
- {
- if (s_singleUIEvents[UIName].ContainsKey(UIEvent))
- {
- s_singleUIEvents[UIName][UIEvent] += CallBack;
- }
- else
- {
- s_singleUIEvents[UIName].Add(UIEvent,CallBack);
- }
- }
- else
- {
- s_singleUIEvents.Add(UIName,new Dictionary<UIEvent,UICallBack>());
- s_singleUIEvents[UIName].Add(UIEvent, CallBack);
- }
- }
- public static void RemoveEvent(string UIName, UIEvent UIEvent, UICallBack CallBack)
- {
- if (s_singleUIEvents.ContainsKey(UIName))
- {
- if (s_singleUIEvents[UIName].ContainsKey(UIEvent))
- {
- s_singleUIEvents[UIName][UIEvent] -= CallBack;
- }
- else
- {
- Debug.LogError("RemoveEvent 不存在的事件! UIName " + UIName + " UIEvent " + UIEvent);
- }
- }
- else
- {
- Debug.LogError("RemoveEvent 不存在的事件! UIName " + UIName + " UIEvent " + UIEvent);
- }
- }
- public static void Dispatch(UIWindowBase UI, UIEvent UIEvent,params object[] objs)
- {
- if (UI == null)
- {
- Debug.LogError("Dispatch l_UI is null!");
- return;
- }
- if (s_allUIEvents.ContainsKey(UIEvent))
- {
- try
- {
- if(s_allUIEvents[UIEvent] != null)
- s_allUIEvents[UIEvent](UI, objs);
- }
- catch (Exception e)
- {
- Debug.LogError("UISystemEvent Dispatch error:" + e.ToString());
- }
- }
- if (s_singleUIEvents.ContainsKey(UI.name))
- {
- if (s_singleUIEvents[UI.name].ContainsKey(UIEvent))
- {
- try
- {
- if (s_singleUIEvents[UI.name][UIEvent] != null)
- s_singleUIEvents[UI.name][UIEvent](UI, objs);
- }
- catch (Exception e)
- {
- Debug.LogError("UISystemEvent Dispatch error:" + e.ToString());
- }
- }
- }
- }
- }
|