PlayQueued.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. using UnityEngine;
  2. namespace BehaviorDesigner.Runtime.Tasks.Unity.UnityAnimation
  3. {
  4. [TaskCategory("Unity/Animation")]
  5. [TaskDescription("Plays an animation after previous animations has finished playing. Returns Success.")]
  6. public class PlayQueued : 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 name of the animation")]
  11. public SharedString animationName;
  12. [Tooltip("Specifies when the animation should start playing")]
  13. public QueueMode queue = QueueMode.CompleteOthers;
  14. [Tooltip("The play mode of the animation")]
  15. public PlayMode playMode = PlayMode.StopSameLayer;
  16. // cache the animation component
  17. private Animation animation;
  18. private GameObject prevGameObject;
  19. public override void OnStart()
  20. {
  21. var currentGameObject = GetDefaultGameObject(targetGameObject.Value);
  22. if (currentGameObject != prevGameObject) {
  23. animation = currentGameObject.GetComponent<Animation>();
  24. prevGameObject = currentGameObject;
  25. }
  26. }
  27. public override TaskStatus OnUpdate()
  28. {
  29. if (animation == null) {
  30. Debug.LogWarning("Animation is null");
  31. return TaskStatus.Failure;
  32. }
  33. animation.PlayQueued(animationName.Value, queue, playMode);
  34. return TaskStatus.Success;
  35. }
  36. public override void OnReset()
  37. {
  38. targetGameObject = null;
  39. animationName.Value = "";
  40. queue = QueueMode.CompleteOthers;
  41. playMode = PlayMode.StopSameLayer;
  42. }
  43. }
  44. }