Create an in-game Inventory UI with UI Toolkit
UI Builder is a way to visually create and edit UXML and USS files. It is also highly useful if you are creating your assets via code and want to mockup how something might look or to see what the proper syntax is for a style variable. Open the window by going to Window > UI Toolkit > UI Builder.
UI Builder is broken out into six different sections:
Now it’s time to create the Inventory UI. Inside UI Builder, click on the first item in the Hierarchy window, called «unsaved window».uxml. This pulls up the base properties. Check the Match Game View checkbox. This ensures that the UI scales with the screen.
Next, Drag VisualElement from the library and drop it in the Hierarchy window. Click on it to pull up the properties. Set the following:
Drag another VisualElement into the hierarchy window and make it a child of the Container. Set the following properties:
Add a Label control as a child of Inventory. Set the following properties:
Add a VisualElement control as a child of Inventory. Set the following properties:
Save your UXML document by going to File > Save As at the top of the Viewport section and name it Inventory. You should now have a window that looks like this:
The layout engine that UI Toolkit uses is the open source project called Yoga, which implements a subset of Flexbox. Flexbox is an HTML/CSS based layout system. A useful guide to Flexbox can be found here.