Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

Question Using UI Toolkit, how can I render the base inspector first, and then render my custom inspector?

Discussion in 'UI Toolkit' started by Deleted User, Feb 25, 2024.

  1. Deleted User

    Deleted User

    Guest

    Is there a simple way to render the base GUI (with my reference MonoBehaviour's serialized fields, etc.), and then render my custom inspector below it? Currently, I can only figure out how to do this by manually recreating the base GUI via serialized properties.

    Code (CSharp):
    1. // Returns the default inspector
    2. public override VisualElement CreateInspectorGUI()
    3. {
    4.     return base.CreateInspectorGUI();
    5. }
    6.  
    7. // Returns a custom inspector
    8. public override VisualElement CreateInspectorGUI()
    9. {
    10.     var customUI = _UXML.Instantiate();
    11.     var root = new VisualElement();
    12.     _root.add(customUI);
    13.     return root;
    14. }
    I tried caching base.CreateInspectorGUI, but that just returns null, which must be the value Unity looks for to see if it should render the base inspector.
     
    Last edited by a moderator: Feb 25, 2024
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    8,647
    SisusCo, Nad_B, Deleted User and 2 others like this.
  3. Nad_B

    Nad_B

    Joined:
    Aug 1, 2021
    Posts:
    730
    I don't know how the new UI Toolkit work, but you should use the VisualElement (root) you get from the base call, not create a new one, something like this:


    Code (CSharp):
    1. public override VisualElement CreateInspectorGUI()
    2. {
    3.     var baseRoot = base.CreateInspectorGUI();
    4.  
    5.     var customUI = _UXML.Instantiate();
    6.  
    7.     // Add your custom UI to the base root, as long as it accepts multiple children,
    8.     // if not you may need to wrap the base root with a VisualElement that accepts multi children.
    9.     baseRoot.add(customUI);
    10.  
    11.     return baseRoot;
    12. }
     
  4. Spy-Master

    Spy-Master

    Joined:
    Aug 4, 2022
    Posts:
    908
    The topic post already mentioned that Editor.CreateInspectorGUI returns null.

    Some sample code showing usage of freshly created objects as opposed to using base.CreateInspectorGUI:
    https://docs.unity3d.com/2023.3/Documentation/Manual/UIE-HowTo-CreateCustomInspector.html
     
    SisusCo and Nad_B like this.
  5. Nad_B

    Nad_B

    Joined:
    Aug 1, 2021
    Posts:
    730
    Thank you for the info! I still haven't used the UI Toolkit for custom editors yet, but I plan to switch to it for my next project, as I find the system like a practical mix of WPF, HTML/CSS and Android's AXML. Although for runtime UI, nothing beats the performance and productivity of Noesis.
     
    Last edited: Feb 25, 2024
  6. Deleted User

    Deleted User

    Guest

    I agree that this new system is a significant improvement. It really feels like working with HTML/CSS/Javascript (where C# is like the JS), and having the layout, styles and logic separated is very nice for organization.
     
    Nad_B likes this.