- Home /
EditorWindow.OnSelectionChange() in scene view?
I'd like to be able to detect pretty much any user interaction within the editor (moving objects, saving the scene, etc) so that I could know when these changes are being made. I'd like to simply get a debug message right now when the scene is changed, such as if I select a different object in the hierarchy/scene view, I'd like to get a simple debug message saying "OnSelectionChange", but this doesn't happen:
using UnityEditor;
using UnityEngine;
[ExecuteInEditMode]
public class RealtimeStuff : EditorWindow {
void OnSelectionChange() {
Debug.Log("OnSelectionChange");
}
}
Is this possible with the scene/hierarchy view, or does this message only work with custom editor windows? What can I do to achieve what I want? Keep in mind I don't have any scripts on any gameobjects, I simply wish for this to work without being on an object (which as far as I know is impossible).
EDIT I know that OnSelectionChange doesn't get called when I select a different object, but I have all the other messages from EditorWindow with debug messages, so I should get something, right?
I have also added [CustomEditor(typeof(SceneView))]
above my class, and changed EditorWindow
to Editor
.
Answer by Bunny83 · Nov 27, 2015 at 05:22 PM
EditorWindow callbacks are only invoked when the window is open. If the class is everything you have it shouldn't do anything. Also the ExecuteInEditMode attribute is ment for MonoBehaviour instances. If you don't create an instance of your EditorWindow it can't possible do anything.
using UnityEditor;
using UnityEngine;
public class RealtimeStuff : EditorWindow
{
[MenuItem("Tools/RealtimeStuff")]
static void OpenWindow()
{
GetWindow<RealtimeStuff>();
}
void OnSelectionChange() {
Debug.Log("OnSelectionChange");
}
void OnGUI()
{
GUILayout.Label("This is some window content");
}
}
This allows you to actually open your editor window. While the window is open you should receive OnSelectionChange messages.
Alternatively you can use a class that is marked with InitializeOnLoad and add a callback to Selection.selectionChanged or any other callback in EditorApplication like hierarchyWindowChanged in the static constructor of the class.
[InitializeOnLoad]
public class Extension
{
static Extension()
{
Selection.selectionChanged += OnSelectionChange;
EditorApplication.hierarchyWindowChanged += OnHierarchyChanged;
}
static void OnSelectionChange()
{
Debug.Log("Selection changed");
// use the Selection class to determine which object(s) are selected
}
static void OnHierarchyChanged()
{
Debug.Log("Hierarchy changed");
}
}
Keep in mind that this class is loaded as soon as your project is opened in Unity.
Thanks, is there any way to detect if I select a different object within the scene view, and/or get its properties such as transform's position?
I was able to detect if I select an object in the hierarchy, but that new EditorWindow must be visible/docked. Can I make this possible without being docked?
Uhm, the selectionChanged event of the Selection class will be called whenever you select a different object. The Selection class is also used to deter$$anonymous$$e what object / objects are selected.