ObservableTransformChangedTrigger.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // after 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 UnityEngine;
  5. namespace UniRx.Triggers
  6. {
  7. [DisallowMultipleComponent]
  8. public class ObservableTransformChangedTrigger : ObservableTriggerBase
  9. {
  10. Subject<Unit> onBeforeTransformParentChanged;
  11. // Callback sent to the graphic before a Transform parent change occurs
  12. void OnBeforeTransformParentChanged()
  13. {
  14. if (onBeforeTransformParentChanged != null) onBeforeTransformParentChanged.OnNext(Unit.Default);
  15. }
  16. /// <summary>Callback sent to the graphic before a Transform parent change occurs.</summary>
  17. public IObservable<Unit> OnBeforeTransformParentChangedAsObservable()
  18. {
  19. return onBeforeTransformParentChanged ?? (onBeforeTransformParentChanged = new Subject<Unit>());
  20. }
  21. Subject<Unit> onTransformParentChanged;
  22. // This function is called when the parent property of the transform of the GameObject has changed
  23. void OnTransformParentChanged()
  24. {
  25. if (onTransformParentChanged != null) onTransformParentChanged.OnNext(Unit.Default);
  26. }
  27. /// <summary>This function is called when the parent property of the transform of the GameObject has changed.</summary>
  28. public IObservable<Unit> OnTransformParentChangedAsObservable()
  29. {
  30. return onTransformParentChanged ?? (onTransformParentChanged = new Subject<Unit>());
  31. }
  32. Subject<Unit> onTransformChildrenChanged;
  33. // This function is called when the list of children of the transform of the GameObject has changed
  34. void OnTransformChildrenChanged()
  35. {
  36. if (onTransformChildrenChanged != null) onTransformChildrenChanged.OnNext(Unit.Default);
  37. }
  38. /// <summary>This function is called when the list of children of the transform of the GameObject has changed.</summary>
  39. public IObservable<Unit> OnTransformChildrenChangedAsObservable()
  40. {
  41. return onTransformChildrenChanged ?? (onTransformChildrenChanged = new Subject<Unit>());
  42. }
  43. protected override void RaiseOnCompletedOnDestroy()
  44. {
  45. if (onBeforeTransformParentChanged != null)
  46. {
  47. onBeforeTransformParentChanged.OnCompleted();
  48. }
  49. if (onTransformParentChanged != null)
  50. {
  51. onTransformParentChanged.OnCompleted();
  52. }
  53. if (onTransformChildrenChanged != null)
  54. {
  55. onTransformChildrenChanged.OnCompleted();
  56. }
  57. }
  58. }
  59. }
  60. #endif