Merge pull request #2461 from sandolkakos/4.1_fix-ondemand-exceptions

Fix some On-demand exceptions + add logic to keep original TextureImporter settings
This commit is contained in:
Harald Csaszar 2024-02-26 15:58:44 +01:00 committed by GitHub
commit 5281177b57
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 100 additions and 6 deletions

View File

@ -203,18 +203,33 @@ namespace Spine.Unity.Editor {
importer.SaveAndReimport();
if (resizePhysically) {
bool hasOverridesToEnable =
TextureImporterUtils.TryDisableOverrides(importer, out List<string> disabledPlatforms);
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();
}
if (hasOverridesToEnable) {
TextureImporterUtils.EnableOverrides(importer, disabledPlatforms);
}
}
placeholderTexture = AssetDatabase.LoadAssetAtPath<Texture>(texturePath);
}
@ -374,7 +389,12 @@ namespace Spine.Unity.Editor {
public void DeletePlaceholderTextures (GenericOnDemandTextureLoader<TargetReference, TextureRequest> 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));
}

View File

@ -0,0 +1,61 @@
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
namespace Spine.Unity.Editor {
/// <summary>
/// Utility class for working with TextureImporter.
/// </summary>
public static class TextureImporterUtils {
private static IEnumerable<string> 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<string> disabledPlatforms) {
IEnumerable<string> platforms = GetAllPossiblePlatforms();
disabledPlatforms = new List<string>();
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<string> platformsToEnable) {
if (platformsToEnable.Count == 0) {
return;
}
foreach (string platform in platformsToEnable) {
var platformSettings = importer.GetPlatformTextureSettings(platform);
platformSettings.overridden = true;
importer.SetPlatformTextureSettings(platformSettings);
}
importer.SaveAndReimport();
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: d7e6bbfca422433c9e60e78930eae065
timeCreated: 1708382726

View File

@ -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<Texture> onTextureLoaded) {
PlaceholderTextureMapping[] placeholderTextures = placeholderMap[materialIndex].textures;
if (placeholderTextures == null || textureIndex >= placeholderTextures.Length) {
return null;
}
TargetReference targetReference = placeholderTextures[textureIndex].targetTextureReference;
loadedDataAtMaterial[materialIndex].lastFrameRequested = Time.frameCount;