Get Animator in editor mode

Hello everyone. Is it possible to get Animator class while game is not in play mode ? I need to get info of selected transition via script, but can’t find a way to do it. Been googling for whole day and still nothing. Closest thing i got was obtaining AnimatorController via reflection, but i don’t know how to get Animator from it if it’s possible. I need to get properties from inspector window as seen in picture. Thanks.

I’m not sure I understand exactly what you are trying to do. However, all the information regarding AnimatorController is found in the UnityEditor.Animations namespace. So you will need to add

using UnityEditor.Animations;

To an editor script and you will be able to pull layers, state machines, states, transitions, etc. Hope that helps you out.

Cheers,
TrickyHandz

I found this code somewhere and it returns me AnimatorController shown in editor, it’s using Animations namespace already…

static AnimatorController GetCurrentController()
    {
        AnimatorController controller = null;
        var tool = EditorWindow.focusedWindow;
        var toolType = tool.GetType();
        var controllerProperty = toolType.GetProperty("animatorController", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
        if(controllerProperty != null)
        {
            controller = controllerProperty.GetValue(tool, null) as AnimatorController;
        }
        else
        {
            Debug.Log("EditorWindow.focusedWindow " + tool + " does not contain animatorController", tool);
        }
        return controller;
    }

The thing is that i don’t know how to get Animator class, not AnimatorController, so i could use something like this to get time of current animation/transition:

AnimatorStateInfo animationState = anim.GetCurrentAnimatorStateInfo(0);
AnimatorClipInfo[] myAnimatorClip = anim.GetCurrentAnimatorClipInfo(0);
myTime = (myAnimatorClip[0].clip.length * animationState.normalizedTime).ToString();

I need to know the time of slider during transition so i can see it in custom editor window and later set event to trigger at that time.

1 Like

I misunderstood what you were looking for. The Animator is the runtime interface to the AnimatorController, so you will need to get a reference to the GameObject that owns the Animator component and then check for the component like this:

Animator anim = gameObject.GetComponent<Animator>();

However, what you are looking for is seems to be information about the state of the transition editor window…not the transition itself. I’m not sure how you would get that information without some serious reflection into the editor itself.

EDIT: Nevermind, it was all in front of me, current time and frame can be seen in transition preview window… Thanks for help anyway @TrickyHandz :slight_smile: