Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
7
Question by TheAdam · Nov 08, 2013 at 02:49 PM · mecanim

is there a way to check if an animatorcontroller has a parameter?

With mecanim, if you try to set a parameter that doesn't exist, it logs a warning. It doesn't throw an exception or return null or anything useful like that. So if I set a parameter very often that doesn't exist, a lot of CPU time is spent logging warnings. How can I tell if a parameter exists in an animatorcontroller? It would be great if there was a method like ProceduralMaterial.HasProceduralProperty (which allows you to check if the ProceduralMaterial has the property in question).

(crossposted from gamedev.stackexchange)

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

11 Replies

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

Answer by drod7425 · Nov 08, 2013 at 03:53 PM

Unfortunately, there doesn't seem to be anything in place to check if a parameter exists. If you just want to turn off warnings to save CPU, set Animator.logWarnings = false (not in the scripting reference for some reason). If you want something that will do error handling, I wrote a hacky way to do it:

 void Awake(){
     animator.SetBool("nonExistant",true);
     if(!animator.GetBool("nonExistant")){
         Debug.LogError("The parameter nonExistant does not exist! Fix it!");
     }else{
         animator.SetBool("nonExistant",false);
     }
 }

Hope this helps!

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
25

Answer by thexdd · Aug 17, 2015 at 03:10 PM

I know its quite an old topic, but maybe somebody is still looking for any other options of how to deal with it. Here is my solution:

 public static bool HasParameter(string paramName, Animator animator)
 {
    foreach (AnimatorControllerParameter param in animator.parameters)
    {
       if (param.name == paramName)
          return true;
    }
    return false;
 }


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 saipavan4391 · May 04, 2018 at 08:35 AM 2
Share

it works! but produces garbage if many parameters are present in the animator controller

avatar image Bunny83 saipavan4391 · May 04, 2018 at 12:33 PM 2
Share

Ins$$anonymous$$d of using parameters you should use parameterCount and GetParameter in a normal "for" loop.

edit
Well I though this might help with garbage but it actually makes things worse ^^. I though that GetParameter is a direct accessing method for a single parameter. However under the hood GetParameter actually uses the parameters property so this is completely pointless.

Though as i said in my old comment the information if the animator has a certain parameter or not does not change over time. So getting this information at start and caching it would be the best approach.

avatar image
5

Answer by RChrispy · Sep 29, 2015 at 10:21 AM

I found the code of @inferno222 and @thexdd realy useful. So I made an extension real quick should work. But such calls should not be done in an Update! Store the outcome during a Start() in a variable and use this variable!!!

Just add it into your projekt I use a lot of extensions.

 public static bool ContainsParam(this Animator _Anim, string _ParamName)
 {
     foreach (AnimatorControllerParameter param in _Anim.parameters)
     {
         if (param.name == _ParamName) return true;
     }
     return false;
 }

Use like this:

 void start() {
 tOutcome =  myAnimator.ContainsParam("JumpCount");
 }

And then you can ask for:

 if( tOutcome ) {
 myAnimator.SetInteger("JumpCount", JumpCount);
 }
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 stjernerlever · May 10, 2019 at 08:44 PM 0
Share

How do you use that extension?

When I enter

  public static bool ContainsParam(this Animator _Anim, string _ParamName)
  {
      foreach (AnimatorControllerParameter param in _Anim.parameters)
      {
          if (param.name == _ParamName) return true;
      }
      return false;
  }

I get this error:

Extension method must be defined in a non-generic static class. Do you have a separate script that defines a public static bool ContainsParam?

alt text

untitled.png (20.2 kB)
avatar image rufreakde stjernerlever · May 10, 2019 at 09:04 PM 0
Share

You just have to define the extension in a seperate file. It is C# thing to extend existing classes horizontically.

Here is a tutorial: https://unity3d.com/de/learn/tutorials/topics/scripting/extension-methods

avatar image
1

Answer by inferno222 · Aug 20, 2015 at 12:54 AM

A slightly shorter answer is:

  public static bool HasParameter(string paramName, Animator animator)
  {
     foreach (AnimatorControllerParameter param in animator.parameters)
     {
        if (param.name == paramName) return true;
     }
     return false;
  }

Comment
Add comment · Show 1 · 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 thexdd · Aug 20, 2015 at 05:58 AM 0
Share

thanks, you're right, I've updated my answer. Sometimes I just should really fall asleep rather than code anything :D

avatar image
1

Answer by mkgame · Jan 12, 2019 at 09:22 PM

I would recommend to speed up this check with a Dictionary:

         Dictionary<string, bool> animNames = new Dictionary<string, bool>();
         
         void Awake() {
              animNames.Add("Attack", false);
              animNames.Add("Run", false);
              animNames.Add("Walk", false);
         
              foreach (AnimatorControllerParameter param in MyAnimator.parameters) {
                  if (animNames.containsKey(param.Name)) {
                        animNames[param.Name] = true;         
                  }
              }
 
             // To check then the known param.
             if (animNames["Attack"] == true) {
                  // Attack param available, use it.
             }
         }
 
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
  • 1
  • 2
  • 3
  • ›

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

31 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

OnStateEnter isn't updating in the correct order with normal update? 0 Answers

Mecanim transition condition can't have both bool and trigger? 0 Answers

Is it possible to change root motion animations to in place? 3 Answers

Blender human meta-rig and Mecanim Mo-Cap error: Coppied Avatar Rig Configuration mis-match. 0 Answers

Serialization of Animator state 1 Answer


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