Skip to content

Instantly share code, notes, and snippets.

@John-O-Really
Forked from MephestoKhaan/TextureArrayCreator.cs
Last active April 19, 2024 08:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save John-O-Really/db321a6ac05c7ba9572d2bbb26f4e795 to your computer and use it in GitHub Desktop.
Save John-O-Really/db321a6ac05c7ba9572d2bbb26f4e795 to your computer and use it in GitHub Desktop.
A quick wizard to create Texture Array assets from standard textures in unity, updated to use Graphics.CopyTexture for Unity URP, 2023.2
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEditorInternal;
namespace MK.Utilities
{
public class TextureArrayCreator : ScriptableWizard
{
[MenuItem("Window/Texture Array Creator")]
public static void ShowWindow()
{
ScriptableWizard.DisplayWizard<TextureArrayCreator>("Create Texture Array", "Build Asset");
}
public string path = "Assets/";
public string filename = "MyTextureArray";
public List<Texture2D> textures = new List<Texture2D>();
private ReorderableList list;
void OnWizardCreate()
{
CompileArray(textures, path, filename);
}
private void CompileArray(List<Texture2D> textures, string path, string filename)
{
if(textures == null || textures.Count == 0)
{
Debug.LogError("No textures assigned");
return;
}
Texture2D sample = textures[0];
Texture2DArray textureArray = new Texture2DArray(sample.width, sample.height, textures.Count, sample.format, false);
textureArray.filterMode = FilterMode.Trilinear;
textureArray.wrapMode = TextureWrapMode.Repeat;
for (int i = 0; i < textures.Count; i++)
{
Graphics.CopyTexture(textures[i], 0, 0, textureArray, i, 0);
}
textureArray.Apply();
string uri = path + filename+".asset";
AssetDatabase.CreateAsset(textureArray, uri);
Debug.Log("Saved asset to " + uri);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment