1
0

ObservableParticleTrigger.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. using System; // require keep for Windows Universal App
  2. using UnityEngine;
  3. namespace UniRx.Triggers
  4. {
  5. [DisallowMultipleComponent]
  6. public class ObservableParticleTrigger : ObservableTriggerBase
  7. {
  8. Subject<GameObject> onParticleCollision;
  9. #if UNITY_5_4_OR_NEWER
  10. Subject<Unit> onParticleTrigger;
  11. #endif
  12. /// <summary>OnParticleCollision is called when a particle hits a collider.</summary>
  13. void OnParticleCollision(GameObject other)
  14. {
  15. if (onParticleCollision != null) onParticleCollision.OnNext(other);
  16. }
  17. /// <summary>OnParticleCollision is called when a particle hits a collider.</summary>
  18. public IObservable<GameObject> OnParticleCollisionAsObservable()
  19. {
  20. return onParticleCollision ?? (onParticleCollision = new Subject<GameObject>());
  21. }
  22. #if UNITY_5_4_OR_NEWER
  23. /// <summary>OnParticleTrigger is called when any particles in a particle system meet the conditions in the trigger module.</summary>
  24. void OnParticleTrigger()
  25. {
  26. if (onParticleTrigger != null) onParticleTrigger.OnNext(Unit.Default);
  27. }
  28. /// <summary>OnParticleTrigger is called when any particles in a particle system meet the conditions in the trigger module.</summary>
  29. public IObservable<Unit> OnParticleTriggerAsObservable()
  30. {
  31. return onParticleTrigger ?? (onParticleTrigger = new Subject<Unit>());
  32. }
  33. #endif
  34. protected override void RaiseOnCompletedOnDestroy()
  35. {
  36. if (onParticleCollision != null)
  37. {
  38. onParticleCollision.OnCompleted();
  39. }
  40. #if UNITY_5_4_OR_NEWER
  41. if (onParticleTrigger != null)
  42. {
  43. onParticleTrigger.OnCompleted();
  44. }
  45. #endif
  46. }
  47. }
  48. }