Skip to content

Instantly share code, notes, and snippets.

@MephestoKhaan
Created October 3, 2018 16:52
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save MephestoKhaan/8953d2f38195c9c15ced7ff4e9c632ef to your computer and use it in GitHub Desktop.
Save MephestoKhaan/8953d2f38195c9c15ced7ff4e9c632ef to your computer and use it in GitHub Desktop.
A quick wizard to create Texture Array assets from normal textures in unity
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++)
{
Texture2D tex = textures[i];
textureArray.SetPixels(tex.GetPixels(0), i, 0);
}
textureArray.Apply();
string uri = path + filename+".asset";
AssetDatabase.CreateAsset(textureArray, uri);
Debug.Log("Saved asset to " + uri);
}
}
}
@patrickjansendesign
Copy link

Any texture i try it gives error
"ArgumentException: Texture2D.GetPixels: texture data is either not readable, corrupted or does not exist."

@MephestoKhaan
Copy link
Author

@patrickjansendesign make sure you toggle read/write enabled in that texture settings

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment