Search Unity

Is it possible to author gui at runtime?

Discussion in 'UI Toolkit' started by DGordon, May 31, 2021.

  1. DGordon

    DGordon

    Joined:
    Dec 8, 2013
    Posts:
    649
    I haven't really looked at this since it was first talked about. I think I remember hearing there will be some sort of html-ish / css-ish system that doesn't require C#. Is that still true (if I'm not making that up in my memory)? If it is, can those be supplied at runtime to create / change the gui?

    IE: If I have a framework to create specific types of games from json files, could I _also_ allow the users to supply text files to create or alter gui on a game-by-game basis?

    Thanks!
     
  2. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,749
    I don't know about "doesn't require C#", but UIElements was originally pitched as having CSS like ...elements, maybe that's what you remember.

    There's talk about UXML assets here : https://docs.unity3d.com/Manual/UIElements.html maybe that's close enough to what you're talking about?
     
  3. DGordon

    DGordon

    Joined:
    Dec 8, 2013
    Posts:
    649
    Yeah, that's exactly what I remembered. Thanks!

    The following is from that documentation. My question is, can that be supplied at runtime, or does it need to be compiled into the build?

    There are two parts to that question ... can things be "reskinned" through uss at runtime, and further, can a brand new uxml document be supplied at runtime. The uxml document would require us to be able to somehow bind to c# properties, so we could read/write. If we could, that would be super awesome, allowing users to both reskin per project and even create custom required gui elements as needed.

    UI Assets
    The UI Toolkit provides the following Asset types that you can use to build user interfaces in a way that’s similar to how you develop web applications:

    • UXML documents: Unity eXtensible Markup Language (UXML) is an HTML and XML inspired markup language that you use to define the structure of user interfaces and reusable UI templates. Although you can build interfaces directly in C# files, Unity recommends using UXML documents in most cases.
    • Unity Style Sheets (USS): Style sheets allow you to apply visual styles and behaviors to user interfaces. They’re similar to Cascading Style Sheets (CSS) used on the web, and support a subset of standard CSS properties. Although you can apply styles directly in C# files, Unity recommends using USS files in most cases.
     
  4. Not at the moment, it has to be part of the build or downloaded through Assetbundle/Addressables, and given that moddability isn't priority for Unity as usual, so it won't be part of 1.0, probably (obviously it can change...)
     
  5. DGordon

    DGordon

    Joined:
    Dec 8, 2013
    Posts:
    649
    If it can come down in an asset bundle, why not some other way? Does it get compiled in some way when the asset bundle is built?

    I'm just curious. I use ABs a lot, but usually just think about them in terms of loading scenes or visual/audio assets.
     
  6. Yes, they are compiled, like your assets compiled when you make a build. In case of asset bundles, there are additional indexes, they are telling unity what assets are in, what to replace, etc.
     
    DGordon likes this.
  7. Leslie-Young

    Leslie-Young

    Joined:
    Dec 24, 2008
    Posts:
    1,148
    Everything you can do in USS/UXML can be done in C# if that helps?

    I am actually going to allow for UI customization in my latest project which will not only include changes to colour/theme but also what elements appears on panels. I will not be going as far as expecting a USS/UXML from the player/modder though. It will be more of a built-in editor that saves to a custom file (like JSON for example) and my runtime code will know how to recreate the UI from that.

    If must use USS/UXML then it probably need to come in via AssetBundle. I do not think there is anything to load a VisualTreeAsset from memory/buffer, yet, at runtime.