Play.cs 1.6 KB

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