The Resources folder

Resources folder

Create a folder called “Resources” in the asset folder.

What is it used for ?

The resources system is used in Unity to load and unload Objects at runtime.

Recommendations

Unity strongly recommend NOT TO use it, mainly for performance purposes as it can increase the startup time and the length of builds; secondly because it tends to make the memory management and the multi-port on various platforms difficult.

BUT Unity also recommend to use during rapid prototyping cases (hey, that’s us!) because of its ease of use. And honestly, we will use it for that, quite simple to load. We can quickly access objects that are not in the scene.

So what is it really used for ?

In fact, when building a game in Unity we have to tell Unity which scenes are used before building and compiling.
Unity organize these scenes by order of appearance (the first scene you include in your build options will be the starting scene for unity, the 1st scene that will be played when you launch your game -usually your main menu-).
And it also uses the chosen scenes to know which assets include or not : if your scene4 contains a specific asset, and you’re not including the scene in your game, the correspondent asset will not be included neither.

Except if you use the resources folder. The assets within the resources folder will be always included in your build.
So what’s the point of having resources always included even if it’s not used in a scene ?
Mainly for loading time; using a resources.load (for loading texture, pictures, screenloaders) will be load directly.
And assets contained in the scene must have the whole scene and assets loaded before to be accessible.

A quick example could be for a loading screen : you can call the loader image needed in the resource folder, while the entire scene is loading.

Usage example

(from Unity manual)

// Instantiates a prefab named "enemy" located in any <a href="https://docs.unity3d.com/ScriptReference/Resources.html">Resources</a>
// folder in your project's Assets folder.
        <a href="https://docs.unity3d.com/ScriptReference/GameObject.html">GameObject</a> instance = Instantiate(<a href="https://docs.unity3d.com/ScriptReference/Resources.Load.html">Resources.Load</a>("enemy", typeof(<a href="https://docs.unity3d.com/ScriptReference/GameObject.html">GameObject</a>))) as <a href="https://docs.unity3d.com/ScriptReference/GameObject.html">GameObject</a>;

Beware

Once again, if you tend to make a big project, DO NOT USE it. Or prototype first, and fix and delete every need of resources before growing big.

 

Leave a Reply