三国卡牌客户端基础资源仓库
hch
2 天以前 8a96b03e8b5c04634c213cb17d6c3bccc6bd28e1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
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");
    }
}