Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Find all Animator transitions

Discussion in 'Immediate Mode GUI (IMGUI)' started by FeastSC2, Jun 1, 2017.

  1. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    969
    I'm creating a 2d game and the animation transitions 's exit time and transition durations are set to numbers which I don't want i.e not 1 for exit time and 0 for transition duration.

    How to find and set all the transitions' settings (animation duration/ exit time) of a given animator programmatically?
     
  2. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    969
    Ok I found a decent way
    Code (CSharp):
    1.  
    2. var stateMachine = _animatorController.layers[0].stateMachine;
    3.             var states = stateMachine.states;
    4.  
    5.             List<AnimatorStateTransition> allTransitions = new List<AnimatorStateTransition>();
    6.             foreach (var state in states)
    7.             {
    8.                 allTransitions.AddRange(state.state.transitions);
    9.             }
    10.  
    11.             foreach (var transition in allTransitions)
    12.             {
    13.                 transition.duration = 0;
    14.                 transition.exitTime = 1;
    15.             }
    16.  
    17.  
    The _animatorController is something I feed to my Editor Window with:
    _animatorController = (AnimatorController)EditorGUILayout.ObjectField(_animatorController,
    typeof(AnimatorController), true);

    I don't know how to do this automatically every time a new transition is created, probably through events but I'm not familiar with that kind of stuff, so right now what I do is simply clean the specific Animator Controller.
    You could also get every Animation Controller in the project to do on every Animation Controller.
     
  3. OC_Raiz

    OC_Raiz

    Joined:
    Jul 4, 2016
    Posts:
    5
    thanks. what class are these "var's"?
    a bit unreadable
    _animatorController is it a "RuntimeAnimatorController"?
    or an "Animator"?
    Or something entirely else?

    Animator and RuntimeAnimatorController dont have a "layers" array.
     
  4. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    969
    _animatorController is AnimatorController (class).
     
  5. Ben_Iyan

    Ben_Iyan

    Joined:
    May 13, 2016
    Posts:
    203
    I know this is an old thread, but it was one of the first Google hits that I got. So, for anyone like @OC_Raiz that's interested, this is my code. Hope it helps.

    Code (CSharp):
    1. var controller = animator.runtimeAnimatorController;
    2.             var stateMachine = ((AnimatorController)controller).layers[layerIndex].stateMachine;
    3.             var states = stateMachine.states;
    4.  
    5.             List<AnimatorStateTransition> allTransitions = new List<AnimatorStateTransition>();
    6.             foreach (var state in states)
    7.             {
    8.                 allTransitions.AddRange(state.state.transitions);
    9.             }
    10.  
    11.             foreach (var transition in allTransitions)
    12.             {
    13.                 transition.duration = 0;
    14.                 transition.exitTime = 1;
    15.            }    
     
    AlexPisarev, CyrilGhys and mickeyband like this.
  6. olejuer

    olejuer

    Joined:
    Dec 1, 2014
    Posts:
    205
    I have build this utility MonoBehaviour that automatically modifies all transitions in OnValidate

    Code (CSharp):
    1.  
    2. [RequireComponent(typeof(Animator))]
    3. public class StateAnimator : MonoBehaviour
    4. {
    5.     private Animator _animator;
    6.  
    7. #if UNITY_EDITOR // put using UnityEditor statement in precompiler flags as well
    8.     private void OnValidate()
    9.     {
    10.         _animator = GetComponent<Animator>();
    11.         var animatorController = _animator.runtimeAnimatorController as AnimatorController;
    12.         if (animatorController == null) return;
    13.         foreach (AnimatorStateTransition transition in animatorController.layers
    14.             .SelectMany(layer => layer.stateMachine.states.SelectMany(state => state.state.transitions)))
    15.         {
    16.             transition.duration = 0f;
    17.             transition.hasExitTime = false;
    18.         }
    19.     }
    20. #endif
    21. }
    22.  
     
    Last edited: Mar 11, 2023
    Malbers and CyrilGhys like this.
  7. gxp_mattias

    gxp_mattias

    Joined:
    Oct 5, 2021
    Posts:
    3
    Wait... all of these solutions that use the AnimatorController class wouldn't work in build, though... since they are using an extension of the UnityEditor class. Right?
     
  8. olejuer

    olejuer

    Joined:
    Dec 1, 2014
    Posts:
    205
    That's right, but they don't have to. OnValidate is only called in the editor, anyway. You'll have to make sure to wrap the code in #if UNITY_EDITOR precompiler flags, though, for the build to work. I'll edit the above snippet accordingly.