Why the resources folder in Unity simultaneously sucks and is awesome!
So in Unity there is this magical folder you can find and create, its’ path is “Assets/Resources”. The reason I call it magical is because it has special powers…
Well, not really but it does some interesting stuff. The resources folder is where you can place assets that you wish to access easily by path as opposed to by reference. Assets in this case can be prefabs, serialized data, materials, textures, etc.
So through using the Resources class, which you can read all about here, you can load and use these assets aslong as you know there location within your resources folder.
This can be pretty useful. A good example is if you want to switch a sprite’s material, you can do so without having to have a reference to the new material you want. Instead you could make use of the Resources folder, placing your material in there and calling;
And you’re good to go!
The only thing that catches some people out is that you never need to add a file extension to the path name. E.g. for pngs rather than “Folder/image.png” it’s simply “Folder/image” for a file in the location “Assets/Resources/Folder/image.png”.
So be wary of naming things the same!
So why is this so great?
So this feature can be useful for a number of reasons.
One would be that you no longer need placeholder objects that are just on your scene so you have a reference to some asset you might need. Using Resources stops you needing references to assets to access them. Therefore can potentially remove the amount of objects you need on your scene, now you can reference things by path.
Another thing is it’s great for prototyping and is easy to use. Just bash objects int to the correct folder and boom you’re good to go! Which is arguably the biggest benefit of using Resources, just how easy it makes accessing assets you don’t have on your scene.
So why does this Suck?
So there are a couple of issues when actually using the Resources folder in Unity;
The first is the issue of what is being done with all the the assets when you don’t need them. Currently anything in the Resources folder is essentially packaged with your project even if they are not actually used anywhere. Everything in there is effectively treated as one big built in asset bundle. So will bloat the size of your project. This isn’t ideal
The other problem is having everything referenced by path. This can be a problem when you are having to hardcode paths as strings into your code. Every time you move an asset in your project you will then have to update your code. This is a bit of a hassle and it’s never ideal to have a bunch of strings hanging around in your code base, due to how easily it can become unmanageable. I guess it is worth noting that there are elegent ways to handle this problem, but I still think it’s something worth bringing up.
Another problem is it also means if you want to do over the air updates of new assets, you can’t do so with any assets in your Resources folder. This is something that can catch people out as using the Resources class is not the same as using Asset Bundles.
So when should I be using resources?
I would say if you’re prototyping or just fleshing out ideas then use it, but if you care in anyway about performance or memory use, then be wary!
Using resources is very beneficial if you have small assets you will constantly need, but don’t want to have references to these asset in all your scenes. Examples of this are scriptable objects you need for some data configurations, or perhaps, like I mentioned earlier, some materials you will want to apply to sprites for commonly occurring effects.
To conclude, Resources in Unity can be super useful, but there is a trade off between performance and ease of use, and it also leaves you with decisions to make about how you want your code base to look. My recommendation would be to think carefully about using resources for loading anything other than scriptable objects and small assets especially if you are developing for mobile platforms.
What are my options Doc!?
To finish off I just want to add that there is still a way to get assets by paths without using Resources and have more control over your memory use. This is through using asset bundles and, especially for more complex games, I recommend looking into this.
Unfortunately that’s a rather large topic to just bang on the end here, but if there is some interest I would be more than happy to talk a bit more about using asset bundles in another post, so let me know!