Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Change default clip in animation window for animator

Discussion in 'Animation' started by Doddler, Aug 21, 2023.

  1. Doddler

    Doddler

    Joined:
    Jul 12, 2011
    Posts:
    268
    Short and simple, every time I click on a gameobject with an animator component, the animation window switches to show a default clip, as far as I can tell it always defaults to the first clip added to the animator. If I select any other game object and return, it switches back to that one clip, even if I was just editing a different one. I can't for the life of me figure out how to make it remember what the last selected clip was. I'm almost at the point where I'm considering building some ungodly editor utility that monitors the animation window and switches it back for you, but before I do that, is there something I'm missing on how to have it remember what clip I was viewing?
     
  2. Doddler

    Doddler

    Joined:
    Jul 12, 2011
    Posts:
    268
    Well, I just went ahead and made an editor utility that does it for me. In my case I already have an editor that I use to initially open the clip (would be a bit different otherwise), I added this code when a clip is initially opened:

    Code (CSharp):
    1.             var animationWindow = EditorWindow.GetWindow<AnimationWindow>();
    2.          
    3.             targetObject = animator.gameObject;
    4.             targetClip = clip;
    5.          
    6.             Selection.activeGameObject = targetObject;
    7.             animationWindow.animationClip = clip;
    8.  
    9.             if(!hasCreatedOnChangeEvent)
    10.                 Selection.selectionChanged += OnSelectionChange;
    11.             hasCreatedOnChangeEvent = true;
    Then, the OnSelectionChange callback looks like this:

    Code (CSharp):
    1.         private void OnSelectionChange()
    2.         {
    3.             if (targetClip == null || !EditorWindow.HasOpenInstances<AnimationWindow>())
    4.                 return;
    5.  
    6.             if (Selection.activeGameObject == targetObject)
    7.             {
    8.                 var animationWindow = EditorWindow.GetWindow<AnimationWindow>();
    9.                 if(animationWindow.animationClip != targetClip)
    10.                  animationWindow.animationClip = targetClip;
    11.             }
    12.         }
    When it's all in place, changing your selected game object and returning back to the initial object will swap back to the clip you previously opened it on. My use case is a bit unique since I only care about one object and one clip on that object which I select elsewhere, but you could probably expand it to keep track of the AnimationWindow clip and selected game object and make a cache to always remember what clip you had selected for each object. It will also forget whenever you recompile or reload the editor, something you could probably solve by throwing the data on a serialized object. A task for another day though.