Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Embed UI Toolkit Editor Inside Another UI Toolkit Editor?

Discussion in 'UI Toolkit' started by Nexer8, Oct 8, 2021.

  1. Nexer8

    Nexer8

    Joined:
    Dec 10, 2017
    Posts:
    271
    I have a GameObject with a reference to a ScriptableObject that has a UI Toolkit Editor. The goal is to draw that Editor if the ScriptableObject is assigned. With IMGUI I used this approach:
    Code (CSharp):
    1. [CustomEditor(typeof(MyClass))]
    2. class MyEditor : Editor
    3. {
    4.     MyClass myClass;
    5.  
    6.     private void OnEnable()
    7.     {
    8.         myClass = (MyClass)target;
    9.     }
    10.     public override void OnInspectorGUI()
    11.     {
    12.         Editor editor = CreateEditor(myClass.scriptable);
    13.         editor.OnInspectorGUI();
    14.     }
    15. }
    With UI Toolkit I tried this, but the Editor seems detached from the ScriptableObject:
    Code (CSharp):
    1. [CustomEditor(typeof(MyClass))]
    2. class MyEditor : Editor
    3. {
    4.     MyClass myClass;
    5.  
    6.     private void OnEnable()
    7.     {
    8.         myClass = (MyClass)target;
    9.     }
    10.     public override VisualElement CreateInspectorGUI()
    11.     {
    12.         var root = new VisualElement();
    13.  
    14.         var editor = CreateEditor(myClass.scriptable);
    15.         root.Add(editor.CreateInspectorGUI());
    16.  
    17.         return root;
    18.     }
    19. }
    All of the values are default values and no changes are saved. Any idea on how to get this to work?
     
  2. uMathieu

    uMathieu

    Unity Technologies

    Joined:
    Jun 6, 2017
    Posts:
    396
    Calling CreateInspectorGUI() will only create the ui, without binding it.

    What you can try:
    Code (CSharp):
    1. var editor = CreateEditor(myClass.scriptable);
    2. var inspector = editor.CreateInspectorGUI();
    3. root.Add(inspector);
    4. inspector.Bind(editor.serializedObject);
    5.  
    6. return root;
    Usually you don't need to Bind because the InspectorElement does it for you.

    Depending on your editor version, YMMV, as there were a few related issues that were fixed in the past. Let me know if that helps
     
    Nexer8 likes this.
  3. Nexer8

    Nexer8

    Joined:
    Dec 10, 2017
    Posts:
    271
    Just tested it and it worked! Thanks for the help.
     
    uMathieu likes this.