AKA
AKA

Reputation: 6925

Finding the list of Resources used in a Scene Unity

I am trying to reduce the build size. I did compressed a few textures which helped me reduce the size a little bit. For further reduction I want to inspect the files kept in the Resources folder. There are multiple Resources folders. Over the time we have used different dummy stuffs inside the Resources folder. This is my plan:

  1. List out all the Resources based on Scenes.

    I managed to get the list of Resources using Resources.LoadAll() function.

    I got the list of Scenes as well.

    I don't know how to get the List of Resources used in a particular Scene.

  2. I can do a comparison with the original set of Resources and can get the list of unused ones.

Any Idea on how to do this?

Upvotes: 2

Views: 2455

Answers (2)

sonnyb
sonnyb

Reputation: 3504

As @Programmer has said, this isn't possible. Everything in your Resources folder is automatically included in your build in order to allow you to load a resource via Resources.load in your code.

I suggest getting in the habit of not using the Resources folder unless it's necessary to load that resource in via Resources.Load. This practice will help reduce the number of unused resources that are included in your build. Instead, reference resources to each other using the Unity Inspector, set references to public variables in scripts, and so on.

You can do some refactoring by figuring out what resources are loaded by code by searching for instances of Resources.Load. Everything else can be moved to a folder not named "Resources" (call it anything else, e.g. "Resources_Managed", to avoid it being considered a special Unity folder).


Note: the official Unity Tutorial for best practices for the Resources folder says not to use the Resources system:

Best Practices for the Resources System

Don't use it. [...]

The ease of the Resources folder makes it an excellent system to rapidly prototype. However, when a project moves into full production, the use of the Resources folder should be eliminated.


Note: It doesn't directly answer your question, but this paid Unity asset store plugin may be of use to you: Asset Usage Finder [I have no affiliation with this tool, though I do use it for my projects.]. It can't list all the resources used in a particular scene, but it can tell you where a particular resource is being referenced in your project. (Of course, it can't detect whether a resource is loaded in via Resources.Load). The source code is also included so it should be possible to write a tool to accomplish what you want (but it would be a lot of work).

Upvotes: 2

Programmer
Programmer

Reputation: 125255

I don't know how to get the List of Resources used in a particular Scene.

You can't do this and please don't spend so much time trying to do so because:

1. Once you place a resource file in the Resources folder, it will ways be included in the final build.

2. Calling Resources.Load from a scene in a particular script should be considered as using that resources in a scene but because this is done by code, you can't count it. There is no way to count that as being used.

3. Assigning any resource data that is outside the Resources folder to a public variable in a script will make that resource file to be included in the Resources folder. Even with this, you still can't count the resources used in particular scene.


One thing you can do is to have a ResourcesManager script for each scene that will hold the resources files then you can use that to retrieve resources files used by a scene. Still, it doesn't make sense to do that due to the three things I mentioned above. If you want to reduce build size, you gave to get ride of the Resources folder and start using the AssetBundle.

Upvotes: 2

Related Questions