Skip to content

Texture2DArrayUtilities

Description

Unity Editor Only

Various helper functions for Texture2DArrays including:

  • Creating a Texture Array
  • Modifying a Texture Array
  • Retrieving textures from a Texture Array

CreateArray

Declaration

public static Texture2DArray CreateArray(Texture2D[] textures, bool compressed = true, bool transferMipmaps = true, bool linear = false)

Parameters

Parameter Description
textures The textures that will be in the array
compressed Default: true
Whether the output array will be in the format DXT5 (Compressed) or RGBA32 (Uncompressed)
transferMipmaps Default: true
If mipmaps will be transferred from the textures to the array
linear Default: false
If the output array will be linear
Recommended in the Built-In Render Pipeline only when including normal maps
Not Recommended in URP/HDRP as it will result in brighter textures

Returns

A Texture2DArray with the inputted textures and settings

Description

Creates a new Texture2DArray with the given textures taking into account unsupported formats

Resolution will be the resolution of the first input texture

Texture will be skipped if it is not the same resolution of the first

Example of creating an array and saving it to the Assets folder

private void OnGUI()
{
    // Replace with your textures
    Texture2D[] textures;

    // Button that creates the array
    // In this example the array will have the default inputs
    if(GUILayout.Button("Create Array")) {
        // Create array with preset textures
        Texture2DArray array = Texture2DArrayUtilities.CreateArray(textures);

        // Save array to the Assets folder
        AssetDatabase.CreateAsset(array, "Assets/TextureArray.asset");
    }

}


CreateArrayAutoResize

Declaration

public static Texture2DArray CreateArrayAutoResize(Texture2D[] textures, bool compressed = true, bool transferMipmaps = true, bool linear = false)

Parameters

Parameter Description
textures The textures that will be in the array
compressed Default: true
Whether the output array will be in the format DXT5 (Compressed) or RGBA32 (Uncompressed)
transferMipmaps Default: true
If mipmaps will be transferred from the textures to the array
linear Default: false
If the output array will be linear
Recommended in the Built-In Render Pipeline only when including normal maps
Not Recommended in URP/HDRP as it will result in brighter textures

Returns

A Texture2DArray with the inputted textures and settings

Description

Creates a new Texture2DArray with the given textures taking into account unsupported formats

Resolution will be the resolution of the first input texture

Automatically resizes all textures to the resolution of the first

Example of creating an array and saving it to the Assets folder

private void OnGUI()
{
    // Replace with your textures
    // In this example imagine the second texture is a different resolution to the first
    // In this case the second texture will be resized to the resolution of the first
    Texture2D[] textures;

    // Button that creates the array
    // In this example the array will have the default inputs
    if(GUILayout.Button("Create Array")) {
        // Create array with preset textures
        // This will resize all textures to the resolution of the first if they are not already
        Texture2DArray array = Texture2DArrayUtilities.CreateArrayAutoResize(textures);

        // Save array to the Assets folder
        AssetDatabase.CreateAsset(array, "Assets/TextureArray.asset");
    }

}


CreateArrayUserInput

Declaration

public static Texture2DArray CreateArrayUserInput(Texture2D[] textures, int[] autoResizeIndexes = null, bool compressed = true, bool transferMipmaps = true, bool linear = false)

Parameters

Parameter Description
textures The textures that will be in the array
autoResizeIndexes Default: null
Automatically resizes all textures at these indexes if required instead of showing a prompt to the user
Ex. If this is { 0, 2 }, the textures at index 0 & 2 will be automatically resized
compressed Default: true
Whether the output array will be in the format DXT5 (Compressed) or RGBA32 (Uncompressed)
transferMipmaps Default: true
If mipmaps will be transferred from the textures to the array
linear Default: false
If the output array will be linear
Recommended in the Built-In Render Pipeline only when including normal maps
Not Recommended in URP/HDRP as it will result in brighter textures

Returns

A Texture2DArray with the inputted textures and settings

Description

Creates a new Texture2DArray with the given textures taking into account unsupported formats

Resolution will be the resolution of the first input texture

Checks for user input to decide whether to resize the texture or array if a texture is at a different resolution to the array

Example of creating an array and saving it to the Assets folder

private void OnGUI()
{
    // Replace with your textures
    Texture2D[] textures;

    // In this example the textures at index 0 and 2 will automatically be resized to the resolution of the first
    // Any other texture in the array will ask for user input
    int[] autoResizeIndexes = { 1, 2 };

    // Button that creates the array
    // In this example the array will have the default inputs
    if(GUILayout.Button("Create Array")) {
        // Create array with preset textures
        // This will resize any textures with an index in the autoResizeIndexes to the resolution of the first
        //  Any other texture in the array will ask for user input if it requires resizing
        Texture2DArray array = Texture2DArrayUtilities.CreateArrayUserInput(textures, autoResizeIndexes);

        // Save array to the Assets folder
        AssetDatabase.CreateAsset(array, "Assets/TextureArray.asset");
    }

}


UpdateTexture

Declaration

public static Texture2DArray UpdateTexture(Texture2DArray array, Texture2D texture, int index, bool transferMipmaps = true)

Parameters

Parameter Description
array The array that will be updated
texture The texture that will be inserted into the array
index The index which the texture will overwrite
transferMipmaps If mipmaps will be transferred from the texture to the array if possible

Returns

The modified array with the texture inserted

Description

Overwrites the texture in the input array at a given index to the input texture

Uses GPU functions to speed up operations

Update will not be applied if the texture resolution is not the same as the array

Example of inserting a texture into a texture array

private void OnGUI()
{
    // Replace with your texture and array
    Texture2DArray array;
    Texture2D texture;

    // Button that updates the array
    if(GUILayout.Button("Update Array")) {
        // Update array with new texture
        // In this example the texture will be inserted into index 0
        array = Texture2DArrayUtilities.UpdateTexture(array, texture, 0);
    }

}


UpdateTextureAutoResize

Declaration

public static Texture2DArray UpdateTextureAutoResize(Texture2DArray array, Texture2D texture, int index, bool transferMipmaps = true)

Parameters

Parameter Description
array The array that will be updated
texture The texture that will be inserted into the array
index The index which the texture will overwrite
transferMipmaps Default: true
If mipmaps will be transferred from the texture to the array if possible

Returns

The modified array with the texture inserted

Description

Overwrites the texture in the input array at a given index to the input texture

Automatically resizes the input texture to the array size if it is a different resolution

Example of inserting a texture into a texture array

private void OnGUI()
{
    // Replace with your texture and array
    Texture2DArray array;
    Texture2D texture;

    // Button that updates the array
    if(GUILayout.Button("Update Array")) {
        // Update array with new texture
        // In this example the texture will be inserted into index 0
        // This will resize the texture to the array resolution if it is not already
        array = Texture2DArrayUtilities.UpdateTextureAutoResize(array, texture, 0);
    }

}


UpdateTextureUserInput

Declaration

public static (Texture2DArray, bool) UpdateTextureUserInput(Texture2DArray array, Texture2D texture, int index, bool transferMipmaps = true)

Parameters

Parameter Description
array The array that will be updated
texture The texture that will be inserted into the array
index The index which the texture will overwrite
transferMipmaps Default: true
If mipmaps will be transferred from the texture to the array if possible

Returns

Item1: Modified Array, Item2: User Cancelled

Description

Overwrites the texture in the input array at a given index to the input texture

Waits for the user to input the outcome when a texture is a different resolution to the array

Example of inserting a texture into a texture array

private void OnGUI()
{
    // Replace with your texture and array
    Texture2DArray array;
    Texture2D texture;

    // Button that updates the array
    if(GUILayout.Button("Update Array")) {
        // Update array with new texture
        // In this example the texture will be inserted into index 0
        // This will wait for user input if the texture requires resizing
        array = Texture2DArrayUtilities.UpdateTextureUserInput(array, texture, 0);
    }

}


GetTexture

Declaration

public static Texture2D GetTexture(Texture2DArray array, int index)

Parameters

Parameter Description
array The array that will be searched
index The index for which texture will be returned

Returns

The texture in the array at the specified index

Description

Retrieves the texture in an input array at a given index

Example of retrieving a texture from a texture array

private void Function()
{
    // Replace with your array
    Texture2DArray array;

    // This will retrieve a texture from the array
    // In this example the texture retrieved is at index 0
    Texture2D texture = Texture2DArrayUtilities.GetTexture(array, 0);
}


GetTextures

Declaration

public static Texture2D[] GetTextures(Texture2DArray array)

Parameters

Parameter Description
array The array that will be searched

Returns

An array with all the textures from the given array

Description

Retrieves all the textures in an input array

Example of retrieving a texture from a texture array

private void Function()
{
    // Replace with your array
    Texture2DArray array;

    // This will retrieve all the textures from the array
    Texture2D[] textures = Texture2DArrayUtilities.GetTextures(array);
}


ResizeArrayTextures

Declaration

public static Texture2DArray ResizeArrayTextures(Texture2DArray array, int newWidth, int newHeight, FilterMode resizeFilterMode = FilterMode.Bilinear)

Parameters

Parameter Description
array The array that will be searched
newWidth The width of the output textures
newHeight The height of the output textures
resizeFilterMode Default: FilterMode.Bilinear
The filter mode used when resizing the textures

Returns

The modified array with all the textures at the new resolution

Description

Scales all textures in the array to the new specified resolution

Skips textures already at the new resolution

Example of resizing an array through GUI inputs

private void OnGUI()
{
    // Replace with your array
    Texture2DArray array;

    // Button to resize array
    if(GUILayout.Button("Resize Array")) {
        // This will resize the array and all textures in it to the specified resolution
        // In this example the array will be resized to 2048x2048 using a Bilinear filter by default 
        array = ResizeArrayTextures(array, 2048, 2048);
    }
}