I have such codes that made me unable to build because of using UnityEditor namespace(Motion ,AnimatorState and AnimatorController)
But I must get the length and speed of a not-playing clip in the statemachine by name to do some calc, is there any other way that doesn’t use UnityEditor namespace to help me get these info?
And, why can such important fields be placed in UnityEditor namespace? I feel really really confused.
#Update:
Most other infos have been found available through GetCurrentAnimatorStateInfo() and runtimeAnimatorController.animationClips.
But, the speed of the state got from GetCurrentAnimatorStateInfo() is diffirent from what I set in Editor. I set the speed to 2 in statemachine, and it plays at 2x speed, but the value I get from GetCurrentAnimatorStateInfo() is 1. In which way can I get the “2” ?
...
layerName = "Basic";
if (AniLength == 0)
{
state = GetStateByName(actor.animator, AniClipName, layerName);
AniLength = (int) (((AnimationClip) (state.motion)).length / Time.fixedDeltaTime / state.speed);
}
...
public static AnimatorState GetStateByName(Animator animator, string stateName, string layerName = "Full")
{
if (null == animator || string.IsNullOrEmpty(stateName) || null == animator.runtimeAnimatorController)
return null;
var layerID = GetLayerIDByName(animator, layerName);
var ac = animator.runtimeAnimatorController as AnimatorController;
var states = ac.layers[layerID].stateMachine.states;
if (null == states || states.Length <= 0) return null;
for (int tCounter = 0, tLen = states.Length; tCounter < tLen; tCounter++)
{
var state = states[tCounter].state;
if (state.name == stateName)
return state;
}
return null;
}
public static int GetLayerIDByName(Animator animator, string layerName)
{
AnimatorController ac = animator.runtimeAnimatorController as AnimatorController;
for (int i = 0; i < ac.layers.Length; i++)
{
if (ac.layers[i].name == layerName)
{
return i;
}
}
return -1;
}