unity3d Resources Introduction


Example

With Resources class it's possible to dynamically load assets that are not part of the scene. It's very usefull when you have to use on demand assets, for example localize multi language audios, texts, etc..

Assets must be placed in a folder named Resources. It's possible to have multiple Resources folders spread across the project's hierarchy. Resources class will inspect all Resources folders you may have.

Every asset placed within Resources will be included in the build even it's not referenced in your code. Thus don't insert assets in Resources indiscriminately.

//Example of how to load language specific audio from Resources

[RequireComponent(typeof(AudioSource))]
public class loadIntroAudio : MonoBehaviour {
    void Start () {
        string language = Application.systemLanguage.ToString();
        AudioClip ac = Resources.Load(language + "/intro") as AudioClip; //loading intro.mp3 specific for user's language (note the file file extension should not be used)
        if (ac==null)
        {
            ac = Resources.Load("English/intro") as AudioClip; //fallback to the english version for any unsupported language
        }
        transform.GetComponent<AudioSource>().clip = ac;
        transform.GetComponent<AudioSource>().Play();
    }
}