SetEnableEmission.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. using UnityEngine;
  2. namespace BehaviorDesigner.Runtime.Tasks.Unity.UnityParticleSystem
  3. {
  4. [TaskCategory("Unity/ParticleSystem")]
  5. [TaskDescription("Enables or disables the Particle System emission.")]
  6. public class SetEnableEmission : Action
  7. {
  8. [Tooltip("The GameObject that the task operates on. If null the task GameObject is used.")]
  9. public SharedGameObject targetGameObject;
  10. [Tooltip("Enable the ParticleSystem emissions?")]
  11. public SharedBool enable;
  12. private ParticleSystem particleSystem;
  13. private GameObject prevGameObject;
  14. public override void OnStart()
  15. {
  16. var currentGameObject = GetDefaultGameObject(targetGameObject.Value);
  17. if (currentGameObject != prevGameObject) {
  18. particleSystem = currentGameObject.GetComponent<ParticleSystem>();
  19. prevGameObject = currentGameObject;
  20. }
  21. }
  22. public override TaskStatus OnUpdate()
  23. {
  24. if (particleSystem == null) {
  25. Debug.LogWarning("ParticleSystem is null");
  26. return TaskStatus.Failure;
  27. }
  28. var emission = particleSystem.emission;
  29. emission.enabled = enable.Value;
  30. return TaskStatus.Success;
  31. }
  32. public override void OnReset()
  33. {
  34. targetGameObject = null;
  35. enable = false;
  36. }
  37. }
  38. }