GenericNotificationCenter.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using UnityEngine;
  2. using System.Collections;
  3. using System;
  4. //Based off http://forum.unity3d.com/threads/37896-Programming-architectures?p=847646&viewfull=1#post847646
  5. //Changed it to static methods (less typing and less indirection)
  6. //Added helper methods for posting notifications
  7. public class GenericNotificationCenter<NT, N>
  8. where NT : struct, IConvertible, IComparable, IFormattable
  9. where N : GenericNotification<NT>, new()
  10. {
  11. public delegate void OnNotificationDelegate(N note);
  12. private static OnNotificationDelegate[] _listeners = new OnNotificationDelegate[System.Enum.GetValues(typeof(NT)).Length];
  13. public static void AddListener(OnNotificationDelegate newListenerDelegate, NT type)
  14. {
  15. int typeInt = type.ToInt32(null);
  16. //Note that these checks will only be done when DEBUG is defined
  17. _listeners[typeInt] += newListenerDelegate;
  18. }
  19. static bool IsAlreadyRegistered(OnNotificationDelegate newListenerDelegate, int typeInt)
  20. {
  21. if (_listeners[typeInt] == null)
  22. {
  23. return false;
  24. }
  25. Delegate[] invocationList = _listeners[typeInt].GetInvocationList();
  26. foreach (Delegate d in invocationList)
  27. {
  28. if (d == newListenerDelegate)
  29. {
  30. return true;
  31. }
  32. }
  33. return false;
  34. }
  35. public static void RemoveListener(OnNotificationDelegate listenerDelegate, NT type)
  36. {
  37. int typeInt = type.ToInt32(null);
  38. _listeners[typeInt] -= listenerDelegate;
  39. }
  40. public static void Post(NT type)
  41. {
  42. //AVDebug.Log("Firing "+type.ToString());
  43. N n = new N();
  44. n.type = type;
  45. Post(n);
  46. }
  47. public static void Post(NT type, object data)
  48. {
  49. N n = new N();
  50. n.type = type;
  51. n.data = data;
  52. Post(n);
  53. }
  54. public static void Post(N note)
  55. {
  56. int typeInt = note.type.ToInt32(null);
  57. if (_listeners[typeInt] != null)
  58. {
  59. _listeners[typeInt](note);
  60. }
  61. }
  62. #region For Testing/Debugging only
  63. /// <summary>
  64. /// Used for debugging to outputs the number of listeners to have an idea of how many listeners are currently registered.
  65. /// </summary>
  66. public static void OutputNumOfListeners()
  67. {
  68. for (int i = 0; i < _listeners.Length; i++)
  69. {
  70. NT notificationType = (NT)(object)i;
  71. }
  72. }
  73. public static int GetListenerCount(NT notificationType)
  74. {
  75. var typeListeners = _listeners[notificationType.ToInt32(null)];
  76. if (typeListeners != null)
  77. {
  78. return typeListeners.GetInvocationList().Length;
  79. } else
  80. {
  81. return 0;
  82. }
  83. }
  84. public static int GetTotalListenersCount()
  85. {
  86. int total = 0;
  87. for (int i = 0; i < _listeners.Length; i++)
  88. {
  89. total += GetListenerCount((NT)(object)i);
  90. }
  91. return total;
  92. }
  93. public static void Reset_USE_ONLY_FOR_UNIT_TESTS()
  94. {
  95. _listeners = new OnNotificationDelegate[System.Enum.GetValues(typeof(NT)).Length];
  96. }
  97. #endregion
  98. }