I am working on tools for the Unity editor which work with animations and have run into an impasse accessing vital information from Animator. The main issue is that I cannot find a way to access animation clip data. With the legacy Animation component I was able to access clips easily, as well as extract curve data.
Below is a utility method I wrote that attempts to extract clip information, however it doesn’t work. The main issue is that I cannot find a way in the API to get an AnimationClip object from an Animator state (even though one is clearly assigned in the editor). The closest thing I have found is the Motion object, which isn’t even documented. This at least gives me the name of the clip, so then I am attempting to look it up using the AnimationUtility. However, the AnimationUtility only works with the Animation component, so in this context returns null. This is where I am stuck.
My end goal is to jump to key points in an animation in the editor. My tactic is to get AnimationClipCurveData from an AnimationClip, though if there is another way to go about it I am all ears!
Does anyone know how I can make this work?
static public AnimationClip GetAnimationClip(GameObject obj, string track) {
AnimationClip clip = null;
string name = null;
if(obj StringUtil.IsNotNull(track)) {
Animator anim = obj.GetComponent<Animator>();
if(anim != null) {
bool found = false;
for(int x = 0; x < anim.layerCount; x++) {
UnityEditorInternal.AnimatorController ac = anim.runtimeAnimatorController as UnityEditorInternal.AnimatorController;
UnityEditorInternal.StateMachine sm = ac.GetLayer(x).stateMachine;
for(int i = 0; i < sm.stateCount; i++) {
UnityEditorInternal.State state = sm.GetState(i);
if(state.uniqueName == track) {
Motion m = state.GetMotion();
if(m != null) {
Debug.Log("Motion:"+m.name);
name = m.name;
found = true;
break;
}
}
}
if(found) break;
}
}
AnimationClip[] clips = GetAnimationClips(obj);
if(clips == null) {
Debug.Log("GetAnimationClip: "+obj.name+": no animation clips");
}
else {
foreach(AnimationClip c in clips) {
if(clip.name == name) {
clip = c;
break;
}
}
}
}
Debug.Log("GetAnimationClip: "+obj.name+":"+name);
return clip;
}
On a side note, I want to voice a bit of frustration that scripting Animator is a pain! While it’s awesome that Unity has incorporated these powerful new animation tools, the API is really lacking to fully utilize it. And what is there is poorly documented. Much of the code I’ve written to overcome these obstacles is using undocumented internal procedures, which is quite ugly and worrisome, but I have found no alternatives. I truly hope the Unity developers realize this shortcoming and expose more of the system for developers.
Thanks in advance for any help!