using UnityEditor;
|
using UnityEditor.U2D;
|
using UnityEngine;
|
|
/// <summary>
|
/// 批量设置贴图和图集的平台压缩格式:
|
/// - PC(Standalone):不设置覆盖,保持默认
|
/// - Android:ETC2(有Alpha→RGBA8 / 无Alpha→RGB4)
|
/// - iPhone:ASTC_6x6
|
/// - WebGL:ASTC_6x6
|
///
|
/// 图集只设置 SpriteAtlas 自身格式,不触碰其中的 Sprite。
|
///
|
/// 菜单:程序/Sprite/批量设置贴图平台格式
|
/// </summary>
|
public class BatchTexturePlatformSettings
|
{
|
private static readonly string TARGET_FOLDER = TexturePlatformFormatHelper.TARGET_FOLDER;
|
|
[MenuItem("程序/Sprite/批量设置贴图平台格式(全部)")]
|
public static void SetAll()
|
{
|
SetTextures();
|
SetSpriteAtlases();
|
AssetDatabase.SaveAssets();
|
AssetDatabase.Refresh();
|
}
|
|
[MenuItem("程序/Sprite/批量设置贴图平台格式(仅贴图)")]
|
public static void SetTexturesOnly()
|
{
|
SetTextures();
|
AssetDatabase.SaveAssets();
|
AssetDatabase.Refresh();
|
}
|
|
[MenuItem("程序/Sprite/批量设置贴图平台格式(仅图集)")]
|
public static void SetAtlasesOnly()
|
{
|
SetSpriteAtlases();
|
AssetDatabase.SaveAssets();
|
AssetDatabase.Refresh();
|
}
|
|
// ============================================================
|
// 贴图
|
// ============================================================
|
static void SetTextures()
|
{
|
var guids = AssetDatabase.FindAssets("t:Texture2D", new[] { TARGET_FOLDER });
|
int total = guids.Length;
|
int converted = 0;
|
int skipped = 0;
|
|
for (int i = 0; i < total; i++)
|
{
|
var assetPath = AssetDatabase.GUIDToAssetPath(guids[i]);
|
EditorUtility.DisplayProgressBar("批量设置贴图平台格式", $"[{i + 1}/{total}] {assetPath}", (float)i / total);
|
|
var importer = AssetImporter.GetAtPath(assetPath) as TextureImporter;
|
if (importer == null) { skipped++; continue; }
|
|
if (TexturePlatformFormatHelper.ApplyTexturePlatformSettings(importer))
|
{
|
importer.SaveAndReimport();
|
converted++;
|
Debug.Log($"[BatchTex] 贴图: {assetPath}");
|
}
|
else
|
{
|
skipped++;
|
}
|
}
|
|
EditorUtility.ClearProgressBar();
|
Debug.Log($"[BatchTex] 贴图完成:转换 {converted},跳过 {skipped},共 {total}");
|
EditorUtility.DisplayDialog("贴图格式设置完成",
|
$"转换 {converted} 个,跳过 {skipped} 个,共 {total} 个贴图。\nAndroid → ETC2\niPhone → ASTC_6x6\nWebGL → ASTC_6x6\nPC → 默认",
|
"OK");
|
}
|
|
// ============================================================
|
// 图集
|
// ============================================================
|
static void SetSpriteAtlases()
|
{
|
var guids = AssetDatabase.FindAssets("t:SpriteAtlas", new[] { TARGET_FOLDER });
|
int total = guids.Length;
|
int converted = 0;
|
int skipped = 0;
|
|
for (int i = 0; i < total; i++)
|
{
|
var assetPath = AssetDatabase.GUIDToAssetPath(guids[i]);
|
EditorUtility.DisplayProgressBar("批量设置图集平台格式", $"[{i + 1}/{total}] {assetPath}", (float)i / total);
|
|
try
|
{
|
var importer = SpriteAtlasImporter.GetAtPath(assetPath) as SpriteAtlasImporter;
|
if (importer == null) { skipped++; continue; }
|
|
if (TexturePlatformFormatHelper.ApplyAtlasPlatformSettings(importer))
|
{
|
importer.SaveAndReimport();
|
converted++;
|
Debug.Log($"[BatchTex] 图集: {assetPath}");
|
}
|
else
|
{
|
skipped++;
|
}
|
}
|
catch (System.Exception ex)
|
{
|
Debug.LogError($"[BatchTex] 图集失败 {assetPath}: {ex.Message}");
|
}
|
}
|
|
EditorUtility.ClearProgressBar();
|
Debug.Log($"[BatchTex] 图集完成:转换 {converted},跳过 {skipped},共 {total}");
|
EditorUtility.DisplayDialog("图集格式设置完成",
|
$"转换 {converted} 个,跳过 {skipped} 个,共 {total} 个图集。\nAndroid → ETC2_RGBA8\niPhone → ASTC_6x6\nWebGL → ASTC_6x6\nPC → 默认",
|
"OK");
|
}
|
}
|