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.
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.
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?
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.
And I tried to save the transitions by adding it to asset.
But instead I have this :
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://discussions.unity.com/t/570455/7
using UnityEngine;
using UnityEditor;
using UnityEditor.Animations;
public class AnimatorTestUI : ScriptableObject
{
[MenuItem("Custom/Make Demo Animator")]
static void CreateTestAnimator()
{
AnimatorController animatorController = AnimatorController.CreateAnimatorControllerAtPath("Assets/TestAnimator.controller");
AnimatorStateMachine stateMachine = animatorController.layers[0].stateMachine;
AnimatorState animatorState1 = stateMachine.AddState("state1", new Vector3(0, 100));
AnimatorState animatorState2 = stateMachine.AddState("state2", new Vector3(300, 100));
stateMachine.entryPosition = new Vector3(-300, 50);
stateMachine.anyStatePosition = new Vector3(-300, -50);
stateMachine.exitPosition = new Vector3(600, 0);
AnimatorStateTransition ast = new AnimatorStateTransition();
ast.hasExitTime = false;
ast.duration = 0;
ast.destinationState = animatorState2;
animatorState1.AddTransition(ast);
}
}
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.