diff --git a/spine-unity/Modules/com.esotericsoftware.spine.on-demand-loading/Editor/GenericOnDemandTextureLoaderInspector.cs b/spine-unity/Modules/com.esotericsoftware.spine.on-demand-loading/Editor/GenericOnDemandTextureLoaderInspector.cs index 50ce4aceb..6c135c349 100644 --- a/spine-unity/Modules/com.esotericsoftware.spine.on-demand-loading/Editor/GenericOnDemandTextureLoaderInspector.cs +++ b/spine-unity/Modules/com.esotericsoftware.spine.on-demand-loading/Editor/GenericOnDemandTextureLoaderInspector.cs @@ -203,18 +203,33 @@ namespace Spine.Unity.Editor { importer.SaveAndReimport(); if (resizePhysically) { + bool hasOverridesToEnable = + TextureImporterUtils.TryDisableOverrides(importer, out List disabledPlatforms); + Texture2D texture2D = AssetDatabase.LoadAssetAtPath(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(); } + + if (hasOverridesToEnable) { + TextureImporterUtils.EnableOverrides(importer, disabledPlatforms); + } } placeholderTexture = AssetDatabase.LoadAssetAtPath(texturePath); } @@ -374,7 +389,12 @@ namespace Spine.Unity.Editor { public void DeletePlaceholderTextures (GenericOnDemandTextureLoader loader) { foreach (var materialMap in loader.placeholderMap) { - Texture texture = materialMap.textures[0].placeholderTexture; + var textures = materialMap.textures; + if (textures == null || textures.Length == 0) { + continue; + } + + Texture texture = textures[0].placeholderTexture; if (texture) AssetDatabase.DeleteAsset(AssetDatabase.GetAssetPath(texture)); } diff --git a/spine-unity/Modules/com.esotericsoftware.spine.on-demand-loading/Editor/TextureImporterUtils.cs b/spine-unity/Modules/com.esotericsoftware.spine.on-demand-loading/Editor/TextureImporterUtils.cs new file mode 100644 index 000000000..4c6b78f3e --- /dev/null +++ b/spine-unity/Modules/com.esotericsoftware.spine.on-demand-loading/Editor/TextureImporterUtils.cs @@ -0,0 +1,61 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using UnityEditor; + +namespace Spine.Unity.Editor { + + /// + /// Utility class for working with TextureImporter. + /// + public static class TextureImporterUtils { + + private static IEnumerable GetAllPossiblePlatforms() { + BuildTarget[] buildTargets = (BuildTarget[])Enum.GetValues(typeof(BuildTarget)); + var platformNames = buildTargets.Select(x => x.ToString()).ToList(); + + // Add additional platforms that are not part of BuildTarget enum. + platformNames.Add("Server"); + + return platformNames.ToArray(); + } + + public static bool TryDisableOverrides(TextureImporter importer, out List disabledPlatforms) { + IEnumerable platforms = GetAllPossiblePlatforms(); + disabledPlatforms = new List(); + + foreach (string platform in platforms) { + var platformSettings = importer.GetPlatformTextureSettings(platform); + + if (!platformSettings.overridden) { + continue; + } + + disabledPlatforms.Add(platform); + platformSettings.overridden = false; + importer.SetPlatformTextureSettings(platformSettings); + } + + if (disabledPlatforms.Count <= 0) { + return false; + } + + importer.SaveAndReimport(); + return true; + } + + public static void EnableOverrides(TextureImporter importer, List platformsToEnable) { + if (platformsToEnable.Count == 0) { + return; + } + + foreach (string platform in platformsToEnable) { + var platformSettings = importer.GetPlatformTextureSettings(platform); + platformSettings.overridden = true; + importer.SetPlatformTextureSettings(platformSettings); + } + + importer.SaveAndReimport(); + } + } +} \ No newline at end of file diff --git a/spine-unity/Modules/com.esotericsoftware.spine.on-demand-loading/Editor/TextureImporterUtils.cs.meta b/spine-unity/Modules/com.esotericsoftware.spine.on-demand-loading/Editor/TextureImporterUtils.cs.meta new file mode 100644 index 000000000..201a599c5 --- /dev/null +++ b/spine-unity/Modules/com.esotericsoftware.spine.on-demand-loading/Editor/TextureImporterUtils.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: d7e6bbfca422433c9e60e78930eae065 +timeCreated: 1708382726 \ No newline at end of file diff --git a/spine-unity/Modules/com.esotericsoftware.spine.on-demand-loading/Runtime/GenericOnDemandTextureLoader.cs b/spine-unity/Modules/com.esotericsoftware.spine.on-demand-loading/Runtime/GenericOnDemandTextureLoader.cs index 95e33ce19..17913689f 100644 --- a/spine-unity/Modules/com.esotericsoftware.spine.on-demand-loading/Runtime/GenericOnDemandTextureLoader.cs +++ b/spine-unity/Modules/com.esotericsoftware.spine.on-demand-loading/Runtime/GenericOnDemandTextureLoader.cs @@ -194,7 +194,13 @@ namespace Spine.Unity { loadedDataAtMaterial = new MaterialOnDemandData[placeholderMap.Length]; for (int i = 0, count = loadedDataAtMaterial.Length; i < count; ++i) { loadedDataAtMaterial[i].lastFrameRequested = -1; - int texturesAtMaterial = placeholderMap[i].textures.Length; + + var textures = placeholderMap[i].textures; + if (textures == null) { + continue; + } + + int texturesAtMaterial = textures.Length; loadedDataAtMaterial[i].textureRequests = new TextureRequest[texturesAtMaterial]; } } @@ -264,6 +270,10 @@ namespace Spine.Unity { System.Action onTextureLoaded) { PlaceholderTextureMapping[] placeholderTextures = placeholderMap[materialIndex].textures; + if (placeholderTextures == null || textureIndex >= placeholderTextures.Length) { + return null; + } + TargetReference targetReference = placeholderTextures[textureIndex].targetTextureReference; loadedDataAtMaterial[materialIndex].lastFrameRequested = Time.frameCount;