209

Share

GAME DEVELOPMENT

How To Access The Resources Folder In Unity?

  • Home
  • Blogs
  • How To Access The Resources Folder In Unity?

Resources folder contains folders to store different files, such as binary, data, images, and library folders that work to store code used by external functions.

Agnesh Pipaliya

June 27, 2022

Introduction To Resources

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.

Limitation Of Resources

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.

Advantage Of Resources

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.

Steps For How To Access Asset From Resources

Step 1:

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.

 

 

Step 2:

Now To Manage And Access Asset We Just Create New Empty Game Objects For Simplicity, Renamed It To “Asset Manager”.

 

 

 

 

Step 3:

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.

 

 

Step 4:

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.

Step 5:

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.

 

 

 

Static Method Related To Resources And Its Function

1. Find Object Of Type All

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

2. Load

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

3. LoadAll

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

4. Load Async

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

5. Unload Asset

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

6. UnloadUnusedAssets

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

Conclusion

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.

 

img

Agnesh Pipaliya

COO of Vasundhara Infotech, a leading Software development company in the USA. His technological interests has helped the company in making useful decisions.

message

Have a project in mind? Drop a message to Bansi Pipaliya & start the discussion!

Get a Newsletter

Sign Up to our newsletter to get latest updates staight in your inbox.

Vasundhara respects your privancy. No Spam!

Get a Newsletter

Sign Up to our newsletter to get latest updates staight in your inbox.

Vasundhara respects your privancy. No Spam!

message

Have a project in mind? Drop a message to Bansi Pipaliya & start the discussion!

Latest 13 Web Development Trends To Expect In 2022
April 11, 2022 Category : company news

Revealing Vasundhara’s New Identity

Read More
Leave a Comment