Intro to UI Toolkit
Code and workflows on how to use UI Builder and UI Toolkit from scratch
Navigating between scenes using UI and appropriately handling creating/destroying client/server worlds
Create a multi-view UI in a Navigation Scene, which is able to trigger a game scene, as well as game UI that is able to navigate back to our Navigation Scene.
- Creating a UI document and Panel settings
- Creating a ScreenManager to handle switching between views
- Nesting uxmls and custom Visual Elements in UI Builder
- Creating custom VisualElements
- Setting callbacks on OnGeometryChange()
- Updating DisplayStyle to switch views
- Creating a USS document shared by multiple UXML files
- Creating a class list
- Extracting inline styles to a class list
- Adding classes to elements from StyleSheets
- Creating custom styling for :hover and :active states
- Styling a view to be responsive to changes in width to be prepared for both mobile and desktop views
- Creating headers and footers for game information
- Using display flex in UI builder to create responsive designs
- Using standard Unity elements to create an interface
- Adding a png or SVG background
- Creating a ListView
- Setting a source of data for the ListView
- Creating custom click events and loading data when clicking on an item in ListView
- Loading data in between scenes to configure host/client build
- Using launch GameObjects to configure a scene to build as a host or a client
- Creating a ClientServerBootstrap to stop automatic world creation
- Triggering client and server worlds manually
- Supporting Thin Clients when creating client worlds
- Deleting all entities and worlds
- Using UniversalQuery in EntityManager
- Clean up to return to initial state
UI Builder is a new UI-creation tool by Unity. It allows you to easily create UI's that integrate with your Unity project. UI Toolkit (formerly UIElement) is a collection of features, functionality, resources and tools for developing user interfaces (UI). A good way to think about the relationship between the two is UI Toolkit is the elements needed to create UI and UI Builder is the tool you use to put them together.
UI Toolkit is a collection of features, functionality, resources, and tools for developing user interfaces (UI). You can use UI Toolkit to develop custom UI and extensions for the Unity Editor, runtime debugging tools, and runtime UI for games and applications.
UI Toolkit is inspired by standard web technologies. If you have experience developing web pages or applications, much of your knowledge might be transferable, and many of the core concepts might be familiar.
This section provides a short description of the major UI Toolkit features, functionality, resources, and tools, including:
The core of UI Toolkit is a retained-mode UI system based on recognized web technologies. It supports stylesheets, and dynamic and contextual event handling.
The UI system includes the following features:
- Event System : Communicates user interactions to elements; for example, input, touch and pointer interactions, drag and drop operations, and other event types. The system includes a dispatcher, a handler, a synthesizer, and a library of event types.
- UI Renderer: A rendering system built directly on top of Unity’s graphics device layer.
The UI Toolkit provides the following Asset types that you can use to build user interfaces in a way that’s similar to how you develop web applications:
- UXML documents: Unity eXtensible Markup Language (UXML) is an HTML and XML inspired markup language that you use to define the structure of user interfaces and reusable UI templates. Although you can build interfaces directly in C# files, Unity recommends using UXML documents in most cases.
- Unity Style Sheets (USS): Style sheets allow you to apply visual styles and behaviors to user interfaces. They’re similar to Cascading Style Sheets (CSS) used on the web, and support a subset of standard CSS properties. Although you can apply styles directly in C# files, Unity recommends using USS files.
The UI toolkit also includes the following tools and resources to help you create UI:
- UI Debugger: The UI debugger is a diagnostic tool that resembles a web browser’s debugging view. Use it to explore a hierarchy of elements and get information about its underlying UXML structure and USS styles. You can find it in the Editor under Window > UI Toolkit > Debugger.
- UI Builder (package): The UI Builder lets you visually create and edit UI Toolkit assets such as UXML and USS files. The UI Builder package is currently in preview. You can install it from the Package Manager window in the Unity Editor under Window > Package Manager.
- UI Samples: The UI Toolkit includes a library of code samples for UI controls that you can view in the Editor under Window > UI Toolkit > Samples.
The UI Builder lets you visually create and edit UI assets such as UI Documents (UXML /
.uxmlassets), and StyleSheets (USS /
.ussfiles), that you use with Unity's UI Toolkit (formerly UIElements). After you set up the UI Builder package, you can open the UI Builder window from the menu (Window > UI Toolkit > UI Builder), or from the Project window (double-click a
Note: In Unity 2019.x, you open the UI Builder from the Window > UI > UI Builder menu.Note: UI Builder is the visual authoring tool for UI Toolkit. It does not include runtime support. To enable runtime support in Unity 2020.1 and later, install the UI Toolkit package. For details, see this post on the Unity Forum: https://forum.unity.com/threads/ui-toolkit-1-0-preview-available.927822/.
UI Builder Main Window
- 1.StyleSheets: Manage StyleSheets and USS Selectors to share styles across UI Documents (UXML) and elements.
- 2.Hierarchy: Select, reorder, reparent, cut, copy, paste, delete elements in the UI hierarchy of your document.
- 3.Library: Create new elements or instance other UI Documents (UXML).
- 4.Viewport: See what your UI Document (UXML) looks like and edit elements visually directly on the Canvas.
- 5.Code Previews: See what the UI Builder is creating as text for both the UI Document (UXML) and the StyleSheets (USS).
- 6.Inspector: Use it to change the attributes and style properties of the selected element or USS selector.
There are a couple of ways to work with UI Toolkit. UI Toolkit requires a GameObject in a Scene with a UI Document Component that will hold a Source Asset (Visual Tree Asset).
GameObject UI Document Component Source Asset (Visual Tree Asset)
Multiple UI views and states can be handled by multiple GameObjects. Each view can have its own GameObject and the use of scripts can enable/disable different GameObjects as needed. This will in turn enable/disable different UIs. With this 1:1 GameObject to Source Asset approach you create a good UI flow. This is a fine approach.
Instead, in this gitbook, we will be using a single GameObject per scene, yet have a "ScreenManager" as the Source Asset which will handle enabling/disabling different child Visual Elements. "ScreenManager" is not an official Unity term, but the term we at Moetsi use to indicate a "screen that manages other screens."
In both cases there are multiple visual elements for different views. However it will be easier to navigate among views and edit in our UI Builder when taking the approach we just outlined above.
It is also possible to use the "ScreenManager" approach + multiple GameObjects (and there are probably use cases where this is preferred, so no hate on that approach). In this section we'll demonstrate how to change between different GameObject/SourceAsset combinations because we will be switching between Scenes using our UI.
Heads up: In this section we'll be working with one single USS sheet that we apply to all our UXML files. This is not a great approach for large and complicated UI projects. If you are familiar with web development, this is like having one single CSS sheet for an entire web app, which is not great for large complex systems with many teams.
So that being said, here's another reminder that the purpose of this gitbook is to introduce how to work with new technologies and put them together. Separating out a USS sheet per UXML document would be overkill for such a small project and would probably add more confusion than clarity in explaining how to work with UI Builder and UI Toolkit.
If you understand how to work with a single USS sheet it should not be too tough to include more USS sheets on your own. We will mention how to do it in this tutorial (although we will not implement it).
UI Toolkit's layout engine is based on the CSS Flexbox Model. Knowing how to create Flexbox layouts is critical to be able to use UI Builder and UI Toolkit.
We will integrate our UI with DOTS in the next section of this gitbook called "Multiplayer (NetCode+, UI Toolkit+), not in this section. Instead, this section will focus on integrating UI with scripts (no DOTS) for folks who are not interested in DOTS at all but still want to learn how to use UI Builder and UI Toolkit.
To best prepare for the following UI Builder and UI Toolkit code-alongs, we recommend you complete the following check-list:
- Watch Unity's UI Builder talk
- Complete Flexbox Froggy