Skip to content

Instantly share code, notes, and snippets.

@Petethegoat
Created October 1, 2019 05:23
Show Gist options
  • Save Petethegoat/b831940eedf9a5ae7dc422b2953eaa5c to your computer and use it in GitHub Desktop.
Save Petethegoat/b831940eedf9a5ae7dc422b2953eaa5c to your computer and use it in GitHub Desktop.
using UnityEngine;
using UnityEditor;
public class TextureArrayWizard : ScriptableWizard
{
[SerializeField] private Texture2D[] textures = null;
[MenuItem("Assets/Create/Texture Array")]
static void CreateWizard()
{
var wiz = ScriptableWizard.DisplayWizard<TextureArrayWizard>("Create Texture Array", "Create");
wiz.textures = new Texture2D[Selection.objects.Length];
for(int i = 0; i < Selection.objects.Length; i++)
{
Texture2D tex = (Texture2D) Selection.objects[i];
if(tex != null)
wiz.textures[i] = tex;
}
}
void OnWizardCreate()
{
if(textures.Length == 0)
return;
string path = EditorUtility.SaveFilePanelInProject("Save Texture Array", "Texture Array", "asset", "Save Texture Array");
if(path.Length == 0)
return;
//Try to find an existing asset, to update instead of creating fresh
var oldTexArray = AssetDatabase.LoadAssetAtPath(path, typeof(Texture2DArray));
Texture2D t = textures[0];
Texture2DArray tArray = new Texture2DArray(t.width, t.height, textures.Length, t.format, t.mipmapCount > 1);
tArray.filterMode = t.filterMode;
tArray.wrapMode = t.wrapMode;
for(int i = 0; i < textures.Length; i++)
{
for(int m = 0; m < t.mipmapCount; m++)
{
Graphics.CopyTexture(textures[i], 0, m, tArray, i, m);
}
}
if(oldTexArray != null)
{
AssetDatabase.RemoveObjectFromAsset(oldTexArray);
AssetDatabase.AddObjectToAsset(tArray, path);
AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate);
AssetDatabase.Refresh();
}
else
{
AssetDatabase.CreateAsset(tArray, path);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment