MultiAction.cs 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. using System;
  2. using System.Collections.Generic;
  3. namespace Rokid.UXR.Interaction {
  4. /// <summary>
  5. /// MAction can be used in place of Action. This allows
  6. /// for interfaces with Actions of generic covariant types
  7. /// to be subscribed to by multiple types of delegates.
  8. /// </summary>
  9. public interface MAction<out T>
  10. {
  11. event Action<T> Action;
  12. }
  13. /// <summary>
  14. /// Classes that implement an interface that has MActions
  15. /// can use MultiAction as their MAction implementation to
  16. /// allow for multiple types of delegates to subscribe to the
  17. /// generic type.
  18. /// </summary>
  19. public class MultiAction<T> : MAction<T>
  20. {
  21. protected HashSet<Action<T>> actions = new HashSet<Action<T>>();
  22. public event Action<T> Action
  23. {
  24. add
  25. {
  26. actions.Add(value);
  27. }
  28. remove
  29. {
  30. actions.Remove(value);
  31. }
  32. }
  33. public void Invoke(T t)
  34. {
  35. foreach (Action<T> action in actions)
  36. {
  37. action(t);
  38. }
  39. }
  40. }
  41. }