Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Resolved UXML parsing at runtime

Discussion in 'UI Toolkit' started by Baste, Aug 25, 2022.

  1. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,194
    There's been mentions that converting uxml to visual elements at runtime would be possible at "one point". Is that something that's still being planned?

    It would be really convenient for sending UIs to display to the players over the internet (with patch notes or whatever) without having to write a custom format to parse (eww) or creating Asset Bundles (double eww).

    Ideally, I'd be able to "just" get one string for the uxml, one string for the uss, and then make things happen. I am aware that this will not necessarily be fast, but I can live with that. Any chance of something like that in the future?

    Somewhat related - any possibility of a VisualElement->UXML converter? It's a bit more esoteric, but I can imagine that it'd have some nice uses.
     
  2. uDamian

    uDamian

    Unity Technologies

    Joined:
    Dec 11, 2017
    Posts:
    1,203
    As I replied to the other question about USS parsing at runtime:
    We've always wanted to ship this capability at some point, but it's just not made it to the top of the queue yet. It is slowly bubbling up and this thread definitely helps.

    The biggest obstacle, as with USS parsing at runtime, are those asset references and resolving them at runtime (just from a path) without relying on the AssetDatabase APIs.

    For:
    See my reply to your other question here:
    https://forum.unity.com/threads/cloning-visualelements.1327308/#post-8392539
    It's basically the same question as cloning VisualElements, assuming you're talking about arbitrary VE hierarchy conversion to UXML.

    Slightly related, as of 2021 LTS, you _can_ find out if a VE was originally cloned from a UXML, retrieve that asset reference at runtime, and clone more copies. See:
    https://docs.unity3d.com/ScriptReference/UIElements.VisualElement-visualTreeAssetSource.html
     
  3. bottledgalaxy

    bottledgalaxy

    Joined:
    Jul 19, 2019
    Posts:
    83
    Oh dang... i just finished creating a whole template for an announcements section in my game, hoping to be able to send them through an api as an uxml string, and looks like it's not possible? :(

    Don't even need the USS part, just the uxml. Any progress being made in this regard?

    Thank you
     
  4. antoine-unity

    antoine-unity

    Unity Technologies

    Joined:
    Sep 10, 2015
    Posts:
    733
    This is not a capability we have added. You'd have to build the UXML inside Unity into an AssetBundle that you load VisualTreeAsset objects from directly.
     
  5. bottledgalaxy

    bottledgalaxy

    Joined:
    Jul 19, 2019
    Posts:
    83
    Thank you, is there any documentation or reference on how to do this?
     
  6. antoine-unity

    antoine-unity

    Unity Technologies

    Joined:
    Sep 10, 2015
    Posts:
    733
    You can check out this tutorial. The steps are the same for all Unity assets. Just adapt some of the details. You'd select the bundle at the bottom of the Inspector for your UXMLs, and at runtime you'd load with the VisualTreeAsset type.
     
  7. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    1,863
    There was a separate asset, called XML Layout, which was pretty much this concept. Once Unity decided to do their own spin on the data-described UI concept, that asset went freeware. I used it in a couple prototypes, it's quite capable, and handy for being able to re-skin in a style sheet.

    https://assetstore.unity.com/packages/tools/gui/xmllayout-plus-139527