Search Unity

Question Questions about best practices for UI Tookit asset organization

Discussion in 'UI Toolkit' started by beevik_, Oct 28, 2021.

  1. beevik_

    beevik_

    Joined:
    Sep 27, 2020
    Posts:
    94
    I've been looking at the UI Toolkit samples included with the UI Builder package version 1.0.0-preview.18, and I have a few questions about the way these samples organize files.

    1. Style sheets are always included in a Resources folder, whereas UXML files are always outside it. Is this considered best practice?
    2. Where should theme style sheets reside? Should they also live in a Resources folder?
    3. The Resources folder is always a subfolder of an Editor folder. Why?
    4. Resource folders are generally discouraged by Unity, so why do the samples use them? (See https://learn.unity.com/tutorial/assets-resources-and-assetbundles#5c7f8528edbc2a002053b5a6)

    Basically, I'd like more clarity around the way my project's UI toolkit assets should be organized and packaged. As far as I can tell, the documentation doesn't cover this topic.
     
    MostHated likes this.
  2. martinpa_unity

    martinpa_unity

    Unity Technologies

    Joined:
    Oct 18, 2017
    Posts:
    479
    HI!
    It depends on the context in which they are used. If this is for editor purposes, the assets could be located anywhere, so it's your preference. For runtime purposes, the important bit is that they need to be included in a build, so there are multiple ways to achieve that.

    Any asset that is directly referenced in a scene will be included in a build (as long as the scene is also included), so having a UXML file referenced in a UIDocument MonoBehaviour will do it. For assets that are loaded through code, you need to force the inclusion in a build by either putting it in a Resources folder or by creating a ScriptableObject that will contain all the references you will use.

    I personally prefer the latter, because it's easier to control what gets included without potentially losing references (we added support for GUID-based references a while ago, but if your assets only contain paths, moving them in and out of a Resources folder would break the link).

    More specific answers:
    UXML files referenced through a UIDocument will be included in a build. If the StyleSheet is added through the UXML file, it should not matter where they reside.

    Similarly to the UXML files, if your theme style sheet is included in a PanelSettings asset, the theme will also be included.

    I believe that was just for structuring the project. Resources folder inside an Editor folder won't be included in a build.

    The samples are primarily focused on UI Toolkit. I don't think the asset management part of it was intended as a way to demonstrates best practices. I'll take the feedback back to the team.

    Hope this helps!
     
    Joserbala, CodeSmile and beevik_ like this.