unity3d Resources Resources 101



Unity has a few 'specially named' folders that allows for a variety of uses. One of these folders is called 'Resources'

The 'Resources' folder is one of only TWO ways of loading assets at runtime in Unity (The other being AssetBundles (Unity Docs)

The 'Resources' folder can reside anywhere inside your Assets folder, and you can have multiple folders named Resources. The contents of all 'Resources' folders are merged during compile time.

The primary way to load an asset from a Resources folder is to use the Resources.Load function. This function takes a string parameter which allows you to specify the path of the file relative to the Resources folder. Note that you do NOT need to specify file extensions while loading an asset

public class ResourcesSample : MonoBehaviour {  
    void Start () {
        //The following line will load a TextAsset named 'foobar' which was previously place under 'Assets/Resources/Stackoverflow/foobar.txt'
        //Note the absence of the '.txt' extension! This is important!

        var text = Resources.Load<TextAsset>("Stackoverflow/foobar").text;
        Debug.Log(string.Format("The text file had this in it :: {0}", text));

Objects which are comprised of multiple objects can also be loaded from Resources. Examples are such objects are 3D models with textures baked in, or a multiple sprite.

//This example will load a multiple sprite texture from Resources named "A_Multiple_Sprite"
var sprites = Resources.LoadAll("A_Multiple_Sprite") as Sprite[];

Putting it all together

Here's one of my helper classes which I use to load all sounds for any game. You can attach this to any GameObject in a scene and it will load the specified audio files from the 'Resources/Sounds' folder

public class SoundManager : MonoBehaviour {
    void Start () {

        //An array of all sounds you want to load
        var filesToLoad = new string[] { "Foo", "Bar" };

        //Loop over the array, attach an Audio source for each sound clip and assign the 
        //clip property.
        foreach(var file in filesToLoad) {
            var soundClip = Resources.Load<AudioClip>("Sounds/" + file);
            var audioSource = gameObject.AddComponent<AudioSource>();
            audioSource.clip = soundClip;

Final Notes

  1. Unity is smart when it comes to including assets into your build. Any asset that is not serialized (i.e. used in a scene that's included in a build) is excluded from a build. HOWEVER this DOES NOT apply to any asset inside the Resources folder. Therefore, do not go overboard on adding assets to this folder

  2. Assets that are loaded using Resources.Load or Resources.LoadAll can be unloaded in the future by using Resources.UnloadUnusedAssets or Resources.UnloadAsset