SetStartSpeed.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. using UnityEngine;
  2. namespace BehaviorDesigner.Runtime.Tasks.Unity.UnityParticleSystem
  3. {
  4. [TaskCategory("Unity/ParticleSystem")]
  5. [TaskDescription("Sets the start speed of the Particle System.")]
  6. public class SetStartSpeed : Action
  7. {
  8. [Tooltip("The GameObject that the task operates on. If null the task GameObject is used.")]
  9. public SharedGameObject targetGameObject;
  10. [Tooltip("The start speed of the ParticleSystem")]
  11. public SharedFloat startSpeed;
  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. ParticleSystem.MainModule mainParticleSystem = particleSystem.main;
  29. mainParticleSystem.startSpeed = startSpeed.Value;
  30. return TaskStatus.Success;
  31. }
  32. public override void OnReset()
  33. {
  34. targetGameObject = null;
  35. startSpeed = 0;
  36. }
  37. }
  38. }