Last active
August 27, 2018 16:19
-
-
Save JLChnToZ/f6be934574de8b5b58cf7a66421fa92c to your computer and use it in GitHub Desktop.
Selection History for Unity Editor
This file contains 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
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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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