step-timeline/Scripts/Commands/AnimationCmd.cs

78 lines
2.6 KiB
C#

using TriInspector;
using UnityEngine;
namespace R0bbie.Timeline
{
/// <summary>
/// Animation StepCmd type, used to control an animation
/// </summary>
[AddComponentMenu("Timeline/Commands/Animation (Step Command)")]
[RequireComponent(typeof(Step))]
public class AnimationCmd : StepCmd
{
[Title("Animation Options")]
[SerializeField] protected Animator animator;
[SerializeField] bool forceAnimation;
[InfoBox("If forcing the animation, the animationTrigger string should be the name on the AnimatorState(not the trigger parameter) to be able to force it")]
[SerializeField] protected string animationTrigger;
/// <summary>
/// Initialise command (called before Activate)
/// </summary>
protected override void Init()
{
init = true;
}
public override void Activate(Step _parentStep)
{
base.Activate(_parentStep);
// Check the necessary command values are set before continuing
if (animator == null || string.IsNullOrEmpty(animationTrigger))
{
Debug.LogWarning("Tried to activate an AnimationCmd with a null animator or trigger. Couldn't continue.");
DeactivateEarly();
return;
}
// Ensure movement resumed to normal on animator (in case paused before)
// The "Speed" float of the animator is used just to set a character idle at the start of an anim so it needs to be setup on the editor
// For the resume function we just need to resume the speed on the animator itself
animator.speed = 1f;
// Play animation
if (forceAnimation)
animator.Play(animationTrigger);
else
animator.SetTrigger(animationTrigger);
// Ensure GO attached to animator is enabled
if (!animator.isActiveAndEnabled)
animator.gameObject.SetActive(true);
}
protected override void Cleanup()
{
active = false;
}
#if UNITY_EDITOR
public Animator GetAnimator() { return animator; }
public string GetAnimatorString() { return animationTrigger; }
public bool GetForceAnimation() { return forceAnimation; }
public void SetAnimatorEditor(Animator editorAnimator)
{
animator = editorAnimator;
}
#endif
}
}