StateMachineBehaviour: how to get runtimeAnimatorController from custom inspector

Hi,

I am looking to get the runtimeAnimatorController from a custom inspector of a StateMachineBehaviour, but I fail to find how to get it.

I don’t want to get it at runtime, I need to get it during editing.

Any hints will be appreciated :slight_smile:

Bye,

Jean

I know this is ancient but it came up first in my Googling so thought I should add a sort-of solution.

Note that this will get you the AnimatorController (in UnityEditor.Animations), NOT the RuntimeAnimatorController, which presumably only exists at runtime. In my case, I was looking to access the parameters of the animator and this can only be done via the AnimatorController anyway.


Refer to this doc:
[Unity - Scripting API: Animations.AnimatorController.FindStateMachineBehaviourContext][1]

They provide an example of this static call which can get you the StateMachineBehaviourContext, and they access the AnimatorState from that context. But this context also provides the AnimatorController just as easily!


Here is part of my use case, just getting the controller and displaying a pop-up selector which let’s you pick one of the parameters.

using UnityEditor;
using UnityEditor.Animations;
using UnityEngine;

[CustomEditor (typeof (SetParameter))]
public class SetParameterEditor : Editor {

    AnimatorController controller;
    int paramIndex;

    void OnEnable () {

        StateMachineBehaviourContext[] context = AnimatorController.FindStateMachineBehaviourContext (target as StateMachineBehaviour);
        if (context == null || context.Length == 0)
            return;

        controller = context[0].animatorController;
    }

    public override void OnInspectorGUI () {

        if (controller == null)
            return;

        string[] paramNames = new string[controller.parameters.Length];
        for (int i = 0; i < paramNames.Length; i++) {
            paramNames _= controller.parameters*.name;*_

}

serializedObject.Update ();

paramIndex = EditorGUILayout.Popup (“Parameter”, paramIndex, paramNames);

serializedObject.ApplyModifiedProperties ();
}
_*[1]: https://docs.unity3d.com/ScriptReference/Animations.AnimatorController.FindStateMachineBehaviourContext.html*_