Search code examples
c#unity-game-engineunity-editor

How to get a reference to the Animation Window?


I am animating a gameobject with the animator. I am matching the animation to a video I have overlade in the editor. At the moment I am having to adjust the video scrubber to get to the next position I want to keypoint, then calculate and change it in the animator. I would like to be able to control the animation scrubber with my video scrubber or vice versa.

The wiki has what I need on for the animation window but I'm unsure how/if I can access the animation window through a script.

Any help appreciated thanks.


Solution

  • In general it is a bit difficult to get a specific instance of a certain built-in editor window since you can have multiple of the same type open at the same time.

    However, just assuming there already is a certain AnimationWindow (CTRL + 6) window open you could simply do e.g.

    private AnimationWindow animationWindow;
    
    ...
    
    // If there is no AnimationWindow open this will also open one
    if(animationWindow == null) animationWindow = EditorWindow.GetWindow<AnimationWindow>();
    
    aniamtionWindow.frame = targetFrame;
    

    Just as a little demo

    using UnityEditor;
    using UnityEngine;
    
    public class Example : MonoBehaviour
    {
        [SerializeField] [Min(0)] private int frame;
    
        private AnimationWindow animationWindow;
    
        private void Update()
        {
            if(animationWindow == null) animationWindow = EditorWindow.GetWindow<AnimationWindow>();
    
            animationWindow.frame = frame;
        }
    }
    

    enter image description here

    Make sure to put any script that uses the UnityEditor namespace either in a folder called Editor or wrap according parts of the script with pre-processor tags (#if UNITY_EDITOR ... #endif)