- Home /
Get list of parameters of an AnimatorController
In Animator class, it is possible to set parameters using SetFloat, SetBool, SetInteger, etc..
Is it possible to know if a parameter exists before setting it?
Answer by chechoggomez · Jan 30, 2014 at 02:50 PM
For Unity 4.3, things have changed a bit, there is a new class called AnimatorControllerParameter which is used to get these parameters.
public Animator animator;
void Start()
{
AnimatorController animatorController = AnimatorController.GetEffectiveAnimatorController(animator);
int countParameters = animatorController.parameterCount;
AnimatorControllerParameter[] animationCParameter = new AnimatorControllerParameter[countParameters];
for (int i = 0; i < countParameters; i++)
{
animationCParameter[i] = animatorController.GetParameter(i);
Debug.Log("Parameter Name: " + animationCParameter[i].name);
if (animationCParameter[i].type == AnimatorControllerParameterType.Bool)
{
Debug.Log("Default Bool: " + animationCParameter[i].defaultBool);
}
else if (animationCParameter[i].type == AnimatorControllerParameterType.Float)
{
Debug.Log("Default Float: " + animationCParameter[i].defaultFloat);
}
else if (animationCParameter[i].type == AnimatorControllerParameterType.Int)
{
Debug.Log("Default Int: " + animationCParameter[i].defaultInt);
}
}
}
AnimatorController couldn't be found. By what ways, we can use it
@cheaster You have to use the UnityEditorInternal namespace:
using UnityEditorInternal;
Answer by ricardo_arango · Jul 17, 2013 at 06:09 PM
It's not possible (Unity 4.1) at runtime to query the list of parameters that an animator has. But it is possible at edit time using using the UnityEditorInternal namespace.
You can use these methods:
animatorController.GetEventCount(); //returns the count of parameters
animatorController.GetEventType(int index); //returns the index of the type(type -> see AnimatorControllerEventType)
//Gets the default values for parameters
animatorController.GetEventDefaultBool(int index);
animatorController.GetEventDefaultFloat(int index);
animatorController.GetEventDefaultInt(int index);
animatorController.GetEventDefaultVector(int index);
//Gets the name of the parameter
animatorController.GetEventName(int index);
Also there is a "Set" version of previous Methods.
You can create an Editor script that saves this data into an ScriptableObject saved as an asset or you could save the data in a script automatically using a CustomEditor inspector:
[CustomEditor(typeof( ClassHoldingAnimatorInfo ))]
public class ClassHoldingAnimatorInfoInspector : Editor
{
public override void OnInspectorGUI()
{
EditorGUIUtility.LookLikeInspector();
DrawDefaultInspector();
if (GUI.changed)
{
ClassHoldingAnimatorInfo classInstance = target as ClassHoldingAnimatorInfo;
Animator animator = classInstance.gameObject.GetComponent<Animator>();
AnimatorController animatorController = AnimatorController.GetAnimatorController (animator);
// get the data from the Animator using the functions from above
// and assign it to serialized variables in the classInstance
}
}
}
You can then query this data at runtime.
Answer by demented_hedgehog · Jul 05, 2015 at 03:10 AM
I suspect this part of the API is in flux at the moment. Here's code that works for me in 5.1.1f1 based on @chechoggomez post (and/or doesn't require the UnityEditorInternal).
AnimatorControllerParameter param;
for (int i = 0; i < anim.parameters.Length; i++) {
param = anim.parameters[i];
Debug.Log("Parameter Name: " + param.name);
if (param.type == AnimatorControllerParameterType.Bool){
Debug.Log("Default Bool: " + param.defaultBool);
}
else if (param.type == AnimatorControllerParameterType.Float){
Debug.Log("Default Float: " + param.defaultFloat);
}
else if (param.type == AnimatorControllerParameterType.Int){
Debug.Log("Default Int: " + param.defaultInt);
}
}
but this technique has a weird behaviour.
If you try to debug the length of params in an editor script, you will have the correct number dispalyed but as soon as you edit something in the Animator view (move a state, transition...), it will return 0 !!
@$$anonymous$$ike99 not sure what you're seeing. Perhaps editing the animator makes the current anim variable stale. In which case try grabbing a new reference each time: anim = gameObject.GetComponent();
In 5.1.3, I try this in the editor script but it only works in run times. (when using UnityEditorInternal getting error)
Your answer
Follow this Question
Related Questions
Javascript to control parameters of animator controller 3 Answers
How to get fileID information so i can just edit Mecanim Animator through script? 2 Answers
Can the mecanim animators Controller var be set in code? 1 Answer
Mecanim Runtime Controller is Private! 4 Answers
Skills controlled by animation (event callbacks and parameters) 0 Answers