Last active
March 14, 2025 00:22
-
-
Save FleshMobProductions/74c1913a4f66191a9e12d621d2c525f4 to your computer and use it in GitHub Desktop.
Editor scripts to navigate between selections in Unity back (Ctrl + Alt + Z) and forward (Ctrl + Alt + Y). MenuItems can be found under "Edit/Selection - Navigate Back" and "Edit/Selection - Navigate Forward" and an EditorWindow can be found under "Window/Navigate Selection History". Place the scripts inside an "Editor" folder so they are exclud…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#if UNITY_2021_1_OR_NEWER | |
using UnityEngine; | |
using UnityEditor; | |
using UnityEditor.Overlays; | |
using UnityEditor.Toolbars; | |
using UnityEngine.UIElements; | |
namespace FMPUtils.Editor | |
{ | |
// https://docs.unity3d.com/Manual/overlays-custom.html | |
// Overlay EditorWindow to be available in all editor windows | |
// Editor icon reference: https://github.com/halak/unity-editor-icons | |
[Overlay(typeof(EditorWindow), "Selection History")] | |
public class SelectionBackwardsForwardsToolbar : ToolbarOverlay | |
{ | |
public const string Id = "navigate-selection-history"; | |
SelectionBackwardsForwardsToolbar() : base(NavigateBackwardButton.Id, NavigateForwardButton.Id) { } | |
[EditorToolbarElement(Id, typeof(EditorWindow))] | |
class NavigateBackwardButton : EditorToolbarButton | |
{ | |
public const string Id = "navigate-selection-history/Backward"; | |
private bool isInteractive; | |
public NavigateBackwardButton() | |
{ | |
this.text = "Back"; | |
this.icon = EditorGUIUtility.IconContent("Animation.PrevKey").image as Texture2D; | |
this.tooltip = $"Navigate the selection history backward {SelectionBackwardsForwardsNavigationMenuItem.NavigateBackwardsHotkeyText}"; | |
this.clicked += OnClick; | |
SelectionBackwardsForwardsNavigationMenuItem.historyOrSelectionChanged += UpdateVisibility; | |
UpdateVisibility(); | |
} | |
private void OnClick() | |
{ | |
if (isInteractive) | |
SelectionBackwardsForwardsNavigationMenuItem.NavigateSelectionBackwards(); | |
} | |
private void UpdateVisibility() | |
{ | |
isInteractive = SelectionBackwardsForwardsNavigationMenuItem.HasPreviousHistoryEntry; | |
style.visibility = new StyleEnum<Visibility>(isInteractive ? Visibility.Visible : Visibility.Hidden); | |
} | |
} | |
[EditorToolbarElement(Id, typeof(EditorWindow))] | |
class NavigateForwardButton : EditorToolbarButton | |
{ | |
public const string Id = "navigate-selection-history/Forward"; | |
private bool isInteractive; | |
public NavigateForwardButton() | |
{ | |
this.text = "Forw."; | |
this.icon = EditorGUIUtility.IconContent("Animation.NextKey").image as Texture2D; | |
this.tooltip = $"Navigate the selection history forward {SelectionBackwardsForwardsNavigationMenuItem.NavigateBackwardsHotkeyText}"; | |
this.clicked += OnClick; | |
SelectionBackwardsForwardsNavigationMenuItem.historyOrSelectionChanged += UpdateVisibility; | |
UpdateVisibility(); | |
} | |
private void OnClick() | |
{ | |
if (isInteractive) | |
SelectionBackwardsForwardsNavigationMenuItem.NavigateSelectionForwards(); | |
} | |
private void UpdateVisibility() | |
{ | |
isInteractive = SelectionBackwardsForwardsNavigationMenuItem.HasNextHistoryEntry; | |
style.visibility = new StyleEnum<Visibility>(isInteractive ? Visibility.Visible : Visibility.Hidden); | |
} | |
} | |
} | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks to WarpedImagination - The Toolbar overlay was mainly inspired by the video "How To LEVEL UP With Overlay Tools In Unity" on his Youtube channel.