Build a runtime inventory view in Unity 6 UI toolkit using visual elements and data binding

Create a Runtime Inventory View in Unity 6 UI Toolkit

Tutorial on building Runtime UI Elements that use the UI Toolkit Data Binding System for Visual Elements from C# scripts

Tutorial Introduction

In this tutorial we will be building a runtime window that uses Unity 6 DataBinding system in UI Toolkit to display data about some example inventory items from a Data Model. The form we make will have 2 filtering options, a DropdownField displaying the item types available, a Textfield allowing a search feature and  a reset Button used to show a Unity Event. Results will be displayed in a Listview element that uses the data model to populate its items. We will also add a UXMLObject in the form of Custom Controls as a Clock to the header. UXML Objects are great little independent items that can run in isolation and update your UI as needed by using is as a Custom Binding Type

The Binding system in Unity 6 is still a work in progress, but it gets better and better with each new release. It is a powerful system once you get used to the way UIToolkit elements are addressed and understand the data model as a middle man between your data and your UI. You can follow along and copy past the code into your project or you can download the completed project from our GitHub Repository and then follow along with the explinations. The source code also has extensive documentation for each method including importaint variables and attributes and is the exact same code that is show on this page in the code snippets section below.

Once completed with project setup we will go over each of the files in detail and I will include screen shots of the section being discused so you can follow along or take notes in your own code.

Running example of the finished code in this tutorial.

Runtime UIToolkit Inventory Window Example

Unity 6 UI Toolkit Reference Materials

The following sites were used while I was learning UI Toolkit and in the creation of this tutorial. There are also numerous YouTube video based tutorials that you can follow on UI Toolkit and UI Builder itself and I will eventually make an accompany video for this tutorial and post the link here once it is completed.

Tutorial Primers 

We have created some additional standard help and reference content that we use in all of our Unity tutorials. We have listed them here for quick access as this tutorial assumes at least general understanding of the topics.

  • If you are a new developer that has worked only with local files then you can look at our How To Download From GitHub Tutorial and you can download a working copy of the project to follow along with OR you can use the code snippets below to create each file as they are covered.
    Note: This example project is a working asset similar to a downloaded asset, there are only a few files but they are detailed with comment based documentation 
    If you are familiar with GitHub then you can access our Repository at https://www.github.com/GarageWareGames there you will find our Unity UI Toolkit Tutorials source code repo with this tutorial being in the /InventoryExample folder. Our examples are NOT completed project's, they are assets only (i.e. Scripts, Scenes, Images, etc) much like a downloaded asset from the asset store. We DO NOT include compressed files of any kind in our public repositories, this includes Assets Bundles, and any other type of compressed files. GitHub does allow you to download a zip of the repository, but that is GitHub creating the compressed file of our repo for you so it can be transferred, not us.

    NEVER open compressed files from an unknown source!
  • We have created a starter tutorial on Unity6 Data-Binding that covers the same fields used here demonstrating the Data Model used to power a UI and the binding process of each Visual Element, one at a time. The code for this section is also included in the Public GitHub Repo located in the DataBinding folder.

    View Primer Tutorial Video on YouTube | Local in Modal Window

Project Setup

Perform the following steps if you are creating the project from scratch.

  1. Create a Scene
    In your Scenes folder create a new scene to use for the project. If you do not have a scenes folder, create one.
  2. Create all C# Script files
    Using the standard Unity Project Layout, create a folder called scripts, within your new Scripts folder create each of the .cs files below and paste the code into the proper files.
  3. Create the needed Resource files
    If you do not have a Resources folder then create one. In your Resources folder create a Stylesheet(USS) and a blank UXML document, copy the InventoryDataBinding.uss source code into the newly created USS file using a code editor, the UXML document just needs to exist and be linked in the UI Document Component.
  4. In the Resources folder create another folder called DataBinding and copy download the images into it.
    Here is a list of the images included with this project, you can download them by right clicking on each image and selecting download image, saving them to the DataBinding folder you created in the previous step.
    Images Used In Project *You may also need to convert the images into a sprite UI format.
    If you are unsure how to do that then see our
    Getting Your Images Ready for UI Toolkit tutorial

    The folder structure as shown in our repository
    contains subfolders in the scripts folder that are not required
    but it is considered a good practice to keep scripts orginized
    Typically grouped by purpose, function or system
    Project Structure Once At This Step Tutorial Project Structure
  5. Importaint Note on the Folder Structure:
    You may notice that there is an additional file in the repository, the DataSourceBoilerPlate.cs file in the Datasources directory is included in the repo download but is not an actual part of this tutorial. It is the base code compiled from the Unity documentation on UI Toolkit DataBinding that is used to create Data Models and is included for reference only to help in your learning and skill development.
  6. Open your Scene and Add a UI Document
    Right click in the Scene hierarchy and select UI toolkit - UI Document. Once created make sure you have a Panel asset assigned and then Add the UXML docuemnt you just created.
  7. Attach the DataBindingTutorialUI.cs Monobehavior to the UI Document
    You can drag the DataBindingTutorialUI.cs script from the project window on to the UI document in your Secene Hierarchy -OR- You can select the UI Document and use the Inspector to add the script by clicking Add Component the searching for DataBindingTutorialUI pnce completed you see the following:
    Views of your Scene heirarchy and Inspector view of the selected Ui Document Binding Project Scene Hierarcht
    UI Document Selected view in the Unity 6 Inspector
  8. Enter PlayMode to see the Runtime window

    At the top of the Unity Editor window click the Play button and make sure your in the Game View tab. the form will auto populate with the sample inventory, 9 items total. This form uses all the same controls covered as individual items in our Understanding Unity UIToolkit Data-Binding Tutorial with all of the individual Visual Elements working together to create an in game user interface.

    The DropdownField is linked to the data source using 2 bindings, one feeding the choices list in a read only setup and another sending and receiving the selected value in a read/write setup. The Textfield is a basic search feature and is bound using a write only setup. The list of items will be filtered based on the selected item type and the entered text. The Button uses a simple but effective way to link the built in button click event to the Data Model that will reset the form when activated. And there is the Listview Element itself that uses the Datamodel to populate a list that uses a custom control to display the line item data. Finally, just as an extra example from the Unity Docs, there is a Custom Binding Type called Current Time Binding that powers the Custom Control and displays a running clock in the header.

    If for some reason you are getting an error when you enter Playmode then make sure that your project structure matches that on the folder structure sample image above.
    If the images are not displaying then they are in the wrong folder location OR have not been converted into a Sprite / GUI UI format.
    If you just see the wire frame display then make sure you are on the Game View Tab.

    If you still have errors then please copy your error into an email and send it to support@garage-ware.com and we will try and help you best we can.

    Runtime UIToolkit Window Example

The Data Model

Section is a Work In Progress

The DataBinding Tutorial UI

Section is a Work In Progress

The Current Time Custom Binding Type Example

Section is a Work In Progress

The Clock Element Custom Control

Section is a Work In Progress

The USS Style Sheet

Section is a Work In Progress

C# Scripts Source Code

The C# source code is included here for reference, you can use it to recreate the project from scratch if you like. Once you have copied the code you will need to setup your project and scene.

MyInventoryDataModel.cs

DataBindingTutorialUI.cs

CurrentTimeBinding.cs

ClockElement.cs

InventoryDataBinding.uss