Blend.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. using UnityEngine;
  2. namespace BehaviorDesigner.Runtime.Tasks.Unity.UnityAnimation
  3. {
  4. [TaskCategory("Unity/Animation")]
  5. [TaskDescription("Blends the animation. Returns Success.")]
  6. public class Blend : 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 weight the animation should blend to")]
  13. public float targetWeight = 1;
  14. [Tooltip("The amount of time it takes to blend")]
  15. public float fadeLength = 0.3f;
  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.Blend(animationName.Value, targetWeight, fadeLength);
  34. return TaskStatus.Success;
  35. }
  36. public override void OnReset()
  37. {
  38. targetGameObject = null;
  39. animationName = "";
  40. targetWeight = 1;
  41. fadeLength = 0.3f;
  42. }
  43. }
  44. }