Unity has preserved some special names for folders like: Assets, Editor, Streaming Assets and Resources. Today we are going to talk about the last one.
Unity allows developers to store Assets within one or more folders named Resources. Those assets can be loaded or unloaded at runtime using the Resources API.
For load assets purpose you can use Resources.Load method. It have 2 parameters:
- path — a path to loaded file. When you pass empty string as argument, this method will load entire contents of the Resources folder.
- systemTypeInstance — optional parameter. Type filter for objects returned.
The path is relative to any Resources folder inside the Assets folder of your project. So when you have structure Assets/RobotsAssets/Resources/head.jpg for example, your path should look like that: Resources.Load(“head”).
Important! — Do not include the file extension names (.txt, .jpg, .fbx) in the path parameter.
Suported file types:
- Text files — supported formats: .txt, .xml, .json, .html, .htm, .bytes, .csv, .yaml, .fnt:
TextAsset textAsset = (TextAsset)Resources.Load("txtFilePath");
string txtFile = textAsset.text;
- Texture files:
Texture2D textureAsset = (Texture2D)Resources.Load("textureFilePath");
- Sprites — Single:
Sprite spriteAsset = (Sprite)Resources.Load("spriteFilePath");
- Sprites — Multiple:
Sprite spriteAssets = Resources.LoadAll<Sprite>("spritesFilePath") as Sprite;
- Sound files:
AudioClip audioAsset = (AudioClip)Resources.Load("audioFilePath");
- GameObject Prefab:
GameObject prefabAsset = (GameObject)Resources.Load("prefabFilePath");
- 3D Mesh:
Mesh modelAsset = (Mesh)Resources.Load("meshFilePath");
- Video files (since Unity 5.6):
VideoClip videoAsset = (VideoClip)Resources.Load("videoFilePath");
You can load files from Resources folder asynchronously using this Coroutine method:
ResourceRequest resourcesRequest= Resources.LoadAsync("anyPrefab", typeof(GameObject));while (!resourcesRequest.isDone)
Debug.Log("Loading progress: " + resourcesRequest.progress);
yield return null;
GameObject prefab = resourcesRequest.asset as GameObject;
Obviously to run this method you have to use this command:
Unity’s advice about Resources system
Don’t use it
There are some reasons that you shouldn’t use Resources:
- Using Resources folder makes memory management more difficult
- Usage of Resources system will increase application startup time and makes build time longer.
- It degrades a project’s ability to deliver content to specific platforms.
In that case, why does Unity provide us Resources folder? There are some specific use cases where this system can be helpful:
- During rapid prototyping and experimentation because this system is simple and easy to use.
- When content stored in Resources folder in not memory-intense
- When content is generally required during whole project’s lifetime — asset hosting third-party configuration data for example
- When content does not vary across platfroms.
Pro tip: Use
Resources.UnloadUnusedAssets()to release memory and unload assets that are not used.
Unity’s Resources system is very helpful tool. However you must remember that you should use it only when it really necessary. Overusing of this system may cause increase of startup time and longer duration of building time.
I hope it’s useful for you. If you have any comments or questions, please write them here!
To help us create more free stuff, consider leaving a donation through Patreon where you can vote for the next asset pack and get access to future packs & articles earlier. Sharing knowledge is our passion. Unfortunately, keeping a team of professionals costs a lot that’s why we are constantly taking on new opportunities.
We’ll be publishing any news on the following outlets:
Thanks for reading!