From 51497d37d2ea18aa4e439250119bb6f01224f519 Mon Sep 17 00:00:00 2001
From: hch <305670599@qq.com>
Date: 星期五, 25 九月 2026 13:29:41 +0800
Subject: [PATCH] 0312 修复图集死循环问题
---
Assets/Editor/Tool/UpdateSpritePackingSetting.cs | 75 ++++++++++++++++++++++++-
Assets/Editor/Tool/TexturePlatformFormatHelper.cs | 17 +++++
Assets/Editor/Tool/TexturePlatformAssetPostprocessor.cs | 48 ++++++++++-----
3 files changed, 119 insertions(+), 21 deletions(-)
diff --git a/Assets/Editor/Tool/TexturePlatformAssetPostprocessor.cs b/Assets/Editor/Tool/TexturePlatformAssetPostprocessor.cs
index 2e8fafd..f326495 100644
--- a/Assets/Editor/Tool/TexturePlatformAssetPostprocessor.cs
+++ b/Assets/Editor/Tool/TexturePlatformAssetPostprocessor.cs
@@ -15,6 +15,11 @@
private static readonly string TARGET_FOLDER = TexturePlatformFormatHelper.TARGET_FOLDER;
private static readonly bool AUTO_APPLY_ATLAS_SETTINGS = true;
+ /// <summary>
+ /// 闃叉鍥炶皟涓� SaveAndReimport 鍚屾瑙﹀彂涓嬩竴娆� OnPostprocessAllAssets 閫犳垚宓屽閲嶅叆銆�
+ /// </summary>
+ private static bool s_IsApplying;
+
void OnPreprocessTexture()
{
if (!assetPath.StartsWith(TARGET_FOLDER))
@@ -38,30 +43,41 @@
{
if (!AUTO_APPLY_ATLAS_SETTINGS)
return;
+ // 鍥炶皟閲岀殑 SaveAndReimport 浼氬悓姝ュ啀娆¤Е鍙戞湰鍥炶皟锛屽繀椤绘尅鎺夛紝鍚﹀垯浼氭棤闄愯嚜瑙﹀彂
+ if (s_IsApplying)
+ return;
- foreach (var assetPath in importedAssets)
+ s_IsApplying = true;
+ try
{
- if (!assetPath.StartsWith(TARGET_FOLDER))
- continue;
- if (!assetPath.EndsWith(".spriteatlasv2") && !assetPath.EndsWith(".spriteatlas"))
- continue;
-
- try
+ foreach (var assetPath in importedAssets)
{
- var importer = SpriteAtlasImporter.GetAtPath(assetPath) as SpriteAtlasImporter;
- if (importer == null)
+ if (!assetPath.StartsWith(TARGET_FOLDER))
+ continue;
+ if (!assetPath.EndsWith(".spriteatlasv2") && !assetPath.EndsWith(".spriteatlas"))
continue;
- if (TexturePlatformFormatHelper.ApplyAtlasPlatformSettings(importer))
+ try
{
- importer.SaveAndReimport();
- Debug.Log($"[TexImport] 鍥鹃泦: {assetPath}");
+ var importer = SpriteAtlasImporter.GetAtPath(assetPath) as SpriteAtlasImporter;
+ if (importer == null)
+ continue;
+
+ if (TexturePlatformFormatHelper.ApplyAtlasPlatformSettings(importer))
+ {
+ importer.SaveAndReimport();
+ Debug.Log($"[TexImport] 鍥鹃泦: {assetPath}");
+ }
+ }
+ catch (System.Exception ex)
+ {
+ Debug.LogError($"[TexImport] 鍥鹃泦澶辫触 {assetPath}: {ex.Message}");
}
}
- catch (System.Exception ex)
- {
- Debug.LogError($"[TexImport] 鍥鹃泦澶辫触 {assetPath}: {ex.Message}");
- }
+ }
+ finally
+ {
+ s_IsApplying = false;
}
}
}
diff --git a/Assets/Editor/Tool/TexturePlatformFormatHelper.cs b/Assets/Editor/Tool/TexturePlatformFormatHelper.cs
index 8e9ab95..c3a0d94 100644
--- a/Assets/Editor/Tool/TexturePlatformFormatHelper.cs
+++ b/Assets/Editor/Tool/TexturePlatformFormatHelper.cs
@@ -142,15 +142,28 @@
return changed;
}
+ /// <summary>
+ /// 浼氳鐪熸鍐欏叆 meta 鐨勫浘闆嗗钩鍙帮紙鍙湁 overridden=true 鐨勫钩鍙拌缃墠浼氳鎸佷箙鍖栵級銆�
+ /// Standalone / DefaultTexturePlatform 鎸夐渶姹備繚鎸侀粯璁わ紝涓嶅仛瑕嗙洊銆�
+ /// </summary>
+ public static readonly string[] OVERRIDDEN_ATLAS_PLATFORMS = { "Android", "iPhone", "WebGL" };
+
+ /// <summary>
+ /// 缁熶竴鍥鹃泦骞冲彴鐨� maxTextureSize銆�
+ /// 娉ㄦ剰锛氱粷涓嶈兘瀵� overridden=false 鐨勫钩鍙板啓 maxTextureSize 鈥斺�� 杩欑被璁剧疆涓嶄細琚寔涔呭寲锛�
+ /// 姣忔瀵煎叆璇诲洖鏉ヤ粛鏄粯璁ゅ�硷紝浼氳鍒ゆ垚"鏈夋敼鍔�"浠庤�屽湪 OnPostprocessAllAssets 閲屽弽澶�
+ /// SaveAndReimport锛屾渶缁堣Е鍙� Unity 鐨� "infinite import loop" 妫�娴嬨��
+ /// </summary>
public static bool ApplyAtlasMaxTextureSize(SpriteAtlasImporter importer)
{
bool changed = false;
- foreach (var platform in new[] { "Standalone", "Android", "iPhone", "WebGL", "DefaultTexturePlatform" })
+ foreach (var platform in OVERRIDDEN_ATLAS_PLATFORMS)
{
var settings = importer.GetPlatformSettings(platform);
- if (settings.maxTextureSize == ATLAS_MAX_SIZE)
+ if (settings.overridden && settings.maxTextureSize == ATLAS_MAX_SIZE)
continue;
+ settings.overridden = true;
settings.maxTextureSize = ATLAS_MAX_SIZE;
importer.SetPlatformSettings(settings);
changed = true;
diff --git a/Assets/Editor/Tool/UpdateSpritePackingSetting.cs b/Assets/Editor/Tool/UpdateSpritePackingSetting.cs
index ab7a208..bcb368c 100644
--- a/Assets/Editor/Tool/UpdateSpritePackingSetting.cs
+++ b/Assets/Editor/Tool/UpdateSpritePackingSetting.cs
@@ -13,8 +13,34 @@
static string rootPath = Application.dataPath + "/ResourcesOut/Sprite";
static string spriteRelativePath = "Assets/ResourcesOut/Sprite";
+ /// <summary>
+ /// 鍥鹃泦澶勭悊娴佺▼鐨勯噸鍏ヤ繚鎶わ細娴佺▼鍐呴儴浼氬ぇ閲� SaveAndReimport / ImportAsset锛�
+ /// 杩欎簺閮戒細瑙﹀彂 AssetPostprocessor锛岃嫢涓嶆尅浣忛噸鍏ュ氨浼氫笌鎵瑰鐞�/鑿滃崟瑙﹀彂褰㈡垚浜掔浉瀵煎叆銆�
+ /// </summary>
+ private static bool s_IsRunning;
+
[MenuItem("绋嬪簭/Sprite/鏍囧噯鍖朣prite璁剧疆")]
public static void UpdateAllSpritePackingSetting()
+ {
+ if (s_IsRunning)
+ {
+ Debug.LogWarning("[SpritePack] 涓婁竴娆″浘闆嗗鐞嗗皻鏈粨鏉燂紝宸茶烦杩囨湰娆¤皟鐢�");
+ return;
+ }
+
+ s_IsRunning = true;
+ try
+ {
+ UpdateAllSpritePackingSettingInternal();
+ }
+ finally
+ {
+ s_IsRunning = false;
+ EditorUtility.ClearProgressBar();
+ }
+ }
+
+ private static void UpdateAllSpritePackingSettingInternal()
{
var path = "Assets/Editor/SpritePacking/SpriteSettings.asset";
var so = AssetDatabase.LoadAssetAtPath<SpriteSettingConfig>(path);
@@ -107,7 +133,7 @@
EditorUtility.ClearProgressBar();
- SpritePack();
+ SpritePackInternal();
}
[MenuItem("绋嬪簭/Sprite/鎵撳寘BuiltIn鍥鹃泦")]
@@ -151,6 +177,11 @@
}
};
spriteAtlas = CreateSpriteAtlas(spriteRelativePath_BuiltIn, setting);
+ if (spriteAtlas == null)
+ {
+ Debug.LogError($"[SpritePack] 鍒涘缓 BuiltIn 鍥鹃泦澶辫触: {builtInSpritePath}");
+ return;
+ }
}
List<string> excludePngs = new List<string>() { "Launch_1.png", "Launch_2.png", "Launch_3.png", "LoginBackGround.png", "TB_DL_Logo.png" };
@@ -166,6 +197,26 @@
[MenuItem("绋嬪簭/Sprite/鎵撳寘鍥鹃泦")]
public static void SpritePack()
+ {
+ if (s_IsRunning)
+ {
+ Debug.LogWarning("[SpritePack] 涓婁竴娆″浘闆嗗鐞嗗皻鏈粨鏉燂紝宸茶烦杩囨湰娆¤皟鐢�");
+ return;
+ }
+
+ s_IsRunning = true;
+ try
+ {
+ SpritePackInternal();
+ }
+ finally
+ {
+ s_IsRunning = false;
+ EditorUtility.ClearProgressBar();
+ }
+ }
+
+ private static void SpritePackInternal()
{
var path = "Assets/Editor/SpritePacking/SpriteSettings.asset";
var so = AssetDatabase.LoadAssetAtPath<SpriteSettingConfig>(path);
@@ -195,6 +246,11 @@
if (spriteAtlas == null)
{
spriteAtlas = CreateSpriteAtlas(spriteRelativePath, setting);
+ if (spriteAtlas == null)
+ {
+ Debug.LogError($"[SpritePack] 鍒涘缓鍥鹃泦澶辫触: {filePath}");
+ continue;
+ }
}
string spriteFilePath = rootPath + "/" + spritefilePathName;
@@ -233,8 +289,12 @@
string atlasPath = outPath + "/" + setting.folderName + ".spriteatlasv2";
CreateEmptySpriteAtlas(atlasPath);
-
SpriteAtlasImporter atlasImporter = SpriteAtlasImporter.GetAtPath(atlasPath) as SpriteAtlasImporter;
+ if (atlasImporter == null)
+ {
+ Debug.LogError($"[SpritePack] 鑾峰彇 SpriteAtlasImporter 澶辫触: {atlasPath}");
+ return null;
+ }
#region 鍥鹃泦鍩虹璁剧疆
@@ -288,7 +348,11 @@
{
SpriteAtlasAsset.Save(new SpriteAtlasAsset(), atlasPath);
AssetDatabase.SaveAssets();
- AssetDatabase.Refresh();
+
+ // 杩欓噷鍙兘鐢ㄧ簿纭鍏ワ紝涓嶈兘璋冪敤 AssetDatabase.Refresh()锛�
+ // 鍏ㄥ簱鍒锋柊浼氭妸鍒氬啓鍏ョ殑鍥鹃泦鍐嶆鎺ㄥ叆瀵煎叆娴佺▼锛岃繘鑰岃Е鍙� OnPostprocessAllAssets锛�
+ // 琚� Unity 鍒ゅ畾涓� "infinite import loop"锛圸ZBF.spriteatlasv2 鍙嶅 changed锛夈��
+ AssetDatabase.ImportAsset(atlasPath, ImportAssetOptions.ForceSynchronousImport);
}
@@ -308,6 +372,11 @@
FileName(spriteFilePath, excludePng);
SpriteAtlas spriteAtlasTmp = AssetDatabase.LoadAssetAtPath(filePath, typeof(UnityEngine.Object)) as SpriteAtlas;
+ if (spriteAtlasTmp == null)
+ {
+ Debug.LogError($"[SpritePack] 鏃犳硶鍔犺浇鍥鹃泦锛岃烦杩囨洿鏂�: {filePath}");
+ return;
+ }
// 鑾峰彇鍥鹃泦涓嬪浘鐗�
List<UnityEngine.Object> packables = new List<UnityEngine.Object>(spriteAtlasTmp.GetPackables());
--
Gitblit v1.8.0