Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ricardo_arango · Jul 17, 2013 at 06:06 PM · animatorcontrollermecanim

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
7
Best Answer

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);
         }
     }
 }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image cheaster · Feb 17, 2014 at 06:37 AM 0
Share

AnimatorController couldn't be found. By what ways, we can use it

avatar image theBrandonWu · Apr 05, 2014 at 01:35 AM 0
Share

@cheaster You have to use the UnityEditorInternal namespace:

 using UnityEditorInternal;
avatar image
2
Wiki

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
2

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);
         }
     }

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Mike99 · Sep 13, 2015 at 07:56 AM 0
Share

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 !!

avatar image demented_hedgehog · Sep 14, 2015 at 03:27 AM 0
Share

@$$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();

avatar image 009 · Nov 16, 2015 at 02:45 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges