Search Unity

  1. Unity 2019.1 beta is now available.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. We're looking for insight from anyone who has experience with game testing to help us better Unity. Take our survey here. If chosen to participate you'll be entered into a sweepstake to win an Amazon gift card.
    Dismiss Notice
  4. On February 28th the Feedback website will shut down and be redirected to the Unity forums. See the full post for more information.
    Dismiss Notice
  5. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  6. Unity 2018.3 is now released.
    Dismiss Notice
  7. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice

Current animator state name?

Discussion in 'Animation' started by Rodolfo-Rubens, Jun 9, 2015.

  1. jhughes2112

    jhughes2112

    Joined:
    Nov 20, 2014
    Posts:
    39
    Hey all. Waaaay up near the top was a runtime solution using reflection by @alpha-cast (which is awesome, thanks!). If you want to just collect all the state names, whether full or short, and do so in the editor, here's a quick inspector that shows you how to do it. Just drag the animator controller onto the inspector field and it'll collect all the states in your whole animator. What you do with it from this point on is up to you. (Obviously, change the name of MyObjType to a script you actually have somewhere... doesn't matter what.)

    Hope this helps someone!

    JH

    Code (CSharp):
    1. [CustomEditor(typeof(MyObjType))]
    2. class AnimInspector : Editor
    3. {
    4.     public UnityEditor.Animations.AnimatorController controller;
    5.    
    6.     private HashSet<UnityEditor.Animations.AnimatorStateMachine> statesVisited = new HashSet<UnityEditor.Animations.AnimatorStateMachine>();
    7.     private HashSet<string> stateNames = new HashSet<string>();
    8.  
    9.     private void RecurseStates(UnityEditor.Animations.AnimatorStateMachine asm)
    10.     {
    11.         statesVisited.Add(asm);
    12.  
    13.         foreach (var states in asm.states)  // collect state names, ignore collisions
    14.         {
    15.             stateNames.Add(states.state.name);
    16.         }
    17.         foreach (var childState in asm.stateMachines)
    18.         {
    19.             if (statesVisited.Contains(childState.stateMachine)==false)
    20.                 RecurseStates(childState.stateMachine);
    21.         }
    22.     }
    23.  
    24.     public override void OnInspectorGUI()
    25.     {
    26.         base.OnInspectorGUI();
    27.         UnityEditor.Animations.AnimatorController newController = (UnityEditor.Animations.AnimatorController)EditorGUILayout.ObjectField(new GUIContent("Animator Controller"), controller, typeof(UnityEditor.Animations.AnimatorController), false);
    28.         if (controller != newController)
    29.         {
    30.             controller = newController;
    31.  
    32.             // reset and retraverse all states
    33.             statesVisited.Clear();
    34.             stateNames.Clear();
    35.             if (controller!=null)
    36.             {
    37.                 foreach (var layer in controller.layers)
    38.                 {
    39.                     RecurseStates(layer.stateMachine);
    40.                 }
    41.             }
    42.         }
    43.  
    44.         foreach (string stateName in stateNames)
    45.         {
    46.             EditorGUILayout.LabelField(stateName);
    47.         }
    48.     }
    49.  
    50. }
    51.  
     
  2. ippdev

    ippdev

    Joined:
    Feb 7, 2010
    Posts:
    2,629
    Nice editor script . However your recommendation to use a reflection based method as though it is the cat's meow is not a good recommendation. Game devs should always have performance in mind and throw out their MSDN practices and work with the engine which marshalls C#..a scripting language.. into C++ at compile time. Reflection is ssslllooowwwww. https://jacksondunstan.com/articles/2972
     
  3. SilentSin

    SilentSin

    Joined:
    Jan 3, 2013
    Posts:
    156
    It's an editor script. Its runtime performance doesn't matter because it doesn't exist at runtime, that's why its called an editor script. And if you actually read the script, it's not even using any reflection.

    It seems like a rather clunky solution to me, but an editor script is exactly the place where inefficient operations like this and reflection should be performed.
     
  4. R1PFake

    R1PFake

    Joined:
    Aug 7, 2015
    Posts:
    208
    Im not 100% sure what you mean with this comment, but Unity doesn't compile C# into C++ at compile time unless you use IL2CPP (for example for mobile) but a "normal" desktop game is compiled as C# (IL) dll and the engine code itself (which is C++) calls the C# code at runtime.
     
  5. ippdev

    ippdev

    Joined:
    Feb 7, 2010
    Posts:
    2,629
    That is why it was said good editor script but pointing to a runtime solution was not optimal performance wise. I would use this script to gather the stateNames into a string builtin array and creating a stateBool matching bool builtin array. By passing the name of the next state into a loop and seeking the match of either bool or string as is suited to your triggering method you have only one current string stateName which stateNames index matches the index of the bool which is true and the loop turns all others false. Superfast and bulletproof.
     
  6. ippdev

    ippdev

    Joined:
    Feb 7, 2010
    Posts:
    2,629
    Thanks for clarifying the jargon. I admit. I am not a jargon guy. I am a programmer/technical artist/procedural animator. More like the backyard hotrod mechanic whose bondoed jalopy will outperform the factory trained mechanics every time in the quarter mile. .. Fact remains..C# is not the final leg of the journey before display..so whatever is in C# to my sensibilities is still scripting the C++ when all is said and done per frame. The less of this you are pipelining the better the performance.