Change UXML file at runtime

Hi all, i’m currently working on a project where I want to change a UXML file depending on the unit type that I select. So an Infantry unit should have a different UXMl from a tank unit and so on. I did some research and found this approach:

VisualTreeAsset additionalVisualTree = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>(infantryAbilitiesFilePath);
      if (additionalVisualTree != null)
      {
         VisualElement targetElement = document.rootVisualElement.Q<VisualElement>("AbilityBar");
         if (targetElement != null)
         {
            // Clone the additional UXML and add it to the target element
            VisualElement additionalContent = additionalVisualTree.CloneTree();
            targetElement.Add(additionalContent);
         }
         else
         {
            Debug.LogError("Target element not found: ");
         }
      }

Would that be a viable way to change a uxml at runtime or is there an alternative? Thank you so much

The high level logic seems correct to me. If you want this code to work in the Runtime, you will need to use something else that AssetDatabase.LoadAssetPath to load the VisualTreeAsset.

Check out this manual page for more information: