Fix for ArgumentException: Texture2D.SetPixels: texture uses an unsupported format

This commit is contained in:
Marllon Vilano 2024-02-19 21:06:16 -03:00
parent 0a3b5dbb31
commit 12e424bee1

View File

@ -206,13 +206,21 @@ namespace Spine.Unity.Editor {
Texture2D texture2D = AssetDatabase.LoadAssetAtPath<Texture2D>(texturePath);
if (texture2D) {
Color[] maxTextureSizePixels = texture2D.GetPixels();
texture2D.SetPixels(maxTextureSizePixels);
var bytes = texture2D.EncodeToPNG();
// SetPixels works only for non-compressed textures using certain formats.
var nonCompressedTexture =
new Texture2D(texture2D.width, texture2D.height, TextureFormat.RGBA32, false);
nonCompressedTexture.SetPixels(maxTextureSizePixels);
var bytes = nonCompressedTexture.EncodeToPNG();
string targetPath = Application.dataPath + "/../" + texturePath;
System.IO.File.WriteAllBytes(targetPath, bytes);
texture2D.Apply(updateMipmaps: true, makeNoLongerReadable: true);
EditorUtility.SetDirty(texture2D);
importer.isReadable = false;
importer.SaveAndReimport();
EditorUtility.SetDirty(nonCompressedTexture);
AssetDatabase.SaveAssets();
}
}