Search Unity

FillDefaultInspector() ignores [Header()] attributes

Discussion in 'UI Toolkit' started by marcospgp, Jun 29, 2022.

  1. marcospgp

    marcospgp

    Joined:
    Jun 11, 2018
    Posts:
    194
    I'm building a custom inspector using UI Toolkit and was sad to see that it's not possible to easily reproduce the default inspector in code.

    I followed the guide in the documentation, but ended up with a version of the default inspector which does not include the headers I specify as attributes.

    This is the default inspector:

    upload_2022-6-29_22-19-32.png

    And this is the default inspector as reproduced by UI Toolkit inside a custom inspector:

    upload_2022-6-29_22-20-7.png

    This is the code I am using:

    Code (CSharp):
    1. using UnityEditor;
    2. using UnityEditor.UIElements;
    3. using UnityEngine.UIElements;
    4.  
    5. namespace MarcosPereira.Terrain {
    6.     [CustomEditor(typeof(TerrainBuilder))]
    7.     public class TerrainBuilderInspector : Editor {
    8.         public VisualTreeAsset inspectorXML;
    9.  
    10.         public override VisualElement CreateInspectorGUI() {
    11.             // Create a new VisualElement to be the root of our inspector UI
    12.             var inspector = new VisualElement();
    13.  
    14.             // Load from default reference
    15.             this.inspectorXML.CloneTree(inspector);
    16.  
    17.             // Attach a default inspector to the foldout
    18.             InspectorElement.FillDefaultInspector(inspector, this.serializedObject, this);
    19.  
    20.             // Return the finished inspector UI
    21.             return inspector;
    22.         }
    23.     }
    24. }
    25.  
    Is there a way to make the custom inspector include the default inspector exactly as it should be?

    Thanks!
     
  2. HB-SpencerC

    HB-SpencerC

    Joined:
    Oct 31, 2013
    Posts:
    2
    A bit late to the party but... there is a hacky way to have headers appear.

    This problem actually depends entirely on what type of UI component FillDefaultInspector chooses for drawing the property directly below the Header. To be specific, if it draws a property with a Toolkit component like FloatField, there will be no header above it. If the function chooses to draw the same property with an IMGUIContainer, the header will appear.

    I happened across this by chance, because I've got members that also use the [Range] attribute, which seems to force using IMGUIContainer. I've submitted a bug report, but if you've got this problem and are willing to reorder and constrain your property, you can technically get the header.