Where I am right now: I have scriptable objects called AnimationPackage that define a list of Motions. I am using AnimationController.addMotion() to add these Motions to my AnimationController. They are created as new State with the name of the Motion used.
What I want to do: I need to add transitions between these new States. I assume I need to use AnimationController.SetStateEffectiveBehaviours() method to create those transitions.
My problem: While I do have the AnimationController and the name of the State, I do not have the State itself. How do I access it, to use it with SetStateEffectiveBehaviours()? I was unable to find a path from the AnimationController to its States so far, help.
I have some static methods you can use to get states in an animator. You may have to use unityeditor.internal though.
public static AnimatorState[] GetAnimatorStateNames(Animator _animator)
{
AnimatorController controller = _animator ? _animator.runtimeAnimatorController as AnimatorController : null;
return controller == null ? null : controller.layers.SelectMany(l => l.stateMachine.states).Select(s => s.state).ToArray();
}
public static AnimatorState[] GetAnimatorStateNames(AnimatorController _animator)
{
AnimatorController controller = _animator ? _animator : null;
return controller == null ? null : controller.layers.SelectMany(l => l.stateMachine.states).Select(s => s.state).ToArray();
}
public static int FindStateLayer(AnimatorController _animCont, string _stateName)
{
for (int i = 0; i < _animCont.layers.Length; i++)
{
foreach (var child in _animCont.layers*.stateMachine.states)*
{
if (child.state.name == _stateName)
return i;
}
}
Debug.LogError("could not find layer with state name: " + _stateName + " in: " + _animCont.name);
return 0;
}