2
0

UnityEventExtensions.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // for uGUI(from 4.6)
  2. #if !(UNITY_4_0 || UNITY_4_1 || UNITY_4_2 || UNITY_4_3 || UNITY_4_4 || UNITY_4_5)
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Text;
  6. using UnityEngine.Events;
  7. namespace UniRx
  8. {
  9. public static partial class UnityEventExtensions
  10. {
  11. public static IObservable<Unit> AsObservable(this UnityEngine.Events.UnityEvent unityEvent)
  12. {
  13. return Observable.FromEvent<UnityAction>(h => new UnityAction(h), h => unityEvent.AddListener(h), h => unityEvent.RemoveListener(h));
  14. }
  15. public static IObservable<T> AsObservable<T>(this UnityEngine.Events.UnityEvent<T> unityEvent)
  16. {
  17. return Observable.FromEvent<UnityAction<T>, T>(h => new UnityAction<T>(h), h => unityEvent.AddListener(h), h => unityEvent.RemoveListener(h));
  18. }
  19. public static IObservable<Tuple<T0, T1>> AsObservable<T0, T1>(this UnityEngine.Events.UnityEvent<T0, T1> unityEvent)
  20. {
  21. return Observable.FromEvent<UnityAction<T0, T1>, Tuple<T0, T1>>(h =>
  22. {
  23. return new UnityAction<T0, T1>((t0, t1) =>
  24. {
  25. h(Tuple.Create(t0, t1));
  26. });
  27. }, h => unityEvent.AddListener(h), h => unityEvent.RemoveListener(h));
  28. }
  29. public static IObservable<Tuple<T0, T1, T2>> AsObservable<T0, T1, T2>(this UnityEngine.Events.UnityEvent<T0, T1, T2> unityEvent)
  30. {
  31. return Observable.FromEvent<UnityAction<T0, T1, T2>, Tuple<T0, T1, T2>>(h =>
  32. {
  33. return new UnityAction<T0, T1, T2>((t0, t1, t2) =>
  34. {
  35. h(Tuple.Create(t0, t1, t2));
  36. });
  37. }, h => unityEvent.AddListener(h), h => unityEvent.RemoveListener(h));
  38. }
  39. public static IObservable<Tuple<T0, T1, T2, T3>> AsObservable<T0, T1, T2, T3>(this UnityEngine.Events.UnityEvent<T0, T1, T2, T3> unityEvent)
  40. {
  41. return Observable.FromEvent<UnityAction<T0, T1, T2, T3>, Tuple<T0, T1, T2, T3>>(h =>
  42. {
  43. return new UnityAction<T0, T1, T2, T3>((t0, t1, t2, t3) =>
  44. {
  45. h(Tuple.Create(t0, t1, t2, t3));
  46. });
  47. }, h => unityEvent.AddListener(h), h => unityEvent.RemoveListener(h));
  48. }
  49. }
  50. }
  51. #endif