Created
October 3, 2018 16:52
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Any texture i try it gives error
"ArgumentException: Texture2D.GetPixels: texture data is either not readable, corrupted or does not exist."