How to map a set of uxml files and load them during runtime?

What I would like to get is:

  1. Create a map linking a “request” (string or enum) with the interface (uxml file)
  2. Call a method “open” with the parameter “key” (string or enum). ie: “loginScreen”
  3. Load the interface of the uxml file into a UIDocument.
  4. Connect to the remote server, gather some data and put it into the interface.

I’ve managed to do the 4th point, but using a UIDocument created on the editor and with the uxml file already attached. I’ve been unabled to find the way to load a uxml file in runtime.

Thanks in advance

Perhaps you are not supposed to load different uxml files during runtime and you must create just a single one and different VisualElements that behave as different windows which you hide and show.

How I did something similar:

  • A MonoBehabior with a VisualTreeAsset property (reference to a UXML file)
  • VisualTreeAsset has the Instantiate() method to create a VisualElement from it
  • Inject i do an existing VisualElement container element in the current UIDocument
1 Like

Hi runner:

So you link the UXML file to the object which contains MonoBehaviour script and switch between different VisualElements, but you never change the UXML file, correct?

Exactly, you don’t have to change the UXML itself.

1 Like

I will try that. Thank you

I managed to build a small set of “windows” which can hide and show, all of them contained in the same UXML document. You can also add more UXML documents into others so can split the design on different documents. But the problem is when you want to add the functionality of all buttons, etc… how you split this on different c# files?

I have created an abstract monobehavior with load and unload methods, from which I then create UI behavior components that are automatically activated when the sub UI is loaded.

So I have a GameObject with the UIDocument and my UIManager.
Then I have prefabs with UIComonent and UIBehavior components.
The UIManager binds clicks a custom element that I have created to the prefab link (I use addresables) and when you click, the prefab is instantiated at the first call and UIComponent manages the UIBehavior components.

Using “Prefabs” is the key, very clever.

Thanks again