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 Add (custom)Inspector to custom EditorWindow via CreateGUI

Discussion in 'Editor & General Support' started by Tzunamii, Oct 29, 2022.

  1. Tzunamii

    Tzunamii

    Joined:
    Nov 16, 2021
    Posts:
    2
    Hi,
    I have a problem with displaying an (custom)inspector for a Scriptableobject in my EditorWindow. I searched around the web and found one or two solutions. However, the solutions refer to display via OnGUI(). But i create my EditorWindow via CreateGUI();

    Actual State
    I'm currently using the TwoPaneSplitView with a leftPane and rightPane as VisualElement. On the left side i have a ListView they lists all Items from a normal List<Item> (Item --> ScriptableObjects). When you click on an Item in this ListView you get on the rightPane the inspector to edit this scriptableobject.

    CustomInspector
    Code (CSharp):
    1. [CustomEditor(typeof(Item))]
    2. public class ItemInspector : Editor
    3. {
    4.  
    5.     public override VisualElement CreateInspectorGUI()
    6.     {
    7.         VisualElement customInspector = new VisualElement();
    8.  
    9.         VisualElement group = new VisualElement();
    10.         group.style.flexDirection = FlexDirection.Row;
    11.  
    12.         Label label = new Label("Itemname");
    13.         TextField field = new TextField();
    14.         field.name = "Itemname";
    15.         field.bindingPath = "itemName";
    16.         field.style.flexGrow = 1;
    17.  
    18.         group.Add(label);
    19.         group.Add(field);
    20.  
    21.         customInspector.Add(group);
    22.  
    23.         return customInspector;
    24.     }
    25. }

    With OnGUI()
    With OnGUI() this seems to work fine:

    actualStateViaONGUI.png

    Code (CSharp):
    1. Editor editor;
    2.  
    3. private void OnItemSelectionChange(IEnumerable<object> selectedItems)
    4. {
    5.      editor = Editor.CreateEditor(selectedItems.First() as Item);
    6. }
    7.  
    8. public void OnGUI()
    9. {
    10.     if (editor != null)
    11.     {
    12.         editor.DrawDefaultInspector();
    13.     }
    14. }
    With CreateGUI()
    With CreateGUI() the editor build my CustomInspector but it seems to has actual no binding. My custom inspector works fine when i use it conventionally via Unity.

    actualStateViaCREATEGUI.png

    Code (CSharp):
    1. private void OnItemSelectionChange(IEnumerable<object> selectedItems)
    2. {
    3.     rightPane.Clear();
    4.     Editor e = Editor.CreateEditor(selectedItems.First() as Item);
    5.     rightPane.Add(e.CreateInspectorGUI());
    6. }
    What im doing wrong here?

    Unfortunately, this whole issue is still very poorly documented. If you find something at all, then only via displaying using OnGUI. I hope realy, I wouldnt have to change everything to OnGUI. I realy like to code via the CreateGUI-Methods.

    btw: What are the difference between displaying things via OnGUI and CreateGUI ? Whats newer?
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,003
    OnGui is what you use to make UI using Unity's old IMGUI system. CreateGUI is to make UI using Unity's new UI Toolkit.

    Also if you want to display something like a scriptable object in the inspector, just use inspector element: https://docs.unity3d.com/ScriptReference/UIElements.InspectorElement.html
     
    Tzunamii likes this.
  3. Tzunamii

    Tzunamii

    Joined:
    Nov 16, 2021
    Posts:
    2
    Wow, thank you so much.
    Instead of Editor.CreateEditor, I've now done this with the InspectorElement. And it worked. Also with a CustomInspector.

    Ah okay. That's probably why it's so poorly documented?