- Home /
Question by
DarkMeatGames · Feb 08, 2015 at 04:44 PM ·
editorhierarchyevents
How can I detect events in the Hierarchy Window?
I'm trying to find a way to listen to events that fire in the Hierarchy Window. Specifically Duplicate and Paste events.
I can do it just fine in the scene view with the following code:
void OnSceneGUI()
{
Event e = Event.current;
if(e.commandName == "Duplicate" )
{
...
}
if( e.commandName == "Paste")
{
...
}
}
But there doesn't seem to be an equivalent 'OnHierarchyGUI'
I've tried playing around a bit with EditorApplication.hierarchyWindowItemOnGUI and EditorApplication.hierarchyWindowChanged callbacks, but I'm don't think there is a way to detect the specific event types that triggered those callbacks to happen.
Can anyone help me listen to duplicate and paste events in the hierarchy window?
Thanks!
Comment
Answer by KJanos1396 · Jun 24, 2016 at 01:21 PM
I have same problem and solved!
[CustomEditor(typeof(XObject))]
public class XObjectDuplicator : Editor
{
protected virtual void OnEnable()
{
EditorApplication.hierarchyWindowItemOnGUI += OnHierarchyGUI;
}
void OnSceneGUI()
{
Check();
}
public void OnHierarchyGUI(int instanceID,Rect selectionRect)
{
Check();
}
private void Check()
{
Event e = Event.current;
if(e != null && e.type == EventType.ValidateCommand && (e.commandName == "Paste" || e.commandName == "Duplicate"))
{
XObject obj = (XObject)Selection.activeObject;
if(obj)
{
//obj ...
}
}
}
}