Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

NGUIEventBroadcaster

By: a guest on Mar 12th, 2014  |  syntax: C#  |  size: 4.49 KB  |  views: 96  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. using UnityEngine;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5.  
  6.  
  7. public class NGUIEventBroadcaster : MonoBehaviour
  8. {
  9.  
  10.     #region Broadcaster Registration
  11.  
  12.     static Dictionary<string, NGUIEventBroadcaster> Broadcasters;
  13.  
  14.     static void RegisterBroadcaster(NGUIEventBroadcaster broadcaster)
  15.     {
  16.         if (Broadcasters == null)
  17.             Broadcasters = new Dictionary<string, NGUIEventBroadcaster>();
  18.  
  19.         if (Broadcasters.ContainsKey(broadcaster.broadcasterID))
  20.             Debug.LogError("Duplicate Broadcaster ID detected: " + broadcaster.broadcasterID);
  21.         else
  22.             Broadcasters.Add(broadcaster.broadcasterID, broadcaster);
  23.     }
  24.  
  25.     static void DeregisterBroadcaster(NGUIEventBroadcaster broadcaster)
  26.     {
  27.         if (Broadcasters == null) return;
  28.  
  29.         if (Broadcasters.ContainsKey(broadcaster.broadcasterID))
  30.             Broadcasters.Remove(broadcaster.broadcasterID);
  31.     }
  32.  
  33.     public static NGUIEventBroadcaster BroadcasterFromID(string id)
  34.     {
  35.         if (Broadcasters != null && Broadcasters.ContainsKey(id))
  36.             return Broadcasters[id];
  37.         else
  38.             return null;
  39.     }
  40.  
  41.     #endregion
  42.  
  43.  
  44.  
  45.     #region Event Registration
  46.  
  47.     static Dictionary<string, List<Action>> EventListeners;
  48.  
  49.     public static void AddListener(string broadcasterID, Action eventDelegate)
  50.     {
  51.         if (broadcasterID == null || broadcasterID.Length == 0 || eventDelegate == null) return;
  52.  
  53.         if (EventListeners == null)
  54.             EventListeners = new Dictionary<string, List<Action>>();
  55.  
  56.         if (EventListeners.ContainsKey(broadcasterID) == false)
  57.             EventListeners.Add(broadcasterID, new List<Action>());
  58.  
  59.         if (EventListeners[broadcasterID].Contains(eventDelegate) == false)
  60.             EventListeners[broadcasterID].Add(eventDelegate);
  61.     }
  62.  
  63.     public static void RemoveListener(string broadcasterID, Action eventDelegate)
  64.     {
  65.         if (broadcasterID == null || broadcasterID.Length == 0 || eventDelegate == null) return;
  66.         if (EventListeners == null) return;
  67.         if (EventListeners.ContainsKey(broadcasterID) == false) return;
  68.         if (EventListeners[broadcasterID].Contains(eventDelegate))
  69.             EventListeners[broadcasterID].Remove(eventDelegate);
  70.     }
  71.  
  72.  
  73.     static Dictionary<string, List<Action<string>>> EventWithIDListeners;
  74.  
  75.     public static void AddListener(string broadcasterID, Action<string> eventDelegate)
  76.     {
  77.         if (broadcasterID == null || broadcasterID.Length == 0 || eventDelegate == null) return;
  78.  
  79.         if (EventWithIDListeners == null)
  80.             EventWithIDListeners = new Dictionary<string, List<Action<string>>>();
  81.  
  82.         if (EventWithIDListeners.ContainsKey(broadcasterID) == false)
  83.             EventWithIDListeners.Add(broadcasterID, new List<Action<string>>());
  84.  
  85.         if (EventWithIDListeners[broadcasterID].Contains(eventDelegate) == false)
  86.             EventWithIDListeners[broadcasterID].Add(eventDelegate);
  87.     }
  88.  
  89.     public static void RemoveListener(string broadcasterID, Action<string> eventDelegate)
  90.     {
  91.         if (broadcasterID == null || broadcasterID.Length == 0 || eventDelegate == null) return;
  92.         if (EventWithIDListeners == null) return;
  93.         if (EventWithIDListeners.ContainsKey(broadcasterID) == false) return;
  94.         if (EventWithIDListeners[broadcasterID].Contains(eventDelegate))
  95.             EventWithIDListeners[broadcasterID].Remove(eventDelegate);
  96.     }
  97.  
  98.     #endregion
  99.  
  100.  
  101.  
  102.     #region Instance
  103.  
  104.     public string broadcasterID = "";
  105.  
  106.     void OnEnable()
  107.     {
  108.         RegisterBroadcaster(this);
  109.     }
  110.  
  111.     void OnDisable()
  112.     {
  113.         DeregisterBroadcaster(this);
  114.     }
  115.  
  116.     void OnClick()
  117.     {
  118.         if (broadcasterID.Length == 0) return;
  119.  
  120.         if (EventListeners != null && EventListeners.ContainsKey(broadcasterID))
  121.         {
  122.             for (int i = 0; i < EventListeners[broadcasterID].Count; i++)
  123.             {
  124.                 if (EventListeners[broadcasterID][i] != null)
  125.                     EventListeners[broadcasterID][i]();
  126.             }
  127.         }
  128.  
  129.         if (EventWithIDListeners != null && EventWithIDListeners.ContainsKey(broadcasterID))
  130.         {
  131.             for (int i = 0; i < EventWithIDListeners[broadcasterID].Count; i++)
  132.             {
  133.                 if (EventWithIDListeners[broadcasterID][i] != null)
  134.                     EventWithIDListeners[broadcasterID][i](broadcasterID);
  135.             }
  136.         }
  137.  
  138.     }
  139.  
  140.     #endregion
  141. }