GetPlaybackSpeed.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. using UnityEngine;
  2. namespace BehaviorDesigner.Runtime.Tasks.Unity.UnityParticleSystem
  3. {
  4. [TaskCategory("Unity/ParticleSystem")]
  5. [TaskDescription("Stores the playback speed of the Particle System.")]
  6. public class GetPlaybackSpeed : 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 playback speed of the ParticleSystem")]
  11. [RequiredField]
  12. public SharedFloat storeResult;
  13. private ParticleSystem particleSystem;
  14. private GameObject prevGameObject;
  15. public override void OnStart()
  16. {
  17. var currentGameObject = GetDefaultGameObject(targetGameObject.Value);
  18. if (currentGameObject != prevGameObject) {
  19. particleSystem = currentGameObject.GetComponent<ParticleSystem>();
  20. prevGameObject = currentGameObject;
  21. }
  22. }
  23. public override TaskStatus OnUpdate()
  24. {
  25. if (particleSystem == null) {
  26. Debug.LogWarning("ParticleSystem is null");
  27. return TaskStatus.Failure;
  28. }
  29. ParticleSystem.MainModule mainParticleSystem = particleSystem.main;
  30. storeResult.Value = mainParticleSystem.simulationSpeed;
  31. return TaskStatus.Success;
  32. }
  33. public override void OnReset()
  34. {
  35. targetGameObject = null;
  36. storeResult = 0;
  37. }
  38. }
  39. }