Rewind.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using UnityEngine;
  2. namespace BehaviorDesigner.Runtime.Tasks.Unity.UnityAnimation
  3. {
  4. [TaskCategory("Unity/Animation")]
  5. [TaskDescription("Rewinds an animation. Rewinds all animations if animationName is blank. Returns Success.")]
  6. public class Rewind : 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. // cache the animation component
  13. private Animation animation;
  14. private GameObject prevGameObject;
  15. public override void OnStart()
  16. {
  17. var currentGameObject = GetDefaultGameObject(targetGameObject.Value);
  18. if (currentGameObject != prevGameObject) {
  19. animation = currentGameObject.GetComponent<Animation>();
  20. prevGameObject = currentGameObject;
  21. }
  22. }
  23. public override TaskStatus OnUpdate()
  24. {
  25. if (animation == null) {
  26. Debug.LogWarning("Animation is null");
  27. return TaskStatus.Failure;
  28. }
  29. if (string.IsNullOrEmpty(animationName.Value)) {
  30. animation.Rewind();
  31. } else {
  32. animation.Rewind(animationName.Value);
  33. }
  34. return TaskStatus.Success;
  35. }
  36. public override void OnReset()
  37. {
  38. targetGameObject = null;
  39. animationName.Value = "";
  40. }
  41. }
  42. }