Search Unity

Question Need a non-IMGUI version of InspectorElement

Discussion in 'UI Toolkit' started by NarallandaKaratoga, Apr 21, 2023.

  1. NarallandaKaratoga

    NarallandaKaratoga

    Joined:
    Jul 11, 2017
    Posts:
    6
    I wish to show all component content in a customized editor window, than I have:
    EditorWindow creates VisualElement tree containing InspectorElement.
    InspectorElement needs to render my component.
    My component field with customized property drawer, which is implemented in UIElement style and works fine in normal unity inspector, shows "no GUI implemented".

    ith UI Toolkit Debugger I found IMGUI container in InspectorElement, so I think it's IMGUI which does not support nesting a VisualElement. So is there any workaround recently? Or latest version of InspectorElement has just implemented without IMGUI?

    (Unity Version 2022.2.10, should be new enough....)
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,923
    Perhaps you can try
    UnityEditor.CreateEditor
    , and get the return on
    CreateInspectorGUI
    ?
     
  3. stevphie123

    stevphie123

    Joined:
    Mar 24, 2021
    Posts:
    82
    You can wrap any IMgui in IMGuiContainer in uitoolkit like this

    Code (CSharp):
    1.            
    2. var edtr = UnityEditor.Editor.CreateEditor(gameObject);
    3. GUIStyle bgColor = new GUIStyle();
    4. bgColor.normal.background = EditorGUIUtility.whiteTexture;
    5. var rect = new Rect(prevWindow.contentRect.x, prevWindow.contentRect.y, prevWindow.contentRect.width * 2, prevWindow.contentRect.height);
    6. var t = new IMGUIContainer(() => edtr.OnInteractivePreviewGUI(GUILayoutUtility.GetRect(prevWindow.contentRect.width, prevWindow.contentRect.height), bgColor));
    7. t.style.backgroundColor = Color.white;
    8.  
    Copy pasted from mine, so you should clean it up a bit
     
  4. NarallandaKaratoga

    NarallandaKaratoga

    Joined:
    Jul 11, 2017
    Posts:
    6
    I tried this:
    Code (CSharp):
    1.  
    2.             var objects = inspectTarget.targetObjects;
    3.             var normalEditor = UnityEditor.Editor.CreateEditor(objects);
    4.             UnityEngine.Debug.Log(normalEditor);
    5.             var element = normalEditor.CreateInspectorGUI();
    6.             return element;
    7.  
    but doesn't work.
    CreateInspectorGUI
    method returns null on any
    GenericInspector
    object, as well as on
    TransformInspector
    .

    Edit: After a bit reading in InspectorElement source, It seems that this is the reason why InspectorElement uses a IMGUI container....
     
    Last edited: Apr 26, 2023
  5. NarallandaKaratoga

    NarallandaKaratoga

    Joined:
    Jul 11, 2017
    Posts:
    6
    After executing this:

    Code (CSharp):
    1.  
    2.             var objects = inspectTarget.targetObjects;
    3.             var edtr = UnityEditor.Editor.CreateEditor(objects);
    4.             GUIStyle bgColor = new GUIStyle();
    5.             bgColor.normal.background = EditorGUIUtility.whiteTexture;
    6.             var t = new IMGUIContainer(() => edtr.OnInteractivePreviewGUI(GUILayoutUtility.GetRect(400, 800), bgColor));
    7.             t.style.backgroundColor = new Color(0.4f, 0.4f, 0.4f, 1f);
    8.  
    the final structure I can discover in UIToolkit Debugger is still a IMGUI containter.
    I can see nothing on the white panel and I don't think it will work properly with CustomPropertyDrawer implemented by
    CreatePropertyGUI
    ...
     
  6. NarallandaKaratoga

    NarallandaKaratoga

    Joined:
    Jul 11, 2017
    Posts:
    6
    Found a solution with some code digging...
    Code (CSharp):
    1.  
    2. var rt = new ScrollView();
    3. var editor = UnityEditor.Editor.CreateEditor(inspectTarget.targetObjects);
    4. if(editor.GetType().Name != "GenericInspector")
    5. {
    6.     var element = editor.CreateInspectorGUI();
    7.     if(element != null)
    8.     {
    9.         rt.AddChild(element);
    10.     }
    11.     else
    12.     {
    13.         rt.AddChild(new IMGUIContainer(() => editor.OnInspectorGUI()));
    14.     }
    15. }
    16. else
    17. {
    18.     InspectorElement.FillDefaultInspector(rt, inspectTarget, editor);
    19. }
    20. rt.Bind(inspectTarget);
    21. return rt;
    22.  
    However I still don't understand why
    CreateInspectorGUI
    will return null....
     
    Last edited: Apr 26, 2023