How to Use Unity’s Resources Folder

Unity has several kinds of special folders. One of them is the Resources folder. Simple concept of storing assets is well-explained in the official documentation:

Generally, you create instances of assets in a scene to use them in gameplay but Unity also lets you load assets on demand from a script. You do this by placing the assets in a folder called Resources or a sub-folder (you can actually have any number of Resources folders and place them anywhere in the project). These assets can then be loaded using the Resources.Load function.

Still the reason why we might want to use the Resources folder may be a little confusing. First you have to understand how Unity build process is works and how Unity is able to access game assets.

Unity build process

Before you will build your game, you have to declare what scenes your game consists of. All of this can be done in Build Settings window.

build settings window

There are at least two reasons why Unity asks you to do this:

  • It needs to know what scene should be loaded first (the top scene)
  • It needs to know what assets should be included in your build (dependencies)

What are scene dependencies? They’re assets which are connected to the scene hierarchy in any way, usually as a component field.

Unity Logo object contains Sprite Renderer object that references Unity Logo asset.

Unity Logo object contains Sprite Renderer object that references Unity Logo asset.

The dependency diagram may look like this:

dependency diagram 1

In this case there are two scenes. Scene 1 is using Asset 1 and Asset 2. Scene 2 is using Asset 2 and Asset 3. What happens if you decide not to build Scene 2?

dependency diagram 2

Only Asset 1 and Asset 2 will be included in the build since Asset 3 is referenced only by Scene 2, that is no longer included in the build. Thanks to this dependency tracking Unity will include in your build only these assets, which are actually used. Needless to say that you don’t have to worry about storing assets you’re not using at the moment. It will not affect your build size in any way.

Override!

There’s a way to get around this process. If you put your assets into a Resources folder, they will be always included in your build. But be careful! You need a really good reason to do so!

As I said before, in most cases when you need to use an asset, you make a reference to it within a scene. It’s really easy to use any kind of attached asset this way. So why would you need to use an asset without keeping a reference to it? There may be several reasons and each one depends on specific needs of the, but let’s look at one case what is quite common for most games.

When an asset is directly referenced from the scene, it will be loaded into the memory before your scene will be launched. Thankfully to that, player will not experience any frame-drops related to assets loading (with small exceptions). The price is of course the time needed for these assets to be loaded. Sometimes it may be not acceptable.

Example – loading screen with different backgrounds

Many game loading screens are displaying random images to be less boring.

Many game loading screens are displaying random images to be less boring.

Loading screen is something that usually is also a scene. Let’s think of a case when you want to display a random image on the background while your actual game level is loading. You’ve collected 15 images and you add these to loading scene images rotation script. It is working great, but when you play your game you realize that your loading scene requires more time to load than you need to pass your actual game levels!

This is caused by assets pre-loading mechanism and can be easily fixed using Resources folder. First remove all the references to your textures from the scene. Then put your images into Resources/LoadingImages directory like this:

resources images

Then somewhere in the code you can use a code like this one:

Note that Random.Range() returns a random number between first argument inclusive and second argument exclusive, that’s why there’s +1.

If you will need to attach this texture to an Image component, you can do it like this:

A word of caution

Use Resources folder only when you really need to. Loading assets on demand will make your FPS rate drop, and having indirect dependencies is makes your work much more difficult.  It’s worth to mention again that these assets will always be included in your build, even if you don’t use them. You have been warned!

Wrong Import Settings are Killing Your Unity Game [Part 1]

There are many things that may go wrong with your game development. Your models may have too much triangles than your target platform can handle, your algorithms may be too expensive for your CPU, and also you may be using too many materials so batching won’t work efficiently. These are difficult issues, and you as a game developer should always remember to keep a good balance between visuals and performance. Yet, there are some things so simple that we often forget about their existence, but these can have a serious impact on your game performance.

Texture Import Settings

When you’re adding a texture into your project, the Unity works magic with it – the texture is converted to a suitable format based on current texture import settings. For most cases default settings will be good enough, but the Unity cannot tell if something looks good enough for the player so it cannot change it without your knowledge. It’s where you step in.

To access Texture Import Settings just select one or more textures that you want to change. Your inspector will look more or less like this:

texture import settings basicThere are 3 main things that you should keep an eye out.

Texture Type

texture type

Texture Type is a way to tell Unity what this texture will be used for. All this options besides Advanced will adjust your texture internal settings to be optimal as possible for the selected purpose. Note that texture of type Texture is a simple diffuse texture used in 3D space and for 3D game should be used the most. Types descriptions can be found in the official manual.

The risk: Setting the wrong texture type may be not noticeable at first and you will get a performance loss. Some types are required for shaders to work correctly (like Normal map type must be set for a texture that will be used for bump mapping).

Size

texture size and compression

This is where you can decide how big your texture should be and how it should be stored in the device memory. Noticed that there’s a Max Size instead of Size? This is simply because you can reduce all the textures resolution for your game in Quality Settings.

quality settings texture quality

Also setting the 2048 as Max Size won’t guarantee that this texture will be of that size even if Quality Settings are set to the highest. The texture resolution will be lower if your original texture file is not big enough. Knowing that, it is a good practice to prepare textures bigger than you need them to be, because you can always scale them down.

The Risk: Some models may be small on screen and those models’ textures can have large resolutions. You can identify these objects by setting your Scene shading to Mipmaps:

scene shading

Format

The texture Format is another thing that may be something confusing.

texture format

When your Texture Type is set to anything else than Advanced you will have 4 options.

  • Compressed – It trades a little of GPU power for a much lower texture size. Compressed option will use the most suitable compressing algorithm for your target platforms.
  • 16 bits – Saves the texture without a compression but it is using 16 bit color palette. Good for textures with small amount of colors.
  • Truecolor – Saves the texture using uncompressed 32 bit color palette. Looks great, but large 2048×2048 texture will require almost 17 megs of memory.
  • Crunched – Compressed using compression format suitable for the GPU and then compressed again using compression format that can be handled only by the CPU.  Good for downloadable asset bundles.

As you can see, you’re trading here texture size for texture quality. My advice is to always set the format as Compressed until you will see that something is not right with the textures in your game.

There’s one more Texture Type option that we haven’t described. If you set your Texture Type to advanced, you will gain access to many more texture formats including more compression methods. Be aware of that not all compression formats may be supported by your target platform (e.g. many Android devices have different texture compression formats support, but all should support ETC).

The Risk: Compressed textures may look ugly for some kind of textures, but textures set to 16 bits or Truecolor may take up too much space. You can find textures that are taking up to much space looking at editor.log after building your game.

You can learn more about texture compression formats here.

Verifying

texture import information

The last thing you want to do is to verify your texture import information. Here at the bottom of your texture preview you will learn about:

  • Used texture size
  • Used compression format
  • Result texture size

Keep in mind that sometimes you will need to build your game before you will see it’s target size.

Continued in Part 2 here

Side note: You may be wondering why textures sizes are power-of-two numbers. If you’re curious why, please read this discussion.