Skip to content

Instantly share code, notes, and snippets.

@lgarczyn
Last active January 8, 2024 11:42
Show Gist options
  • Save lgarczyn/709a6f40dba50375ec36b4bf599767cc to your computer and use it in GitHub Desktop.
Save lgarczyn/709a6f40dba50375ec36b4bf599767cc to your computer and use it in GitHub Desktop.
Unity Editor Script to select an animation in the AnimationWindow when a state is selected in the AnimatorWindow
// Copyright Fleeting Being
// MIT license - https://opensource.org/licenses/MIT
// Inspired by AstralByte's AutoHideUILayer
// https://www.reddit.com/r/Unity3D/comments/6yaiit/free_editor_script_to_automatically_hide_ui_layer/
using System.Linq;
using UnityEditor;
using UnityEditor.Animations;
using UnityEngine;
namespace Plugins.AutoSelectAnimation.Editor
{
/// <summary>
/// Automatically select the animation of the selected animator state
///<para></para>
/// Installation: put in any folder named "Editor". Requires Unity 5.2 or later (for Selection.selectionChanged).
/// </summary>
[InitializeOnLoad]
internal static class AutoSelectAnimation
{
/************************************************************************************************************************/
const string EnabledToggleMenuPath = "Edit/Auto Select Animation";
static bool _isEnabled;
/************************************************************************************************************************/
static AutoSelectAnimation()
{
EditorApplication.delayCall += () =>
{
_isEnabled = EditorPrefs.GetBool(EnabledToggleMenuPath, true);
if (!_isEnabled) return;
Selection.selectionChanged += OnSelectionChanged;
EditorApplication.playModeStateChanged += OnSelectionChanged;
};
}
/************************************************************************************************************************/
[MenuItem(EnabledToggleMenuPath, validate = true)]
static bool ValidateToggleEnabled()
{
Menu.SetChecked(EnabledToggleMenuPath, _isEnabled);
return true;
}
[MenuItem(EnabledToggleMenuPath, priority = 200)]
static void ToggleEnabled()
{
_isEnabled = !_isEnabled;
EditorPrefs.SetBool(EnabledToggleMenuPath, _isEnabled);
if (_isEnabled)
{
Selection.selectionChanged += OnSelectionChanged;
EditorApplication.playModeStateChanged += OnSelectionChanged;
OnSelectionChanged();
}
else
{
Selection.selectionChanged -= OnSelectionChanged;
EditorApplication.playModeStateChanged -= OnSelectionChanged;
}
}
/************************************************************************************************************************/
static void OnSelectionChanged()
{
// Check if the animation window is open.
if (!EditorWindow.HasOpenInstances<AnimationWindow>()) return;
// Get the animation window.
AnimationWindow window = EditorWindow.GetWindow<AnimationWindow>();
// Check if we have a window.
if (!window) return;
// Check that no sensitive operations are in progress.
if (window.playing || window.recording) return;
// Get the selected animator state's clip
AnimationClip selection =
Selection.objects.OfType<AnimatorState>()
.Select(s => s.motion)
.OfType<AnimationClip>()
.FirstOrDefault();
if (!selection) return;
window.animationClip = selection;
}
static void OnSelectionChanged(PlayModeStateChange sc)
{
OnSelectionChanged();
}
/************************************************************************************************************************/
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment