Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Generate bindings for custom uxml components

See original GitHub issue

Unity supports re-use of uxml components in other uxml components. Rosalina doesn’t generate bindings for these custom uxml components.

Pseudo-ish code for an idea about what to generate:

// CustomComponent.g.cs
public partial class CustomComponent
    private VisualElement _root;

    public CustomComponent(VisualElement root)
        _root = root;

    public Button MyButton { get; private set; }

    public void Initialize()
        MyButton = (Button)_root?.Q("MyButton");


// MainUIDocument.g.cs
public partial class MainUIDocument
    private UIDocument _document;

    public CustomComponent MyCustomComponent { get; private set; }

    public VisualElement Root
            return _document?.rootVisualElement;

    public void InitializeDocument()
        MyCustomComponent = new CustomComponent((VisualElement)Root?.Q("MyCustomComponent"));

In general, I have uxml components that will never be a top-level UI element. They will always be composed by something else, so their [SerializeField] UIDocument _document field is unnecessary.

Issue Analytics

  • State:open
  • Created a month ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

Eastrallcommented, Aug 13, 2023

At the moment, Rosalina supports only native types:

I believe this is a good idea to add support of custom components, let’s plan this for next releases. Thanks for the suggestion!

Eastrallcommented, Aug 18, 2023

You are correct, the default constructor generation is a mistake. I thought that without a default constructor, Unity wouldn’t be able to instanciate the Menu document as a component on a game object. Looks like I was wrong, since I removed it and it still worked.

I’m not familiar with how the auto-generation code works, but maybe you can generate different versions of the file based on it’s usage. In your example above, when the autogeneration code gets to Menu.uxml, it’ll see that it contains a MenuItems template. Instead of generating the default MenuItems.g.cs, you could generate a MenuItems.Component.g.cs that only has a single constructor expecting a VisualElement. And when the code generation gets to MenuItems.uxml, it’ll generate the default MenuItems.g.cs, which can be used as a normal top-level ui component.

The code generation mechanism is triggered by uxml file. Which means that every uxml file is treated one by one. Thus looking for what uxml is a component wouldn’t work in that case. I tried to mix up both code generation processes because I currently don’t have any other way to identify what is a component and is not. But, once the Unity team finishes the UI Builder extension mechanism, I would be able to create a field that would allow the developer to specify what “kind” of document it is. For example, a document, a component, an extension, etc…

Another approach would be to have something more “manual” which could be integrated along with the suggestion made on issue #26 : Manual generation

By doing a right-click on the UXML file, you could :

  1. Activate / Deactivate the generation process
  2. Choose how to generate the UXML (document or component)

All this would be stored in the RosalinaSettings and the code generator would generate the correct code without having a UIDocument field for custom component.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Manual: Bind with UXML and C# script
This example demonstrates how to create a binding and set the binding path with UXML, and call the Bind() method with a C#...
Read more >
Create a bindable custom control
This example creates a custom control bound to a property with the double data type. You can adapt this example to bind to...
Read more >
Feature Request - Custom UXML Attributes
Hello, I would like to propose a way to define my own Attributes for VisualElements that I have little to no control of...
Read more >
Extending the Unity Editor with custom tools using UI Toolkit
Like building your own UI tools? Here we go step by step with the UI Builder to quickly create an Inspector for real-time...
Read more >
A GameObject's functionality is defined by the Components attached to it. ... See in Glossary; UXML; SerializedObject data binding; Create the custom ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Post

No results found

github_iconTop Related Hashnode Post

No results found