I’m creating a custom Editor which draws Components which are stored on other GameObjects in the scene.
Currently I’m using DrawFoldOutInspector() to draw these components. This works well except for the headers of each component is huge. Is there a way to set the size the component headers so I can make them look like the default component headers?
I’ve also tried using DrawHeader() but still get these large headers.
I remember using DrawFoldOutInspector in the past and it didn’t look like that. So I’d probably call this a bug, but I think it wouldn’t be given a lot of priority if it was reported. Inspector UIs are moving to UI Toolkit these days, and DrawFoldOutInspector can’t show inspectors made with UI Toolkit.
Maybe you could use EditorGUIUtility.SetIconSize to reduce the size of the header’s icon to 16px as a work around. Just remember to store the previous icon size before with GetIconSize, and to restore the previous size after that.
Now, here are some alternatives that may work better for you than what you are doing:
If you want to do it with UITK to be more prepared for the future, you could use the InspectorElement. It doesn’t have a header, but it’s not too hard to make your own header for it if you want to. I have a FOSS library of UITK elements with some code that might serve you as an example. The element in the library is called ListOfInspectors, and the CreateHeader method makes a header that’s very similar to Unity’s component header.
Unity already has the ability to show an object’s inspector in a dedicated window. It can be shown from multiple places by right-clicking an Object/Asset and selecting “Properties…”. It can also be shown from code with EditorUtility.OpenPropertyEditor. Depending on circumstances, I tend to prefer UIs that let you open other inspectors in this way instead of nesting them; it can be a lot cleaner.
Sadly, I can’t bother right now with finding the proper solution in my code because I have a lot of it and it’s all over the place, because I have dozens of projects. But this is doable, you just need to experiment with the API. Many of the editor-side features simply lack documentation. The trick is, once you find a solution, it’s done and you forget about it. For that reason I don’t actually remember any of it.
There’s a separate function to write the small titlebars, it’s called
EditorGUILayout.InspectorTitlebar
See here for details:
Consider the following code, it requires 2 dictionaries
_editorCache _editorFoldout
if (!_editorCache.TryGetValue(prefabRef, out var editor))
editor = _editorCache[prefabRef] = CreateEditor(prefabRef);
var isFoldout = _editorFoldout.TryGetValue(prefabRef, out var value) && value;
var newFoldout = _editorFoldout[prefabRef] = EditorGUILayout.InspectorTitlebar(isFoldout, editor);
if(newFoldout) editor.OnInspectorGUI();
if(newVal) editor.OnInspectorGUI();