- Home /
Detect on Editor Mode drop prefab to the hierarchy
Hello,
I created one level design tool to create levels and I want to extend one feature. I have one feature that Adds an enemy wave to the current level, the game designer clicks "Add wave" button, it opens a panel (OpenFilePanel) to select the wave prefab and then in my editor script I load and instantiate the prefab as a child of the array object and add it to the correct wave array.
The new feature is to drag and drop the prefab from the "Project Window" to the array object on the "Hierarchy Window" and automatically add it to the array as I did before with the button + OpenFilePanel. I searched a lot for this feature from Unity but didn't find anything..
The game designer can do this by dragging the wave prefab to the hierarchy as a child of the array object but he has to switch to "Debug Mode" to add/drag manually the wave to the array and I want to skip this second process on the Debug mode.
The problem is that I need to identify when one prefab is dropped on the scene to check his parent and add it from the editor script to the array.
Another thing I would like to do, if I can detect the drag and drop operation, is to switch the order of the elements on the prefab. Currently I have two buttons on my editor script for each wave on the array to "move up" and "move down" on the array order but I also would like to do this when the game designer changes the order of the wave objects on the hierarchy. If I can detect a drop I can check the order of the objects on the hierarchy and fix it on the array.
There is a way to do almost this as I want, that is to have a button "Update waves" that checks the child objects of the array on the hierarchy and update the array, but still there will be a button for the game designer to press...
Thanks
Answer by winxalex · Oct 28, 2015 at 05:26 PM
This might give u an idea
Register callback handler
EditorApplication.hierarchyWindowChanged -= OnHierarchyChanged;
EditorApplication.hierarchyWindowChanged += OnHierarchyChanged;
//Callback handler:
private static void OnHierarchyChanged ()
{
if(Selection.activeGameObject!=null && PrefabUtility.GetPrefabType(Selection.activeGameObject)==PrefabType.PrefabInstance)
Debug.Log (Selection.activeGameObject);
[1]: http://docs.unity3d.com/ScriptReference/EditorApplication-hierarchyWindowChanged.html
How to easily distinguish between a mere re-ordering of an existing object in the scene VS a prefab has just been dropped in the scene now.