Selection History for Unity's Editor
This small plug-in for Unity's editor makes it possible to easily navigate backward or forward through recently selected objects and view their properties in the editor's Inspector window. If you've ever had to edit the properties of several related objects, this plug-in can save quite a bit of time spent searching for something that was just selected.
This plug-in does not require a UI, so it uses the EditorSingleton
class that I discussed in a previous article to create a singleton instance whose fields are persisted across script recompilations (domain reloads). See the download section below to get it and other utilities.
// Copyright 2021 by Hextant Studios. https://HextantStudios.com |
The plug-in starts by initializing the singleton when the editor starts using the [InitializeOnLoadMethod]
attribute. This will result in the OnEnable()
handler being called which registers a callback for selectionChanged
notifications. Whenever the selection changes in the editor, the UpdateSelection()
method is called which calls the Add()
method to add the currently selected objects (if they differ) to the history. This method also removes any "newer" items in the history list should the user have moved back in the history.
Next, the plug-in uses the [MenuItem]
attribute to register two menu items with shortcut keys that move backward and forward in the history. The default key is F1
to move backward and Shift + F1
to move forward, but these can be changed in the code or in the Edit / Shortcuts dialog if need be. The Back()
and Forward()
methods are similar and simply move the _currentIndex
field backward or forward while handling any necessary cleanup in case an object has been deleted from the project.
Supporting a history that contains entries with multiple objects is a little more complicated mainly because Unity's serialization does not support lists or arrays of lists or arrays. One simple solution is to create a [Serializable]
wrapper for the array such as the Objects
struct above, and the _history
field can now be a List<Objects>
which will properly serialize between domain reloads.
Finally, the two helper methods IsNull()
and AreEqual()
are used to properly check for entries that may contain references to deleted objects. To understand why this is needed, suppose the history contained two entries: [{Object1}, {Object1,Object3}]. If Object3 was later deleted, it would leave two adjacent entries that essentially contain just Object1 which would require pressing Back twice to move past.
Well, that's pretty much the extent of it! Thanks to the EditorSingleton
base class which derives from ScriptableObject
, the _history
and _currentIndex
field values will be preserved across a domain reload without any special handling. If you happen to run into any issues or have a question, please leave a comment below.
Download
- SelectionHistory.zip (
EditorSingleton
andSelectionHistory
only) - Hextant - Utilities (Git Repository & Package URL)