Texture atlases and Texture Array in Unity are powerful tools for optimization and flexibility when working with textures in 3D and 2D projects. Their main purpose is to reduce the number of draw calls, thereby increasing game performance. If you are developing a scene with a large number of objects using different but similar textures (for example, tiles, ground, walls), then Texture Array will allow you to combine them into a single array without losing quality and control. In this article, we will figure out what Texture Array is, how it differs from regular atlases, and how to use it effectively in Unity.
What is Texture Array?
Unlike a classic texture atlas, where all images are collected into one large texture (with the loss of individual mipmapping and compression limitations), Texture2DArray in Unity is a special structure in which each texture remains independent, but is stored inside a single GPU resource. This means you can use different mip levels, compression formats, and easily access the desired layer in the shader.
Advantages of using Texture Array: - Support for the same resolution and format for all layers;
- Fewer draw calls if used together with instancing;
- Convenient switching between layers in shaders (for example, the same material, but with different textures);
- Ideal for procedural worlds, voxel rendering, terrains, custom tile systems.
Creating a Texture Array in Unity:
Preparing textures
All textures you want to include in the array must have the same resolution, format, and settings. In the Inspector, make sure that they have the sRGB (Color Texture) option disabled if you are working with linear maps (for example, normals), and Read/Write Enabled if you plan to create the array on the fly.
Texture2DArray Creation Script
using UnityEngine;
using UnityEditor;
public class TextureArrayGenerator : MonoBehaviour
{
[MenuItem("Tools/Create Texture2DArray")]
public static void CreateTextureArray()
{
string[] paths = new string[]
{
"Assets/Textures/Grass.png",
"Assets/Textures/Sand.png",
"Assets/Textures/Stone.png"
};
int size = 512;
Texture2DArray array = new Texture2DArray(size, size, paths.Length, TextureFormat.RGBA32, true);
for (int i = 0; i < paths.Length; i++)
{
Texture2D tex = AssetDatabase.LoadAssetAtPath(paths[i]);
for (int m = 0; m < tex.mipmapCount; m++)
Graphics.CopyTexture(tex, 0, m, array, i, m);
}
AssetDatabase.CreateAsset(array, "Assets/Textures/TerrainArray.asset");
AssetDatabase.SaveAssets();
Debug.Log("Texture2DArray created!");
}
}
After running the script, a new TerrainArray.asset file will appear in the Assets folder, ready to be used in a shader or material.
Using in Shader Graph or Custom Shader
In Shader Graph, you can create a Texture2DArray property, then use the Sample Texture 2D Array node, specifying the layer index via a float/int parameter.
In an HLSL shader, using Texture2DArray looks like this:
UNITY_DECLARE_TEX2DARRAY(_MyArray);
fixed4 col = UNITY_SAMPLE_TEX2DARRAY(_MyArray, float3(uv, layerIndex));
Here layerIndex is the layer number in the array (for example, 0 is grass, 1 is sand).
Passing Texture2DArray from C# script:
public class ApplyTextureArray : MonoBehaviour
{
public Material mat;
public Texture2DArray texArray;
void Start()
{
mat.SetTexture("_MyArray", texArray);
}
}
If you have meshes that use the same material but require different textures, simply pass the layer index as a shader parameter. This is especially powerful with GPU Instancing, where you can change on the fly which layer from the Texture Array should be displayed without creating duplicate materials.
Project applications: – Tile systems – if you have a terrain of tiles with different textures, Texture Array allows you to use one shader and simply change the layer index by tile.
– Voxel rendering – each block (grass, stone, dirt) can reference a layer in Texture2DArray.
– Biome rendering – for example, one material for all trees, but different foliage textures.
– Multi-surface terrain – using splat maps, you can mix multiple layers from the Texture Array.
Conclusion
Texture2DArray is a step forward compared to the classic texture atlas. It maintains flexibility and improves performance, especially when using textures en masse in a large world. Unity provides convenient tools for creating and using texture arrays both through code and through visual editors. If you are working on a large project and want to squeeze the most out of rendering, it is worth implementing Texture Array in your pipeline.
Game: Perform tasks and rest cool. 2145 people play!
Play gameGame: Perform tasks and rest cool. 2552 people play!
Play gameShaders are an essential part of any graphics-intensive application. Traditionally, shaders in Unity were created using...
Read moreShaders in Unity are a vital part of the visual design of games and applications, and understanding their principles ope...
Read moreA red crossed-out cross on a sprite in Unity is a visual indicator of an error, and most often it is associated with texture loading issues or incorrect material settings. Here are the main reasons why this may happen
Read more