Unity 3D - Dynamically Load from Resources


You can dynamically load any file from any Resources folder in the project.
Make sure that the path is:
    1) relative to the Resources folder itself
    2) does not have the file extension 

For example:

//Load a text file (Assets/Resources/Text/textFile01.txt)
var textFile = Resources.Load<TextAsset>("Text/textFile01");

 

//Load text from a JSON file (Assets/MyJsonData/Resources/Text/jsonFile01.json)
var jsonTextFile = Resources.Load<TextAsset>("Text/jsonFile01");
//Then use JsonUtility.FromJson<T>() to deserialize jsonTextFile into an object

 

//Load a Texture (Assets/Resources/Textures/texture01.png)
var texture = Resources.Load<Texture2D>("Textures/texture01");

 

//Load a Sprite (Assets/Level1/MySprites/Resources/Sprites/sprite01.png)
var sprite = Resources.Load<Sprite>("Sprites/sprite01");

 

//Load an AudioClip (Assets/Level2/Audio/Resources/Audio/audioClip01.mp3)
var audioClip = Resources.Load<AudioClip>("Audio/audioClip01");


From the Unity documentation:

You can have multiple Resources folders placed anywhere inside the Assets folder.
url: 
https://docs.unity3d.com/Manual/SpecialFolders.html 

So that's cool!


Comments