Search Unity

Bug StyleSheet type filter for Resources.Load broken

Discussion in 'UI Toolkit' started by eidetic, Apr 5, 2019.

  1. eidetic

    eidetic

    Joined:
    Jun 27, 2016
    Posts:
    14
    Hi there,

    I noticed that when I have a USS file and a UXML file with the same file name in Resources, Resources.Load sometimes confuses them.

    In the following example:

    Code (CSharp):
    1. var styleSheet = Resources.Load<StyleSheet>("FileName");
    A VisualTreeAsset object is actually returned if there is a UXML file in resources named "FileName.uxml" -- regardless of the type specified inside the pointy brackets.

    The same issue arises when using the Type overload for Resources.Load() instead of using the Generic method.

    upload_2019-4-5_14-43-24.png

    Maybe it is not best practice to keep the same filename, but I think it is pretty neat to have:

    CustomVisualElement.cs
    Resources/CustomVisualElement.uss
    Resources/CustomVIsualElement.uxml

    Maybe there is a need to load inline styles from a VisualTreeAsset(?), but it should be the job of the Type specification in the Resources.Load method to differentiate the two Types and load the correct asset.
     
  2. uDamian

    uDamian

    Unity Technologies

    Joined:
    Dec 11, 2017
    Posts:
    1,231
    This is not so much a bug as it is a reality of how Unity's Resources work. Extension is not considered when loading Resources so you really do need to name them differently. This is unlikely to change anytime soon.

    That said, in the Editor, you can also put your files in any project folder and load them by path (with extension) via AssetDatabase.LoadAssetAtPath(). With the asset db, you can name them the same and it should work fine.
     
  3. eidetic

    eidetic

    Joined:
    Jun 27, 2016
    Posts:
    14
    I see. I wanted to steer clear of AssetDatabase because I believe the UI System has a future of being usable at runtime, and I want the base classes I'm building to not be dependent on the UnityEditor.

    I thought the Resource would store the Type information and the Type specification on the generic method was actually doing some filtering, not just casting the resource of a particular name.

    Thanks for the response -- I think I'll just name the files differently!
     
  4. johnseghersmsft

    johnseghersmsft

    Joined:
    May 18, 2015
    Posts:
    28
    There are a few other things you can do:

    1) Make your Resources have a directory structure:
    CustomVisualElement.cs
    Resources/styles/CustomVisualElement.uss
    Resources/uxml/CustomVIsualElement.uxml

    Then you can load from the specific paths "styles/CustomVisualElement" and "uxml/CustomVisualElemenet"

    2) Use the new Addressables system. I do a lot with name-based assets in the tools I'm building and moving from AssetBundles to Addressables was a nice change. Definitely a better "Resources" system.