Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Unity Inspector History
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
You can’t perform that action at this time.