// Animancer // Copyright 2020 Kybernetik //
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using UnityEngine.Animations;
using UnityEngine.Playables;
using Object = UnityEngine.Object;
namespace Animancer
{
///
/// A layer on which animations can play with their states managed independantly of other layers while blending the
/// output with those layers.
///
/// This class can be used as a custom yield instruction to wait until all animations finish playing.
///
public sealed class AnimancerLayer : AnimancerNode, IAnimationClipCollection
{
/************************************************************************************************************************/
#region Fields and Properties
/************************************************************************************************************************/
/// [Internal] Constructs a new .
internal AnimancerLayer(AnimancerPlayable root, int index)
: base(root)
{
Index = index;
_Playable = AnimationMixerPlayable.Create(Root._Graph);
}
/************************************************************************************************************************/
/// A layer is its own root.
public override AnimancerLayer Layer { get { return this; } }
/// The receives the output of the .
public override IPlayableWrapper Parent { get { return Root; } }
/// Indicates whether child playables should stay connected to this mixer at all times.
public override bool KeepChildrenConnected { get { return Root.KeepChildrenConnected; } }
/************************************************************************************************************************/
/// All of the animation states connected to this layer.
private readonly List States = new List();
/************************************************************************************************************************/
private AnimancerState _CurrentState;
///
/// The state of the animation currently being played.
///
/// Specifically, this is the state that was most recently started using any of the Play or CrossFade methods
/// on this layer. States controlled individually via methods in the itself will
/// not register in this property.
///
/// Each time this property changes, the is incremented.
///
public AnimancerState CurrentState
{
get { return _CurrentState; }
private set
{
_CurrentState = value;
CommandCount++;
}
}
///
/// The number of times the has changed. By storing this value and later comparing
/// the stored value to the current value, you can determine whether the state has been changed since then,
/// even it has changed back to the same state.
///
public int CommandCount { get; private set; }
/************************************************************************************************************************/
/// [Pro-Only]
/// Determines whether this layer is set to additive blending. Otherwise it will override any earlier layers.
///
public bool IsAdditive
{
get { return Root.Layers.IsAdditive(Index); }
set { Root.Layers.SetAdditive(Index, value); }
}
/************************************************************************************************************************/
/// [Pro-Only]
/// Sets an to determine which bones this layer will affect.
///
public void SetMask(AvatarMask mask)
{
Root.Layers.SetMask(Index, mask);
}
#if UNITY_EDITOR
/// [Editor-Only]
/// The that determines which bones this layer will affect.
///
internal AvatarMask _Mask;
#endif
/************************************************************************************************************************/
///
/// The average velocity of the root motion of all currently playing animations, taking their current
/// into account.
///
public Vector3 AverageVelocity
{
get
{
var velocity = default(Vector3);
var count = States.Count;
for (int i = 0; i < count; i++)
{
var state = States[i];
velocity += state.AverageVelocity * state.Weight;
}
return velocity;
}
}
/************************************************************************************************************************/
#endregion
/************************************************************************************************************************/
#region Child States
/************************************************************************************************************************/
/// The number of states using this layer as their .
public override int ChildCount { get { return States.Count; } }
/// Returns the state connected to the specified `index` as a child of this layer.
/// This method is identical to .
public override AnimancerState GetChild(int index)
{
return States[index];
}
/// Returns the state connected to the specified `index` as a child of this layer.
/// This indexer is identical to .
public AnimancerState this[int index]
{
get { return States[index]; }
}
/************************************************************************************************************************/
///
/// Adds a new port and uses to connect the `state` to it.
///
public void AddChild(AnimancerState state)
{
if (state.Parent == this)
return;
var index = States.Count;
States.Add(null);
_Playable.SetInputCount(index + 1);
state.SetParent(this, index);
}
/************************************************************************************************************************/
/// Connects the `state` to this layer at its .
protected internal override void OnAddChild(AnimancerState state)
{
OnAddChild(States, state);
}
/************************************************************************************************************************/
/// Disconnects the `state` from this layer at its .
protected internal override void OnRemoveChild(AnimancerState state)
{
var index = state.Index;
Validate.RemoveChild(state, States);
if (_Playable.GetInput(index).IsValid())
Root._Graph.Disconnect(_Playable, index);
// Swap the last state into the place of the one that was just removed.
var lastPort = States.Count - 1;
if (index < lastPort)
{
state = States[lastPort];
state.DisconnectFromGraph();
States[index] = state;
state.Index = index;
if (KeepChildrenConnected || state.Weight != 0)
state.ConnectToGraph();
}
States.RemoveAt(lastPort);
_Playable.SetInputCount(lastPort);
}
/************************************************************************************************************************/
///
/// Returns an enumerator that will iterate through all states connected directly to this layer (not inside
/// s).
///
public override IEnumerator GetEnumerator() { return States.GetEnumerator(); }
/************************************************************************************************************************/
#endregion
/************************************************************************************************************************/
#region Create State
/************************************************************************************************************************/
///
/// Creates and returns a new to play the `clip`.
///
/// This method uses to determine the .
///
public ClipState CreateState(AnimationClip clip)
{
return CreateState(Root.GetKey(clip), clip);
}
///
/// Creates and returns a new to play the `clip` and registers it with the `key`.
///
public ClipState CreateState(object key, AnimationClip clip)
{
var state = new ClipState(this, clip);
Root.States.Register(key, state);
return state;
}
/************************************************************************************************************************/
///
/// Calls for each of the specified clips.
///
/// If you only want to create a single state, use .
///
public void CreateIfNew(AnimationClip clip0, AnimationClip clip1)
{
GetOrCreateState(clip0);
GetOrCreateState(clip1);
}
///
/// Calls for each of the specified clips.
///
/// If you only want to create a single state, use .
///
public void CreateIfNew(AnimationClip clip0, AnimationClip clip1, AnimationClip clip2)
{
GetOrCreateState(clip0);
GetOrCreateState(clip1);
GetOrCreateState(clip2);
}
///
/// Calls for each of the specified clips.
///
/// If you only want to create a single state, use .
///
public void CreateIfNew(AnimationClip clip0, AnimationClip clip1, AnimationClip clip2, AnimationClip clip3)
{
GetOrCreateState(clip0);
GetOrCreateState(clip1);
GetOrCreateState(clip2);
GetOrCreateState(clip3);
}
///
/// Calls for each of the specified clips.
///
/// If you only want to create a single state, use .
///
public void CreateIfNew(params AnimationClip[] clips)
{
if (clips == null)
return;
var count = clips.Length;
for (int i = 0; i < count; i++)
{
var clip = clips[i];
if (clip != null)
GetOrCreateState(clip);
}
}
/************************************************************************************************************************/
///
/// Calls and returns the state which registered with that key or
/// creates one if it doesn't exist.
///
/// If the state already exists but has the wrong , the `allowSetClip`
/// parameter determines what will happen. False causes it to throw an while
/// true allows it to change the . Note that the change is somewhat costly to
/// performance to use with caution.
///
///
public AnimancerState GetOrCreateState(AnimationClip clip, bool allowSetClip = false)
{
return GetOrCreateState(Root.GetKey(clip), clip, allowSetClip);
}
///
/// Returns the state registered with the if there is one. Otherwise
/// this method uses to create a new one and registers it with
/// that key before returning it.
///
public AnimancerState GetOrCreateState(ITransition transition)
{
var key = transition.Key;
AnimancerState state;
if (!Root.States.TryGet(key, out state))
{
state = transition.CreateState(this);
Root.States.Register(key, state);
}
return state;
}
///
/// Returns the state which registered with the `key` or creates one if it doesn't exist.
///
/// If the state already exists but has the wrong , the `allowSetClip`
/// parameter determines what will happen. False causes it to throw an while
/// true allows it to change the . Note that the change is somewhat costly to
/// performance to use with caution.
///
///
///
/// Thrown if the `key` is null.
///
/// See also: .
///
public AnimancerState GetOrCreateState(object key, AnimationClip clip, bool allowSetClip = false)
{
if (key == null)
throw new ArgumentNullException("key");
AnimancerState state;
if (Root.States.TryGet(key, out state))
{
// If a state exists with the 'key' but has the wrong clip, either change it or complain.
if (!ReferenceEquals(state.Clip, clip))
{
if (allowSetClip)
{
state.Clip = clip;
}
else
{
throw new ArgumentException(string.Concat(
"A state already exists using the specified 'key', but has a different AnimationClip:",
"\n - Key: ", key.ToString(),
"\n - Existing Clip: ", state.Clip.ToString(),
"\n - New Clip: ", clip.ToString()));
}
}
// Otherwise make sure it is on the correct layer.
else
{
AddChild(state);
}
}
else
{
state = CreateState(key, clip);
}
return state;
}
/************************************************************************************************************************/
///
/// Destroys all states connected to this layer. This operation cannot be undone.
///
public void DestroyStates()
{
var count = States.Count;
while (--count >= 0)
{
States[count].Destroy();
}
States.Clear();
}
/************************************************************************************************************************/
#endregion
/************************************************************************************************************************/
#region Play Management
/************************************************************************************************************************/
// Starting
/************************************************************************************************************************/
///
/// Called by (when this layer starts fading, not when one of its states
/// starts fading). Clears the of all states.
///
protected internal override void OnStartFade()
{
var count = States.Count;
for (int i = 0; i < count; i++)
{
States[i].OnStartFade();
}
}
/************************************************************************************************************************/
///
/// Stops all other animations, plays the `clip`, and returns its state.
///
/// The animation will continue playing from its current .
/// To restart it from the beginning you can use ...Play(clip).Time = 0;.
///
public AnimancerState Play(AnimationClip clip)
{
return Play(GetOrCreateState(clip));
}
///
/// Stops all other animations, plays the `state`, and returns it.
///
/// The animation will continue playing from its current .
/// If you wish to force it back to the start, you can simply set the `state`s time to 0.
///
public AnimancerState Play(AnimancerState state)
{
Validate.Root(state, Root);
if (TargetWeight != 1)
Weight = 1;
AddChild(state);
CurrentState = state;
state.Play();
var count = States.Count;
for (int i = 0; i < count; i++)
{
var otherState = States[i];
if (otherState != state)
otherState.Stop();
}
return state;
}
///
/// Creates a state for the `transition` if it didn't already exist, then calls
/// or
/// depending on the .
///
public AnimancerState Play(ITransition transition)
{
return Play(transition, transition.FadeDuration, transition.FadeMode);
}
///
/// Stops all other animations, plays the animation registered with the `key`, and returns that
/// state. If no state is registered with the `key`, this method does nothing and returns null.
///
/// The animation will continue playing from its current .
/// If you wish to force it back to the start, you can simply set the returned state's time to 0.
/// on the returned state.
///
public AnimancerState Play(object key)
{
AnimancerState state;
if (Root.States.TryGet(key, out state))
return Play(state);
else
return null;
}
/************************************************************************************************************************/
///
/// Starts fading in the `clip` over the course of the `fadeDuration` while fading out all others in the same
/// layer. Returns its state.
///
/// If the `state` was already playing and fading in with less time remaining than the `fadeDuration`, this
/// method will allow it to complete the existing fade rather than starting a slower one.
///
/// If the layer currently has 0 , this method will fade in the layer itself
/// and simply the `state`.
///
/// Animancer Lite only allows the default `fadeDuration` (0.25 seconds) in a runtime build.
///
public AnimancerState Play(AnimationClip clip, float fadeDuration, FadeMode mode = FadeMode.FixedSpeed)
{
return Play(Root.States.GetOrCreate(clip), fadeDuration, mode);
}
///
/// Starts fading in the `state` over the course of the `fadeDuration` while fading out all others in this
/// layer. Returns the `state`.
///
/// If the `state` was already playing and fading in with less time remaining than the `fadeDuration`, this
/// method will allow it to complete the existing fade rather than starting a slower one.
///
/// If the layer currently has 0 , this method will fade in the layer itself
/// and simply the `state`.
///
/// Animancer Lite only allows the default `fadeDuration` (0.25 seconds) in a runtime build.
///
public AnimancerState Play(AnimancerState state, float fadeDuration, FadeMode mode = FadeMode.FixedSpeed)
{
Validate.Root(state, Root);
if (fadeDuration <= 0 ||// With no fade duration, Play immediately.
(Index == 0 && Weight == 0))// First animation on Layer 0 snap Weight to 1.
return Play(state);
bool isFixedDuration;
EvaluateFadeMode(mode, ref state, ref fadeDuration, out isFixedDuration);
StartFade(1, fadeDuration);
if (Weight == 0)
return Play(state);
AddChild(state);
CurrentState = state;
// If the state is already playing or will finish fading in faster than this new fade,
// continue the existing fade but still pretend it was restarted.
if (state.IsPlaying && state.TargetWeight == 1 &&
(state.Weight == 1 || state.FadeSpeed * fadeDuration > Math.Abs(1 - state.Weight)))
{
OnStartFade();
}
// Otherwise fade in the target state and fade out all others.
else
{
state.IsPlaying = true;
state.StartFade(1, fadeDuration);
var count = States.Count;
for (int i = 0; i < count; i++)
{
var otherState = States[i];
if (otherState != state)
otherState.StartFade(0, fadeDuration);
}
}
return state;
}
///
/// Creates a state for the `transition` if it didn't already exist, then calls
/// or
/// depending on the .
///
public AnimancerState Play(ITransition transition, float fadeDuration, FadeMode mode = FadeMode.FixedSpeed)
{
var state = Root.States.GetOrCreate(transition);
state = Play(state, fadeDuration, mode);
transition.Apply(state);
return state;
}
///
/// Starts fading in the animation registered with the `key` over the course of the `fadeDuration` while fading
/// out all others in the same layer. Returns the animation's state (or null if none was registered).
///
/// If the `state` was already playing and fading in with less time remaining than the `fadeDuration`, this
/// method will allow it to complete the existing fade rather than starting a slower one.
///
/// If the layer currently has 0 , this method will fade in the layer itself
/// and simply the `state`.
///
/// Animancer Lite only allows the default `fadeDuration` (0.25 seconds) in a runtime build.
///
public AnimancerState Play(object key, float fadeDuration, FadeMode mode = FadeMode.FixedSpeed)
{
AnimancerState state;
if (Root.States.TryGet(key, out state))
return Play(state, fadeDuration, mode);
else
return null;
}
/************************************************************************************************************************/
///
/// Manipulates the other parameters according to the `mode`.
///
private void EvaluateFadeMode(FadeMode mode, ref AnimancerState state, ref float fadeDuration, out bool isFixedDuration)
{
switch (mode)
{
case FadeMode.FixedSpeed:
fadeDuration *= Mathf.Abs(1 - state.Weight);
isFixedDuration = false;
break;
case FadeMode.FixedDuration:
isFixedDuration = true;
break;
case FadeMode.FromStart:
{
var previousState = state;
state = GetOrCreateWeightlessState(state);
if (previousState != state)
{
var previousLayer = previousState.Layer;
if (previousLayer != this && previousLayer.CurrentState == previousState)
previousLayer.StartFade(0, fadeDuration);
}
isFixedDuration = false;
break;
}
case FadeMode.NormalizedSpeed:
fadeDuration *= Mathf.Abs(1 - state.Weight) * state.Length;
isFixedDuration = false;
break;
case FadeMode.NormalizedDuration:
fadeDuration *= state.Length;
isFixedDuration = true;
break;
case FadeMode.NormalizedFromStart:
{
var previousState = state;
state = GetOrCreateWeightlessState(state);
fadeDuration *= state.Length;
if (previousState != state)
{
var previousLayer = previousState.Layer;
if (previousLayer != this && previousLayer.CurrentState == previousState)
previousLayer.StartFade(0, fadeDuration);
}
isFixedDuration = false;
break;
}
default:
throw new ArgumentException("Invalid FadeMode: " + mode, "mode");
}
}
/************************************************************************************************************************/
#if UNITY_EDITOR
/// [Editor-Only]
/// The maximum number of duplicate states that can be created by for
/// a single clip before it will start giving usage warnings. Default = 5.
///
public static int maxStateDepth = 5;
#endif
///
/// If the `state` is not currently at 0 , this method finds a copy of it
/// which is at 0 or creates a new one.
///
/// Thrown if the is null.
///
/// Thrown if more states have been created for this than the
/// allows.
///
public AnimancerState GetOrCreateWeightlessState(AnimancerState state)
{
if (state.Weight != 0)
{
var clip = state.Clip;
if (clip == null)
{
throw new InvalidOperationException(
"GetOrCreateWeightlessState was called on a state which has no clip: " + state);
// We could probably support any state type by giving them a Clone method, but that would take a
// lot of work for something that might never get used.
}
else
{
// Get the default state registered with the clip.
if (state.Key as Object != clip)
state = Root.States.GetOrCreate(clip, clip);
#if UNITY_EDITOR
int depth = 0;
#endif
// If that state is not at 0 weight, get or create another state registered using the previous state as a key.
// Keep going through states in this manner until you find one at 0 weight.
while (state.Weight != 0)
{
// Explicitly cast the state to an object to avoid the overload that warns about using a state as a key.
state = Root.States.GetOrCreate((object)state, clip);
#if UNITY_EDITOR
if (++depth == maxStateDepth)
{
throw new ArgumentOutOfRangeException("depth", "GetOrCreateWeightlessState has created " +
maxStateDepth + " or more states for a single clip." +
" This is most likely a result of calling the method repeatedly on consecutive frames." +
" You probably just want to use FadeMode.FixedSpeed instead, but you can increase" +
" AnimancerLayer.maxStateDepth if necessary.");
}
#endif
}
}
}
// Make sure it is on this layer and at time 0.
AddChild(state);
state.Time = 0;
return state;
}
/************************************************************************************************************************/
// Stopping
/************************************************************************************************************************/
///
/// Sets = 0 and calls on all animations
/// to stop them from playing and rewind them to the start.
///
public override void Stop()
{
base.Stop();
CurrentState = null;
var count = States.Count;
while (--count >= 0)
{
States[count].Stop();
}
}
/************************************************************************************************************************/
// Checking
/************************************************************************************************************************/
///
/// Returns true if the `clip` is currently being played by at least one state.
///
public bool IsPlayingClip(AnimationClip clip)
{
var count = States.Count;
for (int i = 0; i < count; i++)
{
var state = States[i];
if (state.Clip == clip && state.IsPlaying)
return true;
}
return false;
}
///
/// Returns true if at least one animation is being played.
///
public bool IsAnyStatePlaying()
{
var count = States.Count;
for (int i = 0; i < count; i++)
{
if (States[i].IsPlaying)
return true;
}
return false;
}
///
/// Returns true if the is playing and hasn't yet reached its end.
///
/// This method is called by so this object can be used as a custom yield
/// instruction to wait until it finishes.
///
protected internal override bool IsPlayingAndNotEnding()
{
return _CurrentState != null && _CurrentState.IsPlayingAndNotEnding();
}
/************************************************************************************************************************/
///
/// Calculates the total of all states in this layer.
///
public float GetTotalWeight()
{
float weight = 0;
var count = States.Count;
for (int i = 0; i < count; i++)
{
weight += States[i].Weight;
}
return weight;
}
/************************************************************************************************************************/
///
/// Invokes the callback of the state that is playing the animation
/// which triggered the event. Returns true if such a state exists (even if it doesn't have a callback).
///
internal bool TryInvokeOnEndEvent(AnimationEvent animationEvent)
{
var count = States.Count;
for (int i = 0; i < count; i++)
{
if (AnimancerPlayable.TryInvokeOnEndEvent(animationEvent, States[i]))
return true;
}
return false;
}
/************************************************************************************************************************/
#endregion
/************************************************************************************************************************/
#region Inverse Kinematics
/************************************************************************************************************************/
///
/// Determines the default value of for all new states created in
/// this layer. Default false.
///
/// It requires Unity 2018.1 or newer, however 2018.3 or newer is recommended because a bug in earlier versions
/// of the Playables API caused this value to only take effect while at least one state was at
/// = 1 which meant that IK would not work while fading between animations.
///
public bool DefaultApplyAnimatorIK { get; set; }
///
/// Determines whether OnAnimatorIK(int layerIndex) will be called on the animated object for any
/// . The initial value is determined by when a new
/// state is created and setting this value will also set the default.
///
/// This is equivalent to the "IK Pass" toggle in Animator Controller layers, except that due to limitations in
/// the Playables API the layerIndex will always be zero.
///
/// It requires Unity 2018.1 or newer, however 2018.3 or newer is recommended because a bug in earlier versions
/// of the Playables API caused this value to only take effect while at least one state was at
/// = 1 which meant that IK would not work while fading between animations.
///
public bool ApplyAnimatorIK
{
get
{
var count = States.Count;
for (int i = 0; i < count; i++)
{
if (States[i].ApplyAnimatorIK)
return true;
}
return false;
}
set
{
DefaultApplyAnimatorIK = value;
var count = States.Count;
for (int i = 0; i < count; i++)
{
States[i].ApplyAnimatorIK = value;
}
}
}
/************************************************************************************************************************/
///
/// Determines the default value of for all new states created in this
/// layer. Default false.
///
public bool DefaultApplyFootIK { get; set; }
///
/// Determines whether any of the in this layer are applying IK to the character's feet.
/// The initial value is determined by when a new state is created.
///
/// This is equivalent to the "Foot IK" toggle in Animator Controller states (applied to the whole layer).
///
public bool ApplyFootIK
{
get
{
var count = States.Count;
for (int i = 0; i < count; i++)
{
if (States[i].ApplyFootIK)
return true;
}
return false;
}
set
{
DefaultApplyFootIK = value;
var count = States.Count;
for (int i = 0; i < count; i++)
{
States[i].ApplyFootIK = value;
}
}
}
/************************************************************************************************************************/
#endregion
/************************************************************************************************************************/
#region Inspector
/************************************************************************************************************************/
/// []
/// Gathers all the animations in this layer.
///
public void GatherAnimationClips(ICollection clips)
{
clips.GatherFromSources(States);
}
/************************************************************************************************************************/
#if UNITY_EDITOR
/// [Editor-Only] The Inspector display name of this layer.
private string _Name;
#endif
/// The Inspector display name of this layer.
public override string ToString()
{
#if UNITY_EDITOR
if (_Name == null)
{
if (_Mask != null)
return _Mask.name;
_Name = Index == 0 ? "Base Layer" : "Layer " + Index;
}
return _Name;
#else
return "Layer " + Index;
#endif
}
/// [Editor-Conditional]
/// Sets the Inspector display name of this layer. Note that layer names are Editor-Only so any calls to this
/// method will automatically be compiled out of a runtime build.
///
[System.Diagnostics.Conditional(Strings.EditorOnly)]
public void SetName(string name)
{
#if UNITY_EDITOR
_Name = name;
#endif
}
/************************************************************************************************************************/
///
/// Called by to append the details of this node.
///
protected override void AppendDetails(StringBuilder text, string delimiter)
{
base.AppendDetails(text, delimiter);
text.Append(delimiter).Append("CurrentState: ").Append(CurrentState);
text.Append(delimiter).Append("CommandCount: ").Append(CommandCount);
text.Append(delimiter).Append("IsAdditive: ").Append(IsAdditive);
#if UNITY_EDITOR
text.Append(delimiter).Append("AvatarMask: ").Append(_Mask);
#endif
}
/************************************************************************************************************************/
#endregion
/************************************************************************************************************************/
}
}