- Home /
Custom HUD Hierarchy in Editor Window
I am developing a game using Unity and since the beginning it was clear to me that I would implement a custom way to draw the HUD, which I have achieved easily, using a hierarchical structure of HUDObjects
. The way I have been implementing this structure is simply storing a list of children HUDObjects
in each HUDObject
and then recursively drawing them. This has worked out fine for me so far, but as the project is growing in size, I am finding it more and more tedious and confusing to navigate to each individual HUDObject
to traverse the HUD tree.
I have always been impressed by Unity's extendability, particularly with the Editor, so I looked into a creating a custom HUD Editor Window. I found that I could certainly draw the hierarchy out, using a series of Foldouts, but it didn't give me the level of interactivity I wanted. What I basically am looking to achieve the Unity's Scene Hierarchy Window, but for my custom HUDObjects
, where I can drag them around to reparent them in the heirarchy. I am simply wondering if this is at all possible and if someone could at least point me in the right direction to go with this, I would be very grateful.
Thanks for reading my wordy question!
Answer by RedSandStudios · Feb 20, 2014 at 12:37 PM
So as far as I can tell, there is no way to directly imitate the Hierarchy view. However, using a combination of a GUI drag-drop script by AngryAnt and Unity's DragAndDrop class I have managed to achieve something functional. I use AngryAnt's script to draw the actual hierarchy. It's a very simple system which uses the Unity Event functionality to detect when a drag is occurring and simply draw the offending draggable object relative to the mouse position. Because my HUDObjects
are ScriptableObjects
underneath, I store them as actual assets in the project, so to get them from the Project view into this hierarchy view I had to use Unity's DragAndDrop
class. Relatively simple to use, I only use the AcceptDrag
function, then simply iterate over the DragAndDrop.objectReferences
and add each HUDObject
into the hierarchy.
Answer by MakeCodeNow · Feb 19, 2014 at 04:41 PM
I just went through this for my latest asset store product where I wanted to have a tree control. It's totally possible, but you have to do almost everything yourself. There's no built in tree control widget, and even foldouts only go so far. I think there is a tree control widget you can buy on the asset store, but last I checked it was more targeted to in-game GUIs.
If it's just the number/complexity of the objects in the hierarchy that's a problem, you might find it more productive to just use the filtering/search options built into the scene hierarchy view.
Perhaps I should have waited slightly longer to not post this question as I have actually just found two answers! It seems Unity provides a DragAndDrop class for use with OnGUI, where you can set and get objects with the drag. Also, some clever guy called AngryAnt has put together a GUI drap-drop script. I will update this later with an answer to which is easiest to use/most useful.