Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Animation Controller Transitions missing and not saved

Discussion in 'Animation' started by Konfusius, Sep 20, 2016.

  1. Konfusius

    Konfusius

    Joined:
    Jan 3, 2013
    Posts:
    3
    Hi,

    I am using an editor script to automatically generate states and transitions in a sub state machine of an animation controller.
    But for some reason the transitions are not saved and are missing after a restart of the unity editor.
    And I have to run the script again after every restart, and it seems that the animation controller assets get corrupted somehow since after an update of Unity I had to delete and regenerate the assets, otherwise Unity would crash on loading the project.

    Any hints on what I might be doing wrong or solution are appreciated.
     
  2. DavidGeoffroy

    DavidGeoffroy

    Unity Technologies

    Joined:
    Sep 9, 2014
    Posts:
    542
    Transitions and states need to be saved somewhere.

    Internally, we do something like AssetDatabase.AddObjectToAsset and we add it to the parent asset (the AnimatorStateMachine, the AnimatorState, or the AnimatorController, depending on what asset type it is)

    You're free to use AssetDatabase.CreateAsset to save it to disk, or AssetDatabase.AddObjectToAsset to make it part of another file/asset, but it needs to be part of the asset database.

    Without more info, this is my best guess.
     
    neputanu and Mish like this.
  3. DavidGeoffroy

    DavidGeoffroy

    Unity Technologies

    Joined:
    Sep 9, 2014
    Posts:
    542
    Also, if you want your states, transitions and sub state machines to be hidden like ours, you need to set the HideFlags on them.

    To hide them in the hierarchy, you must set HideFlags.HideInHierarchy
     
    Mish likes this.
  4. Mish

    Mish

    Joined:
    Apr 23, 2010
    Posts:
    96
    I had a similar issue and found this thread very helpful.
    However, if the destination state for a created transition is changed through an editor script, the Animator window does not update the transition visually until either I re-enter the StateMachine or re-open the window. I have tried to get the window and repaint it and also refreshing the asset database, but none if these things work. Did anyone else also experience this and came up with a solution?
     
  5. invadererik

    invadererik

    Joined:
    Oct 31, 2010
    Posts:
    148
    Have you tried AssetDatabase.Refresh(); on the AnimationController ?
     
  6. DavidGeoffroy

    DavidGeoffroy

    Unity Technologies

    Joined:
    Sep 9, 2014
    Posts:
    542
    The simplest way to force a refresh would probably be something like stateMachine.transitions = stateMachine.transitions.

    I haven't tested it, but looking at the code happening behind the scenes, it should trigger a dirtying of the asset, and a refresh of the window.

    It's a wasteful way to do this, but it should get your UI updated.
    If you end up trying it out, please keep me posted.
     
  7. Harton

    Harton

    Joined:
    Dec 5, 2012
    Posts:
    20
    Hi,

    I'm having the same issue. I've tried with AssetDatabase.Refresh() and AssetDatabase.SaveAssets() without results.

    I have a script to make transitions between states for selected AnimatorController. When I run it works but when the editor is restarted or the controller is duplicated all transitions are wiped (only in the copy).

    I'm going to make and script for generate all this info in a new controller but before that I'm want to be sure if I can't do it modifying the controller.

    Code (CSharp):
    1. AnimatorController animator = Selection.activeObject as AnimatorController;
    2.  
    3. AnimatorStateMachine machine = animator.layers[0].stateMachine.
    4.  
    5. AnimatorState from= machine.states[0].state;
    6. AnimatorState to= machine.states[1].state;
    7.  
    8. AnimatorStateTransition animatorTransition = new AnimatorStateTransition();
    9. animatorTransition.AddCondition(AnimatorConditionMode.Equals, (int)id, "State");
    10. ...
    11. animatorTransition.destinationState = to;
    12.  
    13. from.AddTransition(to);
    14.  
    15.  
     
  8. NGC6543

    NGC6543

    Joined:
    Jun 3, 2015
    Posts:
    228
    This info should be on the Scripting Manual. Or am I missing a reference?
    I need more detailed information about this.

    Currently I'm having this issue : https://forum.unity.com/threads/ani...ult-animatorstatemachine.307873/#post-3524106

    And I tried to save the transitions by adding it to asset.

    But instead I have this :
    스크린샷 2018-06-07 21.19.05.png

    So, how should I have to save those transitions and motions into an AnimatorController?

    ——————-
    + After I came home and taking shower, I realized that there was ‘hideinhierarchy’ option. Maybe that’ll do? Also @Mecanim-Dev shared this wonderful instruction so I’ll leave the link here :
    https://forum.unity.com/threads/ani...ult-animatorstatemachine.307873/#post-3524300
     
    Last edited: Jun 7, 2018
  9. Hemming

    Hemming

    Joined:
    Jan 18, 2017
    Posts:
    3
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using UnityEditor;
    4. using UnityEditor.Animations;
    5.  
    6. public class AnimatorTestUI : ScriptableObject
    7. {
    8.     [MenuItem("Custom/Make Demo Animator")]
    9.     static void CreateTestAnimator()
    10.     {
    11.         AnimatorController animatorController = AnimatorController.CreateAnimatorControllerAtPath("Assets/TestAnimator.controller");
    12.  
    13.         AnimatorStateMachine stateMachine = animatorController.layers[0].stateMachine;
    14.  
    15.         AnimatorState animatorState1 = stateMachine.AddState("state1", new Vector3(0, 100));
    16.         AnimatorState animatorState2 = stateMachine.AddState("state2", new Vector3(300, 100));
    17.  
    18.         stateMachine.entryPosition = new Vector3(-300, 50);
    19.         stateMachine.anyStatePosition = new Vector3(-300, -50);
    20.         stateMachine.exitPosition = new Vector3(600, 0);
    21.  
    22.         AnimatorStateTransition ast = new AnimatorStateTransition();
    23.         ast.hasExitTime = false;
    24.         ast.duration = 0;
    25.         ast.destinationState = animatorState2;
    26.  
    27.         animatorState1.AddTransition(ast);
    28.     }
    29. }
    30.  
    The code above, is a simple editor script that makes an animatorController with 2 states, and makes a transition from the first to the second.
    If you try executing this code yourself, you'll see the that the operation is a success! That is until you try exiting the project, and go back into it, then the transition will be lost :(

    The solution I found, was using another "AddTransition" method in the AnimatorState.

    My Solution:

    Code (CSharp):
    1.  
    2. void Example()
    3. {
    4.     AnimatorStateTransition ast = animatorState1.AddTransition(animatorState2);
    5.     ast.hasExitTime = false;
    6.     ast.duration = 0;
    7. }
    8.  
     
  10. DavidGeoffroy

    DavidGeoffroy

    Unity Technologies

    Joined:
    Sep 9, 2014
    Posts:
    542
    Here's the StateMachine code:
    https://github.com/Unity-Technologi.../master/Editor/Mono/Animation/StateMachine.cs

    AddTransition calls CreateTransition.

    If you look at what CreateTransition does:


    if (AssetDatabase.GetAssetPath(this) != "")
    AssetDatabase.AddObjectToAsset(newTransition, AssetDatabase.GetAssetPath(this));
    newTransition.hideFlags = HideFlags.HideInHierarchy;


    It makes sure that the transition is saved (because it is another asset).

    You can use the example code along with this, if for some reason you don't want to let the StateMachine create the asset for you.
     
  11. sregnault

    sregnault

    Joined:
    Jan 15, 2018
    Posts:
    6
    Thanks for the answers it helps me.
    I had the same issue with AnimatorState.
    It was because i did:
    Code (CSharp):
    1. var state = new AnimatorState() { name = "mystate" };
    2. stateMachine.AddState(state, pos);
    and I should do:
    Code (CSharp):
    1. stateMachine.AddState("mystate", pos);
    With this code, the state is saved in the controller

    Same with AnimatorStateMachine