三国卡牌客户端基础资源仓库
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
using UnityEditor;
using UnityEditor.U2D;
using UnityEngine;
 
/// <summary>
/// 贴图平台压缩格式的公共常量、工具方法,以及平台设置的核心逻辑。
/// Batch 和 Import 只需调用 Apply 方法,不重复编写设置代码。
/// </summary>
public static class TexturePlatformFormatHelper
{
    // ---- 格式常量 ----
    public const TextureImporterFormat ANDROID_RGBA = TextureImporterFormat.ASTC_6x6;
    public const TextureImporterFormat ANDROID_RGB  = TextureImporterFormat.ASTC_6x6;
    public const TextureImporterFormat IOS_FORMAT   = TextureImporterFormat.ASTC_6x6;
    public const TextureImporterFormat WEBGL_FORMAT = TextureImporterFormat.ASTC_6x6;
 
    // ---- 目标文件夹 ----
    public const string TARGET_FOLDER = "Assets/ResourcesOut";
 
    // ---- 图集固定尺寸 ----
    public const int ATLAS_MAX_SIZE = 2048;
 
    // ---- 压缩质量 ----
    public const int COMPRESSION_QUALITY = 50;
 
    // ============================================================
    // 核心:对单个 importer 应用平台格式设置
    // ============================================================
 
    /// <summary>
    /// 对 TextureImporter 设置 Android=ETC2 / iPhone=ASTC / WebGL=ASTC。
    /// 返回 true 表示有改动,调用方负责 SaveAndReimport。
    /// </summary>
    public static bool ApplyTexturePlatformSettings(TextureImporter importer)
    {
        bool needsAlpha = NeedsAlpha(importer);
        var androidTarget = needsAlpha ? ANDROID_RGBA : ANDROID_RGB;
        int maxSize = GetMaxTextureSize(importer);
        bool changed = false;
 
        // Android → ETC2
        {
            var s = importer.GetPlatformTextureSettings("Android");
            if (!s.overridden || s.format != androidTarget)
            {
                s.overridden = true;
                s.maxTextureSize = maxSize;
                s.textureCompression = TextureImporterCompression.Compressed;
                s.format = androidTarget;
                s.compressionQuality = COMPRESSION_QUALITY;
                importer.SetPlatformTextureSettings(s);
                changed = true;
            }
        }
 
        // iPhone → ASTC
        {
            var s = importer.GetPlatformTextureSettings("iPhone");
            if (!s.overridden || s.format != IOS_FORMAT)
            {
                s.overridden = true;
                s.maxTextureSize = maxSize;
                s.textureCompression = TextureImporterCompression.Compressed;
                s.format = IOS_FORMAT;
                s.compressionQuality = COMPRESSION_QUALITY;
                importer.SetPlatformTextureSettings(s);
                changed = true;
            }
        }
 
        // WebGL → ASTC
        {
            var s = importer.GetPlatformTextureSettings("WebGL");
            if (!s.overridden || s.format != WEBGL_FORMAT)
            {
                s.overridden = true;
                s.maxTextureSize = maxSize;
                s.textureCompression = TextureImporterCompression.Compressed;
                s.format = WEBGL_FORMAT;
                s.compressionQuality = COMPRESSION_QUALITY;
                importer.SetPlatformTextureSettings(s);
                changed = true;
            }
        }
 
        return changed;
    }
 
    /// <summary>
    /// 对 SpriteAtlasImporter 设置 Android=ETC2_RGBA8 / iPhone=ASTC / WebGL=ASTC。
    /// 返回 true 表示有改动,调用方负责 SaveAndReimport。
    /// </summary>
    public static bool ApplyAtlasPlatformSettings(SpriteAtlasImporter importer)
    {
        bool changed = false;
 
        // Android → ETC2_RGBA8(图集始终含 Alpha)
        {
            var s = importer.GetPlatformSettings("Android");
            if (!s.overridden || s.format != ANDROID_RGBA)
            {
                s.overridden = true;
                s.maxTextureSize = ATLAS_MAX_SIZE;
                s.textureCompression = TextureImporterCompression.Compressed;
                s.format = ANDROID_RGBA;
                s.compressionQuality = COMPRESSION_QUALITY;
                importer.SetPlatformSettings(s);
                changed = true;
            }
        }
 
        // iPhone → ASTC
        {
            var s = importer.GetPlatformSettings("iPhone");
            if (!s.overridden || s.format != IOS_FORMAT)
            {
                s.overridden = true;
                s.maxTextureSize = ATLAS_MAX_SIZE;
                s.textureCompression = TextureImporterCompression.Compressed;
                s.format = IOS_FORMAT;
                s.compressionQuality = COMPRESSION_QUALITY;
                importer.SetPlatformSettings(s);
                changed = true;
            }
        }
 
        // WebGL → ASTC
        {
            var s = importer.GetPlatformSettings("WebGL");
            if (!s.overridden || s.format != WEBGL_FORMAT)
            {
                s.overridden = true;
                s.maxTextureSize = ATLAS_MAX_SIZE;
                s.textureCompression = TextureImporterCompression.Compressed;
                s.format = WEBGL_FORMAT;
                s.compressionQuality = COMPRESSION_QUALITY;
                importer.SetPlatformSettings(s);
                changed = true;
            }
        }
 
        return changed;
    }
 
    // ============================================================
    // 工具方法
    // ============================================================
 
    public static bool NeedsAlpha(TextureImporter importer)
    {
        if (importer.DoesSourceTextureHaveAlpha())
            return true;
 
        foreach (var platform in new[] { "Android", "iPhone", "Standalone" })
        {
            var s = importer.GetPlatformTextureSettings(platform);
            if (s.overridden && IsAlphaFormat(s.format))
                return true;
        }
 
        if (importer.alphaSource != TextureImporterAlphaSource.None)
            return importer.DoesSourceTextureHaveAlpha();
 
        return false;
    }
 
    public static bool IsAlphaFormat(TextureImporterFormat format)
    {
        switch (format)
        {
            case TextureImporterFormat.RGBA32:
            case TextureImporterFormat.RGBA16:
            case TextureImporterFormat.ARGB32:
            case TextureImporterFormat.ARGB16:
            case TextureImporterFormat.DXT5:
            case TextureImporterFormat.ETC2_RGBA8:
            case TextureImporterFormat.ASTC_4x4:
            case TextureImporterFormat.ASTC_5x5:
            case TextureImporterFormat.ASTC_6x6:
            case TextureImporterFormat.ASTC_8x8:
            case TextureImporterFormat.ASTC_10x10:
            case TextureImporterFormat.ASTC_12x12:
            case TextureImporterFormat.PVRTC_RGBA2:
            case TextureImporterFormat.PVRTC_RGBA4:
                return true;
            default:
                return false;
        }
    }
 
    public static int GetMaxTextureSize(TextureImporter importer)
    {
        foreach (var platform in new[] { "Android", "iPhone", "Standalone" })
        {
            var s = importer.GetPlatformTextureSettings(platform);
            if (s.overridden)
                return s.maxTextureSize;
        }
        return importer.maxTextureSize;
    }
}