Last active
May 23, 2022 16:38
-
-
Save AlteredFuture/fbae939155450eacd6d7a1da382ff834 to your computer and use it in GitHub Desktop.
Unity Inspector History
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 System; | |
using System.Collections.Generic; | |
using UnityEditor; | |
using UnityEngine; | |
/// <summary> | |
/// To use this create the window and place it above your inspector window for best use | |
/// </summary> | |
public class UnityInspectorHistory : EditorWindow | |
{ | |
private int currentPoint; | |
private List<int> selectionIDs = new List<int>(); | |
private bool internalOperation; | |
[MenuItem("Tools/UnityInspectorHistory/History Window")] | |
static void Init() | |
{ | |
UnityInspectorHistory window = (UnityInspectorHistory) EditorWindow.GetWindow(typeof(UnityInspectorHistory)); | |
window.Show(); | |
if (Selection.instanceIDs.Length != 0) | |
Selection.instanceIDs = new int[] {Selection.instanceIDs[0]}; | |
} | |
void OnGUI() | |
{ | |
GUILayout.Label("History", EditorStyles.boldLabel); | |
EditorGUI.BeginChangeCheck(); | |
string[] history = new String[selectionIDs.Count]; | |
for (var i = 0; i < selectionIDs.Count; i++) | |
{ | |
string name = ""; | |
if (EditorUtility.InstanceIDToObject(selectionIDs[i]) != null) | |
{ | |
name = $"{i}: {EditorUtility.InstanceIDToObject(selectionIDs[i]).name}"; | |
} | |
else | |
{ | |
name = $"{i}: unknown"; | |
} | |
history[i] = name; | |
} | |
if (selectionIDs.Count == 0) return; | |
currentPoint = EditorGUILayout.Popup(currentPoint, history); | |
if (EditorGUI.EndChangeCheck()) | |
{ | |
internalOperation = true; | |
if (selectionIDs.Count != 0) | |
Selection.instanceIDs = new int[] {selectionIDs[currentPoint]}; | |
Repaint(); | |
} | |
if (GUILayout.Button("clear")) | |
{ | |
if (selectionIDs.Count != 0) | |
selectionIDs = new List<int> {selectionIDs[currentPoint]}; | |
currentPoint = 0; | |
Repaint(); | |
} | |
GUILayout.BeginHorizontal(); | |
GUILayout.FlexibleSpace(); | |
if (GUILayout.Button("<", GUILayout.Width(EditorGUIUtility.currentViewWidth / 2))) | |
{ | |
Back(); | |
} | |
GUILayout.FlexibleSpace(); | |
if (GUILayout.Button(">", GUILayout.Width(EditorGUIUtility.currentViewWidth / 2))) | |
{ | |
Forward(); | |
} | |
GUILayout.FlexibleSpace(); | |
GUILayout.EndHorizontal(); | |
} | |
void OnSelectionChange() | |
{ | |
if (Selection.instanceIDs == null || Selection.instanceIDs.Length == 0) return; | |
int sel = Selection.instanceIDs[0]; | |
if (selectionIDs.Count == 0) | |
{ | |
selectionIDs.Add(sel); | |
currentPoint = 0; | |
} | |
else if (sel != selectionIDs[selectionIDs.Count - 1] && !internalOperation) | |
{ | |
if (currentPoint < selectionIDs.Count - 1) | |
{ | |
var lastSelected = selectionIDs[currentPoint]; | |
selectionIDs.RemoveRange(currentPoint, selectionIDs.Count - currentPoint); | |
selectionIDs.Add(lastSelected); | |
currentPoint = selectionIDs.Count - 1; | |
} | |
selectionIDs.Add(sel); | |
currentPoint = selectionIDs.Count - 1; | |
} | |
Repaint(); | |
internalOperation = false; | |
} | |
public void Back() | |
{ | |
internalOperation = true; | |
if (currentPoint > 0) | |
currentPoint -= 1; | |
if (selectionIDs.Count != 0) | |
Selection.instanceIDs = new int[] {selectionIDs[currentPoint]}; | |
Repaint(); | |
} | |
private void Forward() | |
{ | |
internalOperation = true; | |
if (currentPoint < (selectionIDs.Count - 1)) | |
{ | |
currentPoint += 1; | |
} | |
if (selectionIDs.Count != 0) | |
Selection.instanceIDs = new int[] {selectionIDs[currentPoint]}; | |
Repaint(); | |
} | |
#region Hotkeys | |
[MenuItem("Tools/UnityInspectorHistory/Back _&LEFT", false, 0)] // ALT + LEFT, | |
private static void BackHK() | |
{ | |
UnityInspectorHistory window = (UnityInspectorHistory) EditorWindow.GetWindow(typeof(UnityInspectorHistory)); | |
window.Back(); | |
} | |
[MenuItem("Tools/UnityInspectorHistory/Forward _&RIGHT", false, 1)] // ALT + RIGHT. | |
private static void ForwardHK() | |
{ | |
UnityInspectorHistory window = (UnityInspectorHistory) EditorWindow.GetWindow(typeof(UnityInspectorHistory)); | |
window.Forward(); | |
} | |
#endregion | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment