• 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 k_Strannik · Feb 26, 2013 at 10:35 PM · mecanim

How to get current state name on Mecanim?

Hi.

How to get current state name on Mecanim? Is there a better solution than using AnimatorStateInfo.IsName() or AnimatorStateInfo.nameHash?

Thanks in advance!

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

5 Replies

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

Answer by Blubberfisch · Mar 29, 2016 at 03:07 PM

For performance reasons you should NOT use the names at runtime. Comparing an integer is MUCH faster than a string comparison. However, that said, it can be nice thing so have the name of a state for debugging.

What we are did in our project is to export the whole controller graph to XML with the editor. At runtime that XML is loaded again and allows us to map hashes back to names. We also have information about state machines, so be can easily check if are currently in some child state of a certain StateMachine, etc. Checkout Editor API docs here: Animations.AnimatorController I think we tied it in as an AssetModificationProcessor to generate the XML automatically whenever the controller graph changed.

If your controller graph is not that big and not that often changing, I would recommend to just build a Dictionary mapping fullPathHash back to the name. Something like this (untested). Note the use of #if UNITY_EDITOR to make sure that stuff is not used in a real build.

 #if UNITY_EDITOR
 public class DebugAnimationStates
 {
 
     private Dictionary<int, string> m_states = new Dictionary<int, string>();
 
     public void Init()
     {
         Add("Base.Attack");
         Add("Base.Dodge");
         Add("Base.Walk");
 
         // ...
 
     }
 
     public string Get(int _fullPathHash )
     {
         string name;
         if (m_states.TryGetValue(_fullPathHash, out name))
             return name;
 
         return "Unknown#" + _hash;
     }
 
     private void Add(string _stateName)
     {
         int hash = Animator.StringToHash(_stateName);
         m_states.Add(hash, _stateName);
     }
 }
 #endif

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 v01pe_ · May 20, 2016 at 08:47 AM 0
Share

Good idea, not quite what would be optimal, but the closest I found so far!

avatar image
0

Answer by liszto · Feb 27, 2013 at 01:16 AM

I think your second solution is a good one as written here (but I never compare both) :

http://answers.unity3d.com/questions/351534/how-to-get-current-state-on-mecanim.html

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 k_Strannik · Feb 27, 2013 at 10:44 PM 1
Share

Both these solutions are bad.

I need to get the name of each state. $$anonymous$$anually compare all states is irrational.

avatar image liszto · Feb 27, 2013 at 10:57 PM 3
Share

ok so please next time be clear on your goal you ask to have the name of current one and not all.

Then post this as a comment and not as an answer

Finally, double post. Please delete one then edit your first post with the last one and delete it too ;)

avatar image hawken · Mar 29, 2016 at 02:06 PM 0
Share

This is not an answer.

The linked code does not give the name of the current animation playing.

Specifically, this is not a variable defined anywhere and is error code

currentBaseState = anim.GetCurrentAnimatorStateInfo(0);

Theres no sample to get the name.

avatar image
0

Answer by k_Strannik · Mar 29, 2016 at 11:57 PM

@Blubberfisch Thanks for answer, but I long ago came up with a solution) I did to export controller graph data to prefab instead XML. My solution for unity3d 4: http://unity3d.ru/distribution/viewtopic.php?f=13&t=14440

I came up with an another solution for unity3d 5: http://unity3d.ru/distribution/viewtopic.php?f=105&t=31486#p205103 There add StateMachineBehaviour scripts to a states and to a controller for getting data from any state. There you need to set manually the desired data in each state.

For performance reasons you should NOT use the names at runtime.

Yes, I know. But I have no problem with performance. This is often surplus optimization for animations (in my judgment) because that such checks are rarely invoked.
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
1

Answer by vikitosss · May 18, 2016 at 02:09 PM

 var CurrStateName = "";
 
 function LateUpdate(){
     var anim = GetComponent(Animator);
 
     if( anim.GetCurrentAnimatorStateInfo(0).IsName("WALK")) { CurrStateName = "WALK"; }
     else if( anim.GetCurrentAnimatorStateInfo(0).IsName("RUN")) { CurrStateName = "RUN"; }
     else if( anim.GetCurrentAnimatorStateInfo(0).IsName("JUMP")) { CurrStateName = "JUMP"; }
     else{ CurrStateName = "IDLE"; }
 }
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 hawken · May 20, 2016 at 05:11 AM 0
Share

seems comparing is the only way.

getting the actual name as a string is not possible.

avatar image vikitosss · May 24, 2016 at 12:58 PM 0
Share

Yes, predefined only.

avatar image
0

Answer by BFS_Samuel · Apr 25, 2018 at 04:51 PM

There's a plugin on the asset store that lets you have access to the current state name. It can also be used to trigger events on Animator States with custom parameters, preview animator state in the scene, filter states more easily and much more!

Here's the link: https://www.assetstore.unity3d.com/#!/content/115408

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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

15 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

Related Questions

Mecanim how to stop animation from going sideways? 0 Answers

Animation Loop ends in jitter. 1 Answer

Mecanim clips transition options 0 Answers

Mecanim how get time of the animation 0 Answers

is it possible to extend Mecanim? 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