SetMaxParticles.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. using UnityEngine;
  2. namespace BehaviorDesigner.Runtime.Tasks.Unity.UnityParticleSystem
  3. {
  4. [TaskCategory("Unity/ParticleSystem")]
  5. [TaskDescription("Sets the max particles of the Particle System.")]
  6. public class SetMaxParticles : 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 max particles of the ParticleSystem")]
  11. public SharedInt maxParticles;
  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.maxParticles = maxParticles.Value;
  30. return TaskStatus.Success;
  31. }
  32. public override void OnReset()
  33. {
  34. targetGameObject = null;
  35. maxParticles = 0;
  36. }
  37. }
  38. }