Created
May 31, 2018 09:34
-
-
Save DanPuzey/bb0fdbd7740fce6ce54f1c4486a2f0ce to your computer and use it in GitHub Desktop.
Unity3d editor window to show recent selections
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.Collections.Generic; | |
using System.Linq; | |
using UnityEditor; | |
using UnityEngine; | |
public class SelectionHistory : EditorWindow | |
{ | |
private const int NumberOfItemsToKeep = 6; | |
private const string MultiSelectPrefix = "+"; | |
private const int ItemsPerRow = 2; | |
private static readonly Color[] ItemColors = new Color[] { Color.green, Color.cyan, Color.red, Color.blue, Color.yellow, Color.magenta }; | |
private static int NextColorIndex = 0; | |
[MenuItem("BigRobot/Selection History")] | |
public static void ShowWindow() | |
{ | |
GetWindow(typeof(SelectionHistory)); | |
} | |
private class SelectionDetail | |
{ | |
public SelectionDetail(string name, int[] iDs) | |
{ | |
Name = name; | |
IDs = iDs; | |
Color = ItemColors[NextColorIndex++]; | |
if (NextColorIndex >= ItemColors.Length) | |
{ | |
NextColorIndex = 0; | |
} | |
} | |
public string Name { get; private set; } | |
public int[] IDs { get; private set; } | |
public Color Color { get; private set; } | |
} | |
private List<SelectionDetail> _selections = new List<SelectionDetail>(); | |
private void OnGUI() | |
{ | |
if (_selections.Count == 0) | |
{ | |
EditorGUILayout.LabelField("No selections yet..."); | |
} | |
else | |
{ | |
EditorGUILayout.BeginHorizontal(); | |
var count = 0; | |
foreach (var s in _selections) | |
{ | |
GUI.backgroundColor = s.Color; | |
if (GUILayout.Button(s.Name, GUILayout.ExpandWidth(false))) | |
{ | |
MoveToTop(s); | |
Selection.instanceIDs = s.IDs; | |
break; | |
} | |
if (++count % ItemsPerRow == 0) | |
{ | |
EditorGUILayout.EndHorizontal(); | |
EditorGUILayout.BeginHorizontal(); | |
} | |
} | |
EditorGUILayout.EndHorizontal(); | |
} | |
} | |
private void OnSelectionChange() | |
{ | |
var ids = Selection.instanceIDs; | |
var match = _selections.SingleOrDefault(s => s.IDs.SequenceEqual(ids)); | |
if (match == null) | |
{ | |
var isMultiSelect = ids.Length > 1; | |
var name = isMultiSelect ? string.Concat(MultiSelectPrefix, Selection.activeObject.name) : Selection.activeObject.name; | |
var sel = new SelectionDetail(name, ids); | |
AddNewItem(sel); | |
} | |
else | |
{ | |
MoveToTop(match); | |
} | |
Repaint(); | |
} | |
private void MoveToTop(SelectionDetail item) | |
{ | |
_selections.Remove(item); | |
_selections.Insert(0, item); | |
} | |
private void AddNewItem(SelectionDetail item) | |
{ | |
_selections.Insert(0, item); | |
_selections = _selections.Where(i => i != null).Take(NumberOfItemsToKeep).ToList(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment