Skip to content

Instantly share code, notes, and snippets.

@JLChnToZ
Last active August 27, 2018 16:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JLChnToZ/f6be934574de8b5b58cf7a66421fa92c to your computer and use it in GitHub Desktop.
Save JLChnToZ/f6be934574de8b5b58cf7a66421fa92c to your computer and use it in GitHub Desktop.
Selection History for Unity Editor
using UnityEditor;
using System.Collections.Generic;
public static class SelectionHistory {
private const string PREV_SELECTED_MENU = "Edit/Previous Selection %&LEFT";
private const string NEXT_SELECTED_MENU = "Edit/Next Selection %&RIGHT";
private const int SELECTION_HISTORY_MENU_PRIORITY = 200;
private const int MAX_HISTORY_COUNT = 10;
private static readonly LinkedList<int[]> prevSelected = new LinkedList<int[]>();
private static readonly Stack<int[]> nextSelected = new Stack<int[]>();
private static int[] currentSelected;
static SelectionHistory() {
currentSelected = Selection.instanceIDs;
Selection.selectionChanged += OnSelectionChanged;
}
private static void OnSelectionChanged() {
int[] selected = Selection.instanceIDs;
if (IsSelectionEquals(selected))
return;
if (currentSelected != null)
prevSelected.AddLast(currentSelected);
currentSelected = selected;
nextSelected.Clear();
while (prevSelected.Count > MAX_HISTORY_COUNT)
prevSelected.RemoveFirst();
}
[MenuItem(PREV_SELECTED_MENU, true)]
private static bool CheckPreviousSelectable() {
return prevSelected.Count > 0;
}
[MenuItem(NEXT_SELECTED_MENU, true)]
private static bool CheckNextSelectable() {
return nextSelected.Count > 0;
}
[MenuItem(PREV_SELECTED_MENU, priority = SELECTION_HISTORY_MENU_PRIORITY)]
public static void SelectPreviousSelected() {
if (!CheckPreviousSelectable()) return;
if (currentSelected != null)
nextSelected.Push(currentSelected);
currentSelected = prevSelected.Last.Value;
prevSelected.RemoveLast();
Selection.instanceIDs = currentSelected;
}
[MenuItem(NEXT_SELECTED_MENU, priority = SELECTION_HISTORY_MENU_PRIORITY)]
public static void SelectNextSelected() {
if (!CheckNextSelectable()) return;
if (currentSelected != null)
prevSelected.AddLast(currentSelected);
currentSelected = nextSelected.Pop();
Selection.instanceIDs = currentSelected;
}
private static bool IsSelectionEquals(int[] selected) {
if (selected == currentSelected)
return true;
if (selected == null || currentSelected == null)
return false;
if (selected.Length != currentSelected.Length)
return false;
for (int i = 0; i < selected.Length; i++)
if (currentSelected[i] != selected[i])
return false;
return true;
}
}
@JLChnToZ
Copy link
Author

JLChnToZ commented Aug 27, 2018

Similar to undo/redo, but will not affect modified changes, only for selections.
Usage: drop this file to editor/ folder, and it is ready to use.

Hotkeys:
Ctrl + Alt + : Previous Selection
Ctrl + Alt + : Next Selection

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