- Home /
Hierarchy in editor
I have a public string so it shows up in the editor, but how could I make it so that this string shows up under a menu, so for example:
> Menu
String
So the public string only shows up in the editor when I actually click on the black arrow next to the menu. Just like how the hierarchy works with parents and children. So I want the public string to be a child the menu 'folder'
Answer by ThomasVandenberghe · Nov 03, 2015 at 01:17 PM
You can use
[Header("Menu")]
public int myInt = 0;
This can be used to group variables together, but you won't be able to collapse the data.
Here's the unity link for reference http://docs.unity3d.com/ScriptReference/HeaderAttribute.html
You can also use
[Space(10)]
If you want some extra space between variables
Tyvm! This is an easy solution and grouping the variables was indeed what I wanted! Thank you for your answer!
Answer by Bunny83 · Nov 03, 2015 at 12:27 PM
The inspector is there to simply allow editing of the variables of an instance. There's no built-in functionality to somehow group member variables inside a class logically. However you can physically group several variables in a seperate serializable class.
// C#
[System.Serializable]
public class SomeData
{
public string someString;
public int someInt;
}
public class MyMonoBehaviour : MonoBehaviour
{
public SomeData data;
}
That way your class MonoBehaviour will show a field "data" which can be expanded.
You can also nest classes to create a hierarchy. It's also possible to make an array or List of a custom class. Just try it out.
Thanks, I'm not very familiar with GUI's or editor scripting so I think I'll just use the other guy's answer. Your answer does motivates me to take a good in-depth look at editor scripting, tyvm for your fast answer!
Use whatever you like ^^. I always forget about the (relatively new) Header attribute, probably because i never use it. It just makes the variable listing longer. In some cases it might be quite handy.
You don't need any editor scripting for what i said in my answer ^^. The above example will directly give you what you want. Even some of Unity's standard assets use this approach (the FPS controller as far as i remember).
In the inspector you will see a "data" foldout which can be collapsed / expanded. When expanded you will see the two members inside the "child" class.