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
6
Question by fherbst · Mar 17, 2013 at 08:00 PM · animationmecanim

Getting a list of mecanim states in Animator

I was just getting started with mecanim today. I really like the way it's set up, but a few questions arose:

  • how can I just trigger an animation (e.g. "Dying") and fade it in immediately?

  • this unity doc page states that you should "Implement a small AI layer to control the Animato. You can make it provide simple callbacks for OnStateChange, OnTransitionBegin etc...". I think events are very much needed for fine-grained animation control, but I have no idea how to implement that given my third question:

  • how to get a list of states/transitions (and their names/hashes) from an Animator?

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

9 Replies

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

Answer by TonyLi · May 07, 2013 at 03:26 PM

  1. To trigger an animation immediately from anywhere, create a transition from Any State to your Dying state. Create a bool parameter called "Dying" and set the transition to trigger whenever the Dying parameter is true. Then in your control script call animator.SetBool("Dying", true). (Side note: Per http://docs.unity3d.com/Documentation/Manual/MecanimPeformanceandOptimization.html, using a hash is better than the string "Dying").

  2. Mecanim doesn't yet implement events. You can use curves. I was doing this for some animations and triggering an "event" whenever the curve crossed the zero-axis. But now I use G1NurX's Event System for Mecanim (http://u3d.as/content/g1nur-x/event-system-for-mecanim/3QP) -- well worth the cost.

  3. To my knowledge, Animator doesn't expose this information yet. You can get the current and next state, but not an enumeration of all states on any given layer. Hmm, it just occurred to me that G1NurX's product does this somehow. When I get back to the office I'll look into it.

Comment
Add comment · Show 14 · 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 TonyLi · May 07, 2013 at 11:23 PM 5
Share

Hey, there is a way! It doesn't appear to be documented, so there's no guarantee that a later version of Unity won't change things. But here's how to do it:

 // Get a reference to the Animator Controller:
 UnityEditorInternal.AnimatorController ac = GetComponent<Animator>().runtimeAnimatorController as UnityEditorInternal.AnimatorController;
         
 // Number of layers:
 int layerCount = ac.GetLayerCount();
 Debug.Log(string.Format("Layer Count: {0}", layerCount));
 
 // Names of each layer:
 for (int layer = 0; layer < layerCount; layer++) {
     Debug.Log(string.Format("Layer {0}: {1}", layer, ac.GetLayerName(layer)));
 }
         
 // States on layer 0:
 UnityEditorInternal.State$$anonymous$$achine sm = ac.GetLayerState$$anonymous$$achine(0);
 List<UnityEditorInternal.State> states = sm.statesRecursive; // Also: sm.states
 foreach (UnityEditorInternal.State s in states) {
     Debug.Log(string.Format("State: {0}", s.GetUniqueName()));
 }

In addition to State.GetUniqueName(), there's State.GetUniqueNameHash(), and for transitions:

  • transitionCount

  • transitions

  • GetTransitionsFromState()

  • etc.

avatar image fherbst · May 07, 2013 at 11:26 PM 0
Share

Wow, thanks for the elaborate answer and for actually looking into that again.

avatar image petrucio · Mar 03, 2014 at 01:34 AM 4
Share

Awesome stuff! This really should not be internal stuff.

The API has indeed changed, this is working for 4.3:

 // States on layer 0:
 UnityEditorInternal.State$$anonymous$$achine sm = ac.GetLayer(0).state$$anonymous$$achine;
 for (int i = 0; i < sm.stateCount; i++) {
     UnityEditorInternal.State state = sm.GetState(i);
     Debug.Log(string.Format("State: {0}", state.uniqueName));
 }
avatar image kayy · Jun 13, 2014 at 06:07 PM 1
Share

I created an open source asset called Animator Access Generator, that generates a C# class as $$anonymous$$onoBehaviour which contains the hash IDs of all states and parameters. So this information is available at runtime too.

See the forum posting. I generate a couple of convenience methods for checking states and accessing parameters and there is a StateIdToName method to get the clear text name for a hash ID.

Thanks to all the valuable comments here that inspired me :)

avatar image Oncle-Ben · Jul 10, 2015 at 07:23 PM 3
Share

Note that as of Unity 5 this now sits in UnityEditor.Animations, and is documented (http://docs.unity3d.com/ScriptReference/Animations.AnimatorController.html).

Show more comments
avatar image
9

Answer by MilanGM · Jul 15, 2015 at 10:45 AM

This is a method that I made for my game, and it works:

 void Start () {
     _animator = gameObject.GetComponent<Animator>();
 }

 public AnimationClip GetAnimationClip(string name) {
     if (!_animator) return null; // no animator
 
     foreach (AnimationClip clip in _animator.runtimeAnimatorController.animationClips) {
         if (clip.name == name) {
             return clip;
         }
     }
     return null; // no clip by that name
 }
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 NG_Matthieu · Oct 01, 2016 at 04:25 PM 1
Share

If you want could turn that method static so don't need to copy it around in classes:

 internal static AnimationClip GetAnimationClipFromAnimatorByName(Animator animator, string name)
     {
         //can't get data if no animator
         if (animator == null)
             return null;
 
         //favor for above foreach due to performance issues
         for (int i = 0; i < animator.runtimeAnimatorController.animationClips.Length; i++)
         {
             if (animator.runtimeAnimatorController.animationClips [i].name == name)
                 return animator.runtimeAnimatorController.animationClips [i];
         }
 
         Debug.LogError ("Animation clip: " +name + " not found");
         return null;
     }

also good to note: name of animation is not the name inside the Animator but of the Animation file itself

avatar image KakCAT2 · Sep 05, 2018 at 08:29 AM 0
Share

just a note. This method doesn't work when you put a bunch of animations with the same name (i.e. mixamo.com) because the clip name is "mixamo.com" but the states are renamed to "mixamo.com 0, 1, ..."

avatar image
3

Answer by akelsey · Apr 28, 2015 at 03:15 AM

This is now available inside unity. It returns all the animation clips used by an actor. It works on both legacy prefabs and mecanim prefabs.

AnimationUtility.GetAnimationClips(GameObject)

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 bjennings76 · Dec 01, 2016 at 02:22 AM

Here's the editor-time method I use to get an array of states from the Animator component:

 private static AnimatorState[] GetStateNames(Animator animator) {
     AnimatorController controller = animator ? animator.runtimeAnimatorController as AnimatorController : null;
     return controller == null ? null : controller.layers.SelectMany(l => l.stateMachine.states).Select(s => s.state).ToArray();
 }
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 frattapa · Jul 16, 2020 at 02:19 PM

I made an update working with the current APIs. I hope someone finds this helpful!

using UnityEditor.Animations;

 public void PrintStates()
         {
             // Get a reference to the Animator Controller:
             AnimatorController ac = _anim.runtimeAnimatorController as AnimatorController;
 
             // Number of layers:
             int layerCount = ac.layers.Length;
             Debug.Log(string.Format("Layer Count: {0}", layerCount));
 
             // Names of each layer:
             for (int layer = 0; layer < layerCount; layer++)
             {
                 Debug.Log(string.Format("Layer {0}: {1}", layer, ac.layers[layer].name));
             }
 
             // States on layer 0:
             AnimatorStateMachine sm = ac.layers[0].stateMachine;
             ChildAnimatorState[] states = sm.states; // Also: sm.states
             foreach (ChildAnimatorState s in states)
             {
                 Debug.Log(string.Format("State: {0}", s.state.name));
             }
         }
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
  • ›

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

26 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

Related Questions

Mecanim add weight to certain animations 0 Answers

3rd person gta like weapon problem?? 0 Answers

Mecanim Boolean 1 Answer

How to make FSM transition to mute with script ? 1 Answer

Swap Mecanim Layers in Unity 4.2 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