- Home /
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 :)
Bye,
Jean
Could this be of any help?
http://forum.unity3d.com/threads/statemachinebehaviours-get-from-editor-script.312123/
Answer by Jason-RT-Bond · Apr 19, 2020 at 08:57 PM
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: https://docs.unity3d.com/ScriptReference/Animations.AnimatorController.FindStateMachineBehaviourContext.html
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[i] = controller.parameters[i].name;
}
serializedObject.Update ();
paramIndex = EditorGUILayout.Popup ("Parameter", paramIndex, paramNames);
serializedObject.ApplyModifiedProperties ();
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612035028im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Transition to multiple states from Entry Node in Unity 5 1 Answer
Mecanim transition retargeting and conditional exit to any state? 0 Answers
Having an Animator play Either/Both/None of two animations 1 Answer
Checking Transitions in Animator 0 Answers
Bool in animator is not changing state 0 Answers