mirror of
https://github.com/EsotericSoftware/spine-runtimes.git
synced 2026-03-26 22:49:01 +08:00
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:
commit
5281177b57
@ -203,18 +203,33 @@ namespace Spine.Unity.Editor {
|
|||||||
importer.SaveAndReimport();
|
importer.SaveAndReimport();
|
||||||
|
|
||||||
if (resizePhysically) {
|
if (resizePhysically) {
|
||||||
|
bool hasOverridesToEnable =
|
||||||
|
TextureImporterUtils.TryDisableOverrides(importer, out List<string> disabledPlatforms);
|
||||||
|
|
||||||
Texture2D texture2D = AssetDatabase.LoadAssetAtPath<Texture2D>(texturePath);
|
Texture2D texture2D = AssetDatabase.LoadAssetAtPath<Texture2D>(texturePath);
|
||||||
if (texture2D) {
|
if (texture2D) {
|
||||||
Color[] maxTextureSizePixels = texture2D.GetPixels();
|
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;
|
string targetPath = Application.dataPath + "/../" + texturePath;
|
||||||
System.IO.File.WriteAllBytes(targetPath, bytes);
|
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();
|
AssetDatabase.SaveAssets();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (hasOverridesToEnable) {
|
||||||
|
TextureImporterUtils.EnableOverrides(importer, disabledPlatforms);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
placeholderTexture = AssetDatabase.LoadAssetAtPath<Texture>(texturePath);
|
placeholderTexture = AssetDatabase.LoadAssetAtPath<Texture>(texturePath);
|
||||||
}
|
}
|
||||||
@ -374,7 +389,12 @@ namespace Spine.Unity.Editor {
|
|||||||
|
|
||||||
public void DeletePlaceholderTextures (GenericOnDemandTextureLoader<TargetReference, TextureRequest> loader) {
|
public void DeletePlaceholderTextures (GenericOnDemandTextureLoader<TargetReference, TextureRequest> loader) {
|
||||||
foreach (var materialMap in loader.placeholderMap) {
|
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)
|
if (texture)
|
||||||
AssetDatabase.DeleteAsset(AssetDatabase.GetAssetPath(texture));
|
AssetDatabase.DeleteAsset(AssetDatabase.GetAssetPath(texture));
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,3 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: d7e6bbfca422433c9e60e78930eae065
|
||||||
|
timeCreated: 1708382726
|
||||||
@ -194,7 +194,13 @@ namespace Spine.Unity {
|
|||||||
loadedDataAtMaterial = new MaterialOnDemandData[placeholderMap.Length];
|
loadedDataAtMaterial = new MaterialOnDemandData[placeholderMap.Length];
|
||||||
for (int i = 0, count = loadedDataAtMaterial.Length; i < count; ++i) {
|
for (int i = 0, count = loadedDataAtMaterial.Length; i < count; ++i) {
|
||||||
loadedDataAtMaterial[i].lastFrameRequested = -1;
|
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];
|
loadedDataAtMaterial[i].textureRequests = new TextureRequest[texturesAtMaterial];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -264,6 +270,10 @@ namespace Spine.Unity {
|
|||||||
System.Action<Texture> onTextureLoaded) {
|
System.Action<Texture> onTextureLoaded) {
|
||||||
|
|
||||||
PlaceholderTextureMapping[] placeholderTextures = placeholderMap[materialIndex].textures;
|
PlaceholderTextureMapping[] placeholderTextures = placeholderMap[materialIndex].textures;
|
||||||
|
if (placeholderTextures == null || textureIndex >= placeholderTextures.Length) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
TargetReference targetReference = placeholderTextures[textureIndex].targetTextureReference;
|
TargetReference targetReference = placeholderTextures[textureIndex].targetTextureReference;
|
||||||
loadedDataAtMaterial[materialIndex].lastFrameRequested = Time.frameCount;
|
loadedDataAtMaterial[materialIndex].lastFrameRequested = Time.frameCount;
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user