ostanton::blog::posts::unity_uitk_vs

using namespace ostanton;

Unity's UI Toolkit with Visual Script

24 Feb 2023

Unity Visual Scripting:

As a designer, I find myself using visual scripting a lot more than code, so having a good visual scripting solution is necessary. Unreal Engine's Blueprints are the pinnacle of this. They aren't perfect, but they're the best I've used. Then comes Unity with its own visual scripting language. The highest praise I can give it is "passable". It's not fun or inspiring to work with, nor does it help you as a designer at all. It simply sits there assuming you know what every icon means and what each variable is called and that you don't care about how clean it looks. Blueprints in comparison provide context sensitive node searching, grid snapping, functions, easy and clear variable types (with tooltips!), and more. Now this first part isn't actually relevant to UI Toolkit, but I wanted to preface this post with my thoughts on Unity's visual scripting overall so you know what you're getting into.

UI Toolkit:

I appreciate the Unity team for creating the UI Toolkit, as it genuinely seems like a good UI system, and doesn't make me want to bash my head against a wall like other UI systems I've tried. However, there is one problem with it. Its usage with visual script is limited. I can't for the life of me work out how to call a clicked event for a button, but I have worked out how to change properties of elements, which is the main focus of this post.

Tutorial-esque section:

The first thing you need in order for this to work is more node libraries. Go to:
Edit > Project Settings > Visual Scripting and expand the "Type Options" box.

Project Settings

Add the options:

Project Settings

Then click "Regenerate Nodes". Once that is done, you can start hooking up the UI functionality in visual script. I trust you already know how to make a script and edit it etc., but if not, here's a quick rundown:

  1. Create a UI Document object in your scene (this is displayed automatically like Unity's old UI system)
  2. Create a UI Document in your Asset browser
  3. Link the document asset to your game object
  4. Edit the document asset and add your UI
  5. Add a Script Graph component to the UI Document object (or anything that has access to it)
  6. You'll see a choice for the source of the file, Graph and Embed.
  7. Graph acts like a C# file, where it's saved in the Asset browser and can be added to any other game object
  8. Embed doesn't create a separate file, instead making everything in this script only in this one game object (this doesn't affect Object, Scene, etc. variables)
  9. I'm using Embed for this example since I don't need to re-use it.
  10. Now get the Root Visual Element of the UI Document, either by getting the UI Document object as a variable, or using This if the script is on the object itself.

Visual Script

  1. Then out of Get Root Visual Element, call the Q node from U Query Extensions, and set the Name to be whatever element you want to change in UI Toolkit (must have that same name exactly).

Visual Script

  1. Now you can change whatever property you need of that element. Keep in mind that Unity doesn't change your search based on context, so you need to know what kind of element it is. This also only works if you have the relevant Type Option added in the Project Settings. Since I'm changing a progress bar's Low Value, I need the Progress Bar Type Option.

Visual Script

  1. Now hook that up to an event, and witness the glory!

Anyway, enough of that. I only learnt this because of this thread, and couldn't find anything about it in Unity's own documentation. So much for being well documented.

::blog::tags