Introduction
This post is not about state machine basic, rather how we would setup/access state machine parameters.
There is a higher level Animator
class to access a part of the animator features, but we need to use
Unity’s Animations
package in order to dive deeper.
1 | using UnityEditor.Animations; |
There’s a lot of parts composing Unity’s Animator: Layer, Parameter, State machine (and everything inside it including states of different kind, transition between states), there are of course multiple parameters inside a single state.
The examples in this post is only about accessing states and state machines, as the rest is translatable.
We start from the root: Base layer, if we think about it, a layer is essentially a state machine. Inside this root state machine are sets of states (including blend tree/state/sub-state machine)
1 | // getting the base layer state machine |
(Base Layer)
Next up, a very confusing part of the scripting process are the definition of
AnimatorStateMachine
vs. ChildAnimatorStateMachine
and AnimatorState
vs. ChildAnimatorState
.
I really wish Unity provides internal ways to cast these to each other.
To summarize:
-
ChildAnimatorStateMachine[]
andChildAnimatorState[]
is used in conjunction with Arrays asAnimatorStateMachine.states
andAnimatorStateMachine.stateMachines
will return the previous mentioned type. -
Instead of casting, they have an internal property
ChildAnimatorStateMachine.stateMachine
andChildAnimatorState.state
which respectively returnsAnimatorStateMachine
andAnimatorState
type object
States
All states (state or. state with blendtree inside)
1 | ChildAnimatorState[] childStates = rootStateMachine.states; |
(Simple state only contains a single clip)
(BlendTree nested in a state)
Sub-state Machine
1 | ChildAnimatorStateMachine[] childSubStateMachines = rootStateMachine.stateMachines; |
(Essentially, another sub layer of state machine)
Reference
Unity Manual - State Machine Basics