To reduce loading times and manage an application's memory footprint, it's important to understand the resource lifecycle of Unity Engine.
In this chapter discusses about the Resources life-cycle and how to access assets from Resources. This is a system that allows us to store Assets within one or more folders having a name with Resources and to load or unload Objects from those Assets at runtime using the Resources API.
Generally we don't use path names to access assets in Unity.
When you assign asset directly into the inspector by declaring that asset type of variable, Unity reduce the build size of your player because Unity automatically checks which assets are used to build our player.
But in the case of Resources, all asset sizes are consumed which are used to build player or not while we build the player. So keep in mind that the remove all asset from Resources folder which is not necessary for our player build.
Sometimes there are situations where it is much good to access an asset by its name instead of linking to it in the inspector. It is inconvenient to assign the reference to the object in the inspector whenever you might want to create a game object procedurally from a script and for example assign a texture to a procedurally generated mesh.
When You Want To Access Assets From Resources You Have Need To Create A Folder Name With Resources. To Create Resources Folder Just Select Project > Right-Click > Create > Folder.
Note: Resources folder is not created when a new Project is created.
Then renamed this folder to “Resources”.
In my case I just want to change my Player sprite at runtime which is placed in the following path asset > Resource > Sprites > Mario Run.
Now To Manage And Access Asset We Just Create New Empty Game Objects For Simplicity, Renamed It To “Asset Manager”.
Let’s Create A New C# Script Then Open That Screen And Now Declare A Public Game Object That You Want To Do Any Kind Of Stuff.
In the below-mentioned code, I'm just declaring my Player game object as a public game object so I can able to assign from my hierarchy.
In My Case My Resources Folder Contains Three Main Subfolders As Shown Below.
But the sprite i want to access from Resources is placed in Sprites folder. You make sure that the asset that you want to access from Resources have a proper folder and name wise path.
So as above mentioned code, I’m writing this code like “Resources.Load("Sprites/Mario Run");” to tell the compiler to load Sprite type asset from the “Resources/Sprites/Mario Run” path.
Now I'm assuming this loaded sprite to my “PlayerSprite” and after that assign this sprite to my Player sprite.
Save The Script And Apply This Script To The Asset Manager Game Object And Assign The Reference Of Our Player From The Hierarchy.
Now run the scene and you can see that my Player change its sprite after 3 seconds.
This method returns a list of all objects of Type type.
Open the below link from more details about this method.
https://docs.unity3d.com/ScriptReference/Resources.FindObjectsOfTypeAll.html
This method loads an asset stored at the path in a Resources folder.
Open the below link from more details about this method.
https://docs.unity3d.com/ScriptReference/Resources.Load.html
This method loads all assets in a folder or file at the path in a Resources folder.
Open the below link from more details about this method.
https://docs.unity3d.com/ScriptReference/Resources.LoadAll.html
Asynchronously loads an asset stored at the path in a Resources folder.
Open the below link from more details about this method.
https://docs.unity3d.com/ScriptReference/Resources.LoadAsync.html
This method helps to unload assetToUnload from memory.
Open the below link from more details about this method.
https://docs.unity3d.com/ScriptReference/Resources.UnloadAsset.html
It helps to unload assets that are not used.
Open the below link from more details about this method.
https://docs.unity3d.com/ScriptReference/Resources.UnloadUnusedAssets.html
In this blog, we have discussed resource folder limitations, advantages, and how to access it from Unity. A step-by-step process is explained here for you.
There is a list of static methods as well with a documented file for each method that is related to resources and its function.
COO of Vasundhara Infotech, a leading Software development company in the USA. His technological interests has helped the company in making useful decisions.
Sign Up to our newsletter to get latest updates staight in your inbox.
Vasundhara respects your privancy. No Spam!
Sign Up to our newsletter to get latest updates staight in your inbox.
Vasundhara respects your privancy. No Spam!