Search Unity

"Set Default State" in AnimatorController in code?

Discussion in 'Animation' started by Straafe, Oct 8, 2019.

  1. Straafe

    Straafe

    Joined:
    Oct 15, 2012
    Posts:
    73


    Basically I am trying to do the above image but in script (Editor script OK, I do not need it at runtime). I want to create a blank state and then set it as default all within script. Is this possible? Normally you would accomplish this in the Animator Window by selecting "Set as Layer Default State" in the context menu.
     
    ModLunar likes this.
  2. Straafe

    Straafe

    Joined:
    Oct 15, 2012
    Posts:
    73
    Figured it out (cont is a reference to the AnimatorController you want to do this with):

    Code (CSharp):
    1. AnimatorStateMachine asm = cont.layers[0].stateMachine;
    2. AnimatorState newState = asm.AddState("Default Empty State");
    3. asm.defaultState = newState;
     
    Spicyruby and ModLunar like this.
  3. punixer

    punixer

    Joined:
    Oct 25, 2017
    Posts:
    2
    Thank you Straafe :)
    Its not working, i guess i must consider more things.
    I'll keep searching.
     
  4. Straafe

    Straafe

    Joined:
    Oct 15, 2012
    Posts:
    73
    Hi @punixer , it still works, but note that I am using this in an Editor script. What I did was use this in a script to add a context menu option so that I could right click on an AnimatorController and have an option to instantly add an empty state as the default state. Here is a video of how the script works:

    https://i.imgur.com/7V1yRhz.mp4

    And here is the script (just add it somewhere in your project and you should get the context menu):


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEditor;
    5. using UnityEditor.Animations;
    6.  
    7. public static class ControllerInitialState
    8. {
    9.  
    10.     [MenuItem("Assets/Set Initial State", false, 16)]
    11.     private static void SetInitialStates()
    12.     {
    13.         List<AnimatorController> conts = GetSelectedControllers();
    14.         if (conts != null && conts.Count > 0)
    15.         {
    16.             foreach (AnimatorController con in conts)
    17.             {
    18.                 SetInitialState(con);
    19.             }
    20.             Debug.Log("All selected controllers set with initial states");
    21.         }
    22.     }
    23.  
    24.     public static List<AnimatorController> GetSelectedControllers()
    25.     {
    26.         var conts = Selection.GetFiltered(typeof(AnimatorController), SelectionMode.Assets);
    27.         List<AnimatorController> animConts = new List<AnimatorController>();
    28.         if (conts.Length > 0)
    29.         {
    30.             foreach (var cont in conts)
    31.             {
    32.                 animConts.Add(cont as AnimatorController);
    33.             }
    34.             return animConts;
    35.         }
    36.         return null;
    37.     }
    38.  
    39.     private static void SetInitialState(AnimatorController cont)
    40.     {
    41.         Debug.Log("Setting initial state on... " + cont.name);
    42.  
    43.         AnimatorStateMachine asm = cont.layers[0].stateMachine;
    44.         AnimatorState newState = asm.AddState("Default State");
    45.         asm.defaultState = newState;
    46.  
    47.         Debug.Log("Animation controller initial state set!");
    48.     }
    49.  
    50.     [MenuItem("Assets/Set Initial State", true)]
    51.     static bool SetInitialStateValidation()
    52.     {
    53.         return Selection.activeObject.GetType() == typeof(AnimatorController);
    54.     }
    55.  
    56. }
    57.