- Home /
Creating a hierarchial GameObject Browser
I am trying to create an object browser for GameObjects in Unity. The user should be able to see the main parent object originally. If they select the main parent, they should be able to see the children (and so on and so forth). There should be some indicator of what objects have children (a toggle button, little arrow, etc). Every child of the selected parent should be given space and the things below them should be moved down. The problem I encounter is that formatting is very tricky (accessing children/parents is not the problem). I cannot use Windows.Forms because this project is supposed to be platform independent. How can I create a hierarchical object browser? For reference, the hierarchy view in the Unity window is a perfect example of what I am trying to do.
For some background, I have attempted to use buttons, but the code I am writing is being called from OnGUI and thus is called continuously. The effects of a button click get overwritten instantly. I am currently using toggle buttons in a recursive function that calls itself every time it encounters an object with children. It does not create toggle buttons for all children, just the first.
This is the code I have so far (I know there are a few errors in it currently): public struct member { public int level;
public member (List<bool> bools, int index, string name, int lev) { GUILayout.BeginHorizontal (); bools[index] = GUILayout.Toggle (bools[index], "", GUILayout.MaxWidth (15)); bools[index + 1] = GUILayout.Toggle (bools[index + 1], name, GUI.skin.GetStyle ("label")); level = lev; GUILayout.EndHorizontal (); }
}
void showChildren (GameObject parent, member parM) {
int level = parM.level; foreach (Transform child in parent.transform) { count += 2; Rect r = new Rect ((30 * level+1)+ 10*(count/2), (40 * level+1) + 20*(count/2), 350 - (10 * level), 350 - (20 * level)); GUILayout.BeginArea (r); toggles.Add (false); toggles.Add (false); member childT = new member (toggles, count, child.name, parM.level); if(child.transform.GetChildCount() != 0){ showChildren(child.gameObject, childT); } GUILayout.EndArea (); }
}
So you're essentially trying to create an 'in-game' version of the hierarchy view - is that correct? As far as the effects of button clicks being 'overwritten instantly', it's hard to say without seeing your code, but it sounds like you're not saving the current state of the hierarchy display correctly (or maybe at all).
Yes, that is correct. The "overwriting" effect is due to the fact that button actions are only done when a button is pressed. So, the button is pressed and (for example) the text is written. Then, OnGUI is called, the button is not pressed, the text is not written and the effect of the button press is lost.