Skip to content

Instantly share code, notes, and snippets.

@phosphoer
Last active November 1, 2020 06:41
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save phosphoer/7d1f674d2545a78dce9bf3534f796b97 to your computer and use it in GitHub Desktop.
Save phosphoer/7d1f674d2545a78dce9bf3534f796b97 to your computer and use it in GitHub Desktop.
Selection history for Unity Editor
// The MIT License (MIT)
// Copyright (c) 2020 David Evans @phosphoer
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
// To use - put this in an Editor/ folder within Assets/.
// Default hotkeys are alt+[ and alt+] for back / forward
[ExecuteInEditMode]
[InitializeOnLoad]
public class EditorSelectionHistory
{
private static List<List<Object>> s_historyStack;
private static int s_historyIndex;
private static bool s_ignoreNextSelectionChange;
private const int kHistoryLength = 100;
[MenuItem("Edit/Selection History: Back &[")]
public static void GoBack()
{
// Debug.Log("SelectionHistory: Pressed back");
if (s_historyIndex + 1 < s_historyStack.Count)
{
s_historyIndex += 1;
RestoreHistory(s_historyIndex);
}
}
[MenuItem("Edit/Selection History: Forward &]")]
public static void GoForward()
{
// Debug.Log("SelectionHistory: Pressed forward");
if (s_historyIndex > 0)
{
s_historyIndex -= 1;
RestoreHistory(s_historyIndex);
}
}
static EditorSelectionHistory()
{
// Debug.Log("SelectionHistory: Initialized");
s_historyStack = new List<List<Object>>();
s_historyIndex = 0;
Selection.selectionChanged += OnSelectionChanged;
}
private static void OnSelectionChanged()
{
if (s_ignoreNextSelectionChange)
{
s_ignoreNextSelectionChange = false;
return;
}
if (Selection.objects.Length > 0)
{
// Debug.Log($"SelectionHistory: {Selection.objects[0].name}");
StoreHistory();
}
}
private static void RestoreHistory(int historyIndex)
{
List<Object> oldSelection = s_historyStack[historyIndex];
// Don't add the next selection change to the history as its part of going back/forward
s_ignoreNextSelectionChange = true;
Selection.objects = oldSelection.ToArray();
// Debug.Log($"Restored history to index {historyIndex}");
}
private static void StoreHistory()
{
List<Object> selection = new List<Object>(Selection.objects);
// If we're storing new history and we're in the middle of the history stack,
// we will throw away everything in front of where we are and make this the new head
if (s_historyIndex > 0)
{
// Debug.Log($"Resetting history index from {s_historyIndex} to 0");
s_historyStack.RemoveRange(0, s_historyIndex);
s_historyIndex = 0;
}
s_historyStack.Insert(0, selection);
// Debug.Log($"History now has {s_historyStack.Count} items");
// Keep history from getting too long
while (s_historyStack.Count > kHistoryLength)
{
s_historyStack.RemoveAt(s_historyStack.Count - 1);
}
}
}
@phosphoer
Copy link
Author

This script tracks your selection history in the Unity Editor and provides menu items / hotkeys to navigate back and forward in your history. Handy for swapping between two objects you want to edit in the inspector!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment