2
0

ObservableEnableTrigger.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using System; // require keep for Windows Universal App
  2. using UnityEngine;
  3. namespace UniRx.Triggers
  4. {
  5. [DisallowMultipleComponent]
  6. public class ObservableEnableTrigger : ObservableTriggerBase
  7. {
  8. Subject<Unit> onEnable;
  9. /// <summary>This function is called when the object becomes enabled and active.</summary>
  10. void OnEnable()
  11. {
  12. if (onEnable != null) onEnable.OnNext(Unit.Default);
  13. }
  14. /// <summary>This function is called when the object becomes enabled and active.</summary>
  15. public IObservable<Unit> OnEnableAsObservable()
  16. {
  17. return onEnable ?? (onEnable = new Subject<Unit>());
  18. }
  19. Subject<Unit> onDisable;
  20. /// <summary>This function is called when the behaviour becomes disabled () or inactive.</summary>
  21. void OnDisable()
  22. {
  23. if (onDisable != null) onDisable.OnNext(Unit.Default);
  24. }
  25. /// <summary>This function is called when the behaviour becomes disabled () or inactive.</summary>
  26. public IObservable<Unit> OnDisableAsObservable()
  27. {
  28. return onDisable ?? (onDisable = new Subject<Unit>());
  29. }
  30. protected override void RaiseOnCompletedOnDestroy()
  31. {
  32. if (onEnable != null)
  33. {
  34. onEnable.OnCompleted();
  35. }
  36. if (onDisable != null)
  37. {
  38. onDisable.OnCompleted();
  39. }
  40. }
  41. }
  42. }