Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Custom Editor - Set component header height

Discussion in 'Scripting' started by SwinJR, Apr 17, 2023.

  1. SwinJR

    SwinJR

    Joined:
    Oct 1, 2020
    Posts:
    3
    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've attached an image showing what I mean.

    Thanks for any help

    EditorExample.jpg
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,951
  3. oscarAbraham

    oscarAbraham

    Joined:
    Jan 7, 2013
    Posts:
    431
    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:

    1. 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.

    2. 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.
     
    Last edited: Apr 17, 2023
    SwinJR likes this.
  4. SwinJR

    SwinJR

    Joined:
    Oct 1, 2020
    Posts:
    3
    Thanks Oscar, it seems that using UITK is the correct forward.

    I tested EditorGUIUtility.SetIconSize & using GUIStyles neither of which had any affect and as you mentioned is really just a workaround
     
  5. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    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.
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,620
  7. MHDante

    MHDante

    Joined:
    Aug 20, 2013
    Posts:
    10
    There's a separate function to write the small titlebars, it's called

    EditorGUILayout.InspectorTitlebar


    See here for details:
    https://docs.unity3d.com/ScriptReference/EditorGUILayout.InspectorTitlebar.html


    Consider the following code, it requires 2 dictionaries

    _editorCache

    _editorFoldout


    Code (CSharp):
    1.  
    2. if (!_editorCache.TryGetValue(prefabRef, out var editor))
    3.     editor = _editorCache[prefabRef] = CreateEditor(prefabRef);
    4. var isFoldout = _editorFoldout.TryGetValue(prefabRef, out var value) && value;
    5. var newFoldout = _editorFoldout[prefabRef] = EditorGUILayout.InspectorTitlebar(isFoldout, editor);
    6. if(newFoldout) editor.OnInspectorGUI();
    7.  
    8. if(newVal) editor.OnInspectorGUI();
     
    Last edited: Jul 17, 2023