# 2. CreateGUI, UXML, and USS Part of [[Editor Windows with UI Toolkit]]. Prev: [[1. Windows versus Inspectors]]. Next: [[3. SerializedObject in a Tool Window]]. ## 2.1 Scaffold Official 6000.5 workflow: **Create > UI Toolkit > Editor Window** generates a C# window plus optional UXML/USS, and a menu under **Window > UI Toolkit > ...**. You can also write it by hand. ```csharp using UnityEditor; using UnityEngine; using UnityEngine.UIElements; /// <summary>Minimal CreateGUI window. NOT compiled in Unity.</summary> public sealed class TemplateBrowserWindow : EditorWindow { [MenuItem("Window/Tutorials/Template Browser")] public static void Open() { var wnd = GetWindow<TemplateBrowserWindow>(); wnd.titleContent = new GUIContent("Template Browser"); } public void CreateGUI() { VisualElement root = rootVisualElement; var uxml = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>( "Assets/Editor/TemplateBrowserWindow.uxml"); if (uxml != null) root.Add(uxml.Instantiate()); else root.Add(new Label("Add a UXML at the path in CreateGUI, or build the tree in C#.")); var uss = AssetDatabase.LoadAssetAtPath<StyleSheet>( "Assets/Editor/TemplateBrowserWindow.uss"); if (uss != null) root.styleSheets.Add(uss); } } ``` Official guidelines (6000.5 `EditorWindow` / `CreateGUI`): - Put UXML/USS loading in **`CreateGUI`**, so the assets are available. - Register events in `CreateGUI` or after it. - **`CreateGUI` might not run before the first `Update`.** Guard `Update` if it touches widgets that `CreateGUI` has not created yet. - Official order: `OnEnable` → (if the Editor is not mid-`AssetDatabase` update) `CreateGUI` → `Update` → `OnGUI` → `OnDisable`. Hard-coded `Assets/Editor/...` paths are fine for a class demo; a `[SerializeField] VisualTreeAsset` assigned in a default-reference or loaded by a known path is sturdier. If `LoadAssetAtPath` returns null, show a Label - do not throw. ## 2.2 Do not mix OnGUI for the same UI `CreateGUI` is the UI Toolkit entry. Official samples still mention `OnGUI` in the lifecycle because IMGUI windows exist. For this series, **do not draw the same tools in `OnGUI` and `CreateGUI`.** If you must host a legacy IMGUI control, the Inspectors note already mentions `IMGUIContainer`. ## 2.3 Exercise Create a window from the official Editor Window creator, open it from the menu, dock it next to the Inspector, add one Button in UXML and one in C# inside `CreateGUI`. Confirm both show. Close and reopen - `CreateGUI` should run again and not stack duplicates (if you add in `OnEnable` by mistake, you will get duplicates; that is a part 6 bug). ## Sources - `CreateGUI` (6000.5): https://docs.unity3d.com/6000.5/Documentation/ScriptReference/EditorWindow.CreateGUI.html - Get started with UI Toolkit (6000.5): https://docs.unity3d.com/6000.5/Documentation/Manual/UIE-simple-ui-toolkit-workflow.html