ObservableVisibleTrigger.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using System; // require keep for Windows Universal App
  2. using UnityEngine;
  3. namespace UniRx.Triggers
  4. {
  5. [DisallowMultipleComponent]
  6. public class ObservableVisibleTrigger : ObservableTriggerBase
  7. {
  8. Subject<Unit> onBecameInvisible;
  9. /// <summary>OnBecameInvisible is called when the renderer is no longer visible by any camera.</summary>
  10. void OnBecameInvisible()
  11. {
  12. if (onBecameInvisible != null) onBecameInvisible.OnNext(Unit.Default);
  13. }
  14. /// <summary>OnBecameInvisible is called when the renderer is no longer visible by any camera.</summary>
  15. public IObservable<Unit> OnBecameInvisibleAsObservable()
  16. {
  17. return onBecameInvisible ?? (onBecameInvisible = new Subject<Unit>());
  18. }
  19. Subject<Unit> onBecameVisible;
  20. /// <summary>OnBecameVisible is called when the renderer became visible by any camera.</summary>
  21. void OnBecameVisible()
  22. {
  23. if (onBecameVisible != null) onBecameVisible.OnNext(Unit.Default);
  24. }
  25. /// <summary>OnBecameVisible is called when the renderer became visible by any camera.</summary>
  26. public IObservable<Unit> OnBecameVisibleAsObservable()
  27. {
  28. return onBecameVisible ?? (onBecameVisible = new Subject<Unit>());
  29. }
  30. protected override void RaiseOnCompletedOnDestroy()
  31. {
  32. if (onBecameInvisible != null)
  33. {
  34. onBecameInvisible.OnCompleted();
  35. }
  36. if (onBecameVisible != null)
  37. {
  38. onBecameVisible.OnCompleted();
  39. }
  40. }
  41. }
  42. }