Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Building seems to include contents of any folder with name "Resources" to the build

Discussion in 'Editor & General Support' started by maikkanerva, Dec 1, 2023.

  1. maikkanerva

    maikkanerva

    Joined:
    Dec 14, 2018
    Posts:
    27
    I recently made a new build of our game, and I was surprised about the increased build size.
    We use addressables, and not many new asset groups were added between the latest and this new build.

    The difference was that we had added an asset pack to the project, which contained a lot of high-detail textures, located in a folder called "Resources" (path being something like Assets/Asset packs/Asset_X/.../Resources/). This also contained a lot of materials, shaders etc. Currently none of these are referenced anywhere in the game.

    Is this just due to the naming scheme of the asset, and should I check all of my imported assets to not have folders name Resources to not have them include stuff to builds that are not referenced anywhere?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,312
  3. tsibiski

    tsibiski

    Joined:
    Jul 11, 2016
    Posts:
    591
    Like Kurt said, this is as design. Assets in the Resources folder are forced to be included by design. Because you may reference the assets and load them programmatically via "Resources.Load<AssetType>("RELATIVE/PATH/UNDER/SOME/RESOURCES/FOLDER);".

    When Unity culls unused assets, it is doing so based on references to the assets in a GameObjects inspector window. If it doesn't find a reference there, then it removes the asset. But that can't detect if the item is referenced in code. So the Resources folder is how you guarantee that the asset will be available for your code to reference.
     
  4. maikkanerva

    maikkanerva

    Joined:
    Dec 14, 2018
    Posts:
    27
    To clarify, these textures etc. were located within a folder called Resources within that asset packs hierarchy.
    Does Unity look up for each folder named "Resources" in the project and include all of them?

    I thought when it's being called "the Resources folder" that it's only the folder directly under Assets folder.
     
  5. tsibiski

    tsibiski

    Joined:
    Jul 11, 2016
    Posts:
    591
    It does not matter where the folder is located. All folders anywhere recursively under Assets, that have the exact name "Resources" will have their files pulled and combined together as a resource pool.

    So it doesn't matter if the file is under "Assets/Resources/MySprite01.png" or "Assets/SomeFolder1/SomeFolder2/Resources/MySprite02.png".

    Both will be added to the resource pool, and both are accessed by Resources.Load<Sprite>(), by the relative path under their respective Resources foler. In this case, both would be referenced like this:


    Code (CSharp):
    1. Resources.Load<Sprite>("MySprite01");
    2. Resources.Load<Sprite>("MySprite02");
    But keep in mind that if you add subfolders under each Resource folder, like "Assets/Resources/SomeSubFolder/MySprite01.png", then the code to reference it would appear like:


    Code (CSharp):
    1. Resources.Load<Sprite>("SomeSubFolder/MySprite01");
    The resource for all resources folders being included like this is because people don't generally want one massive folder with all their unorganized resources thrown into a flat list. It's insanely hard to organize and find things like that. So people organize all of the assets logically in a human readable way under their Assets folder, and then resources specific to each asset are put into a Resources folder under that asset's own folder. All for the sake of organization and separation of dependencies for any human coming along to look at this stuff.
     
    Last edited: Dec 4, 2023
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,312
    This is true, and there's an additional technical reason: if you use
    Resources.LoadAll<Sprite>("");
    , Unity will actually load EVERY SINGLE ASSET in all
    Resources/
    folders everywhere (!!!!!!) into memory, then figure out which ones are of the type you want and return an array of those that match.

    Try it yourself. :)

    ALSO... just a heads-up: you must not include the
    .png
    extension. It should be:

    Code (csharp):
    1. var sprite = Resources.Load<Sprite>("MySprite01");
     
    tsibiski likes this.
  7. tsibiski

    tsibiski

    Joined:
    Jul 11, 2016
    Posts:
    591
    Thanks for the correction on the extension!
     
    Kurt-Dekker likes this.
  8. maikkanerva

    maikkanerva

    Joined:
    Dec 14, 2018
    Posts:
    27
    Alright, thanks for the clarification!

    We don't intend to use Resources folders at all, so the solution for me was to rename the folder name from the asset hierarchy and that removed all those unused textures etc. not being included in the build.
     
    Kurt-Dekker likes this.