CrossFadeQueued.cs 1.9 KB

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