You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

71 lines
1.8 KiB

8 years ago
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. public class NotificationServer : MonoBehaviour
  6. {
  7. static Dictionary<string, List<Action>> notifications = new Dictionary<string, List<Action>>();
  8. static Dictionary<string, List<Action<object>>> notificationsWithArgument = new Dictionary<string, List<Action<object>>>();
  9. static public void register(string key, Action action)
  10. {
  11. if (!notifications.ContainsKey(key))
  12. notifications[key] = new List<Action>();
  13. if (!notifications[key].Contains(action))
  14. notifications[key].Add(action);
  15. }
  16. static public void unregister(string key, Action action)
  17. {
  18. if (!notifications.ContainsKey(key))
  19. return;
  20. if (!notifications[key].Contains(action))
  21. return;
  22. notifications[key].Remove(action);
  23. }
  24. static public void notify(string key)
  25. {
  26. if (!notifications.ContainsKey(key))
  27. return;
  28. for (int i=0; i<notifications[key].Count; i++)
  29. notifications[key][i]();
  30. }
  31. static public void register(string key, Action<object> action)
  32. {
  33. if (!notificationsWithArgument.ContainsKey(key))
  34. notificationsWithArgument[key] = new List<Action<object>>();
  35. if (!notificationsWithArgument[key].Contains(action))
  36. notificationsWithArgument[key].Add(action);
  37. }
  38. static public void unregister(string key, Action<object> action)
  39. {
  40. if (!notificationsWithArgument.ContainsKey(key))
  41. return;
  42. if (!notificationsWithArgument[key].Contains(action))
  43. return;
  44. notificationsWithArgument[key].Remove(action);
  45. }
  46. static public void notify(string key, object argument)
  47. {
  48. if (!notificationsWithArgument.ContainsKey(key))
  49. return;
  50. for (int i=0; i<notificationsWithArgument[key].Count; i++)
  51. notificationsWithArgument[key][i](argument);
  52. }
  53. public void doNotify(string key)
  54. {
  55. notify(key);
  56. }
  57. public void doNotify(string key, string argument)
  58. {
  59. notify(key, argument);
  60. }
  61. }