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

Display uxml inspector in EditorWindows

Discussion in 'Scripting' started by Madkins75, Nov 14, 2019.

  1. Madkins75

    Madkins75

    Joined:
    Nov 23, 2018
    Posts:
    1
    Hello,
    I created a custom editor with uxml and uss, which works perfectly fine, but now, I am trying to display it within an EditorWindow. However I can only display the default inspector (commented code) using OnInspectorGUI.
    Any help is appreciated, thanks a lot.

    Code (CSharp):
    1. using UnityEditor;
    2. using UnityEngine;
    3. using UnityEngine.UIElements;
    4.  
    5. public class EditorTest : EditorWindow {
    6.     private static int _armor  = 75;
    7.     private static int _damage = 25;
    8.  
    9.     [MenuItem("EditorTest/Open _%#T")]
    10.     public static void ShowWindow() {
    11.         // Opens the window, otherwise focuses it if it’s already open.
    12.         var window = GetWindow<EditorTest>();
    13.  
    14.         // Adds a title to the window.
    15.         window.titleContent = new GUIContent("EditorTest");
    16.  
    17.         // Sets a minimum size to the window.
    18.         window.minSize = new Vector2(250, 50);
    19.     }
    20.  
    21.     /*
    22.     private void OnGUI() {
    23.         var editor = Editor.CreateEditor(TestEditorWindow.Instance);
    24.         editor.OnInspectorGUI();
    25.     }*/
    26.  
    27.    private void OnEnable() {
    28.        var editor = Editor.CreateEditor(TestEditorWindow.Instance);
    29.        editor.CreateInspectorGUI();
    30.    }
    31. }
    32.  
    33. public class TestEditorWindow : System.ScriptableSingleton<TestEditorWindow> {
    34.     public int armor  = 75;
    35.     public int damage = 25;
    36.     public string text;
    37. }
    38.  
    39. [CustomEditor(typeof(TestEditorWindow))]
    40. public class TestEditorWindowEditor : Editor {
    41.    
    42.     public override VisualElement CreateInspectorGUI() {
    43.         VisualElement customInspector = new VisualElement();
    44.         var quickToolVisualTree = Resources.Load<VisualTreeAsset>("EditorTest_Main");
    45.         quickToolVisualTree.CloneTree(customInspector);
    46.         customInspector.styleSheets.Add(Resources.Load("custom-editor-uie-style") as StyleSheet);
    47.         return customInspector;
    48.     }
    49. }
    50.  
     
  2. Dliix66

    Dliix66

    Joined:
    Sep 5, 2017
    Posts:
    16
    Bump, I am also facing this problem :(
     
  3. aybeone

    aybeone

    Joined:
    May 24, 2015
    Posts:
    107
    I've been facing a similar issue, I hope that helps ! ;)

    (basically, the inability of CreateEditor to use UI elements)

    Unity_2020-05-03_05-07-04.png devenv_2020-05-03_05-08-03.png devenv_2020-05-03_05-09-12.png

    In short, re-use your elements tree where you need it + Bind().

    :)