ObservableRectTransformTrigger.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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 ObservableRectTransformTrigger : ObservableTriggerBase
  9. {
  10. Subject<Unit> onRectTransformDimensionsChange;
  11. // Callback that is sent if an associated RectTransform has it's dimensions changed
  12. void OnRectTransformDimensionsChange()
  13. {
  14. if (onRectTransformDimensionsChange != null) onRectTransformDimensionsChange.OnNext(Unit.Default);
  15. }
  16. /// <summary>Callback that is sent if an associated RectTransform has it's dimensions changed.</summary>
  17. public IObservable<Unit> OnRectTransformDimensionsChangeAsObservable()
  18. {
  19. return onRectTransformDimensionsChange ?? (onRectTransformDimensionsChange = new Subject<Unit>());
  20. }
  21. Subject<Unit> onRectTransformRemoved;
  22. // Callback that is sent if an associated RectTransform is removed
  23. void OnRectTransformRemoved()
  24. {
  25. if (onRectTransformRemoved != null) onRectTransformRemoved.OnNext(Unit.Default);
  26. }
  27. /// <summary>Callback that is sent if an associated RectTransform is removed.</summary>
  28. public IObservable<Unit> OnRectTransformRemovedAsObservable()
  29. {
  30. return onRectTransformRemoved ?? (onRectTransformRemoved = new Subject<Unit>());
  31. }
  32. protected override void RaiseOnCompletedOnDestroy()
  33. {
  34. if (onRectTransformDimensionsChange != null)
  35. {
  36. onRectTransformDimensionsChange.OnCompleted();
  37. }
  38. if (onRectTransformRemoved != null)
  39. {
  40. onRectTransformRemoved.OnCompleted();
  41. }
  42. }
  43. }
  44. }
  45. #endif