If you’re a game developer, you know how important it is to optimize your game’s performance. One way to do this is by using texture arrays, which can significantly reduce the number of draw calls and improve your game’s frame rate. In this post, I’ll show you how to use texture arrays in Unity to optimize your game’s performance.

Texture arrays? Are they like texture atlases?

Texture arrays are a type of texture that allows you to store multiple textures in a single asset. Each texture in the array is assigned an index, which can be used to access the texture in shaders.

Texture arrays are different from texture atlases, which combine multiple textures into a single large texture. While texture atlases are useful for reducing texture memory usage, they can actually increase the number of draw calls required to render your game. Texture arrays, on the other hand, can reduce the number of draw calls required, making them an effective tool for optimizing games.

Sounds interesting, but how to make those arrays?

Generating a texture array in Unity was a bit tricky since I did not find a direct option to create it by right-clicking and selecting “Create > Texture 2D Array”. So, I decided to create my own helper script to overcome this issue.

using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

public class Texture2DArrayCreator : MonoBehaviour
{
    public string fileName;
    public List<Texture2D> texturesToPack;

    public void ConvertToTexture2DArray(string fileName)
    {
        if (fileName == null)
        {
            Debug.Log("no valid file name...");
            return;
        }

        if (texturesToPack.Count == 0)
        {
            Debug.Log("no textures to pack...");
            return;
        }

        Texture2DArray arr = new Texture2DArray(
            texturesToPack[0].width, texturesToPack[0].height,
            texturesToPack.Count, texturesToPack[0].format, false
        );

        for (int i = 0; i < texturesToPack.Count; i++)
        {
            arr.SetPixels(texturesToPack[i].GetPixels(), i);
        }

        arr.Apply();
        AssetDatabase.CreateAsset(arr, "Assets/" + fileName + ".asset");
    }
}

This script requires a list of textures that you want to include in the Texture2DArray, which you will need to add manually. You will also need to specify a file name for the new asset.

The accompanying editor script allows you to add a button to the inspector that executes the above method when pressed.

using UnityEngine;
using UnityEditor;

[CustomEditor(typeof(Texture2DArrayCreator))]
public class Texture2DArrayCreatorEditor : Editor
{
    public override void OnInspectorGUI()
    {
        Texture2DArrayCreator creator = (Texture2DArrayCreator)target;

        DrawDefaultInspector();

        if (GUILayout.Button("Create Texture2DArray"))
        {
            creator.ConvertToTexture2DArray(creator.fileName);
        }
    }
}

Nice work man! Anything else to keep in mind?

I utilized textures of the same size while creating the texture array and I am unsure if this script will function properly if you use textures of varying sizes. You also have to enable read/write and change texture format to anything but ‘Automatic’ in the texture import settings.

To further optimize your texture array, consider using compression or reducing the size of your textures before adding them to the array. This can help reduce the size of the texture array file and improve performance.

Bottom Line

Texture arrays are a powerful tool for optimizing the performance of your Unity game. By packing multiple textures into a single object, you can reduce the number of draw calls your game engine needs to make, leading to smoother gameplay and improved performance.

Hope this post was helpful and see you next time!

destructible object unity Previous post #3 game dev stuff | destructible objects
visual studio behaviour tree boilerplate code Next post #5 game dev stuff | enemy AI