三国卡牌客户端基础资源仓库
hch
2025-06-06 2feb3add98556a7b6243ba01a0119bfb5cbab259
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;
using System;
using UnityEditor.U2D;
using UnityEngine.U2D;
 
 
public class UpdateSpritePackingSetting
{
    static string rootPath = Application.dataPath + "/ResourcesOut/Sprite";
    static string spriteRelativePath = "Assets/ResourcesOut/Sprite";
 
    [MenuItem("程序/Sprite/标准化Sprite设置")]
    public static void UpdateAllSpritePackingSetting()
    {
        var path = "Assets/Editor/SpritePacking/SpriteSettings.asset";
        var so = AssetDatabase.LoadAssetAtPath<SpriteSettingConfig>(path);
 
        if (so == null)
        {
            return;
        }
 
        foreach (var setting in so.spriteSettings)
        {
            var directoryPath = StringUtility.Contact(rootPath, Path.DirectorySeparatorChar, setting.folderName);
 
            if (!Directory.Exists(directoryPath))
            {
                continue;
            }
 
            var files = new DirectoryInfo(directoryPath).GetFiles("*.png", SearchOption.TopDirectoryOnly);
 
            var total = files.Length;
            var count = 0;
 
 
 
            foreach (var file in files)
            {
                try
                {
                    var folderName = setting.folderName;
                    var importerPath = spriteRelativePath + "/" + folderName + "/" + file.Name;
                    var importer = AssetImporter.GetAtPath(importerPath);
 
                    if (importer == null)
                    {
                        continue;
                    }
 
                    var textureImporter = importer as TextureImporter;
                    if (textureImporter == null)
                    {
                        continue;
                    }
                    bool isChange = false;
                    if (textureImporter.textureType != TextureImporterType.Sprite)
                    {
                        textureImporter.textureType = TextureImporterType.Sprite;
                        isChange = true;
                    }
 
                    if (textureImporter.spriteImportMode != SpriteImportMode.Single)
                    {
                        textureImporter.spriteImportMode = SpriteImportMode.Single;
                        isChange = true;
                    }
 
                    if (isChange)
                    {
                        AssetDatabase.ImportAsset(importerPath);
                        EditorUtility.SetDirty(textureImporter);
                    }
 
                    count++;
 
                    EditorUtility.DisplayProgressBar(StringUtility.Contact("设置图集标签:", setting.folderName), file.Name, count / (float)total);
                }
                catch (Exception ex)
                {
                    Debug.Log(ex);
                    EditorUtility.ClearProgressBar();
                }
            }
 
        }
 
        AssetDatabase.SaveAssets();
        AssetDatabase.Refresh();
 
        EditorUtility.ClearProgressBar();
 
        SpritePack();
    }
 
    [MenuItem("程序/Sprite/打包BuiltIn图集")]
    public static void SpritePack_BuiltIn()
    {
        string rootPath_BuiltIn = Application.dataPath + "/ResourcesOut/BuiltIn/Sprites";
        string spriteRelativePath_BuiltIn = "Assets/ResourcesOut/BuiltIn/Sprites";
        var builtInSpritePath = spriteRelativePath_BuiltIn + "/sprites.spriteatlasv2";
        SpriteAtlasAsset spriteAtlas = SpriteAtlasAsset.Load(builtInSpritePath);
 
        // 不存在则创建后更新图集
        if (spriteAtlas == null)
        {
            var setting = new SpriteSettingConfig.SpriteSetting()
            {
                folderName = "sprites",
                blockOffset = 1,
                enableRotation = false,
                enableTightPacking = false,
                padding = 4,
                readable = false,
                generateMipMaps = false,
                sRGB = true,
                filterMode = FilterMode.Bilinear,
                platformSettings = new SpriteSettingConfig.PlatformSetting[]
                {
                    new SpriteSettingConfig.PlatformSetting()
                    {
                        name = "Android",
                        maxTextureSize = 2048,
                        textureCompression = TextureImporterCompression.Compressed,
                        textureFormat = TextureImporterFormat.ASTC_6x6
                    },
                    new SpriteSettingConfig.PlatformSetting()
                    {
                        name = "iPhone",
                        maxTextureSize = 2048,
                        textureCompression = TextureImporterCompression.Compressed,
                        textureFormat = TextureImporterFormat.ASTC_6x6
                    }
                }
            };
            spriteAtlas = CreateSpriteAtlas(spriteRelativePath_BuiltIn, setting);
        }
        List<string> excludePngs = new List<string>() { "Launch_1.png", "Launch_2.png", "Launch_3.png", "LoginBackGround.png", "TB_DL_Logo.png" };
 
        SpriteAtlas spriteAtlasTmp = AssetDatabase.LoadAssetAtPath(builtInSpritePath, typeof(UnityEngine.Object)) as SpriteAtlas;
        UpdateAtlas(spriteAtlas, rootPath_BuiltIn, excludePngs, builtInSpritePath);
 
        AssetDatabase.Refresh();
 
        Debug.Log("打包BuiltIn图集执行结束");
    }
 
    [MenuItem("程序/Sprite/打包图集")]
    public static void SpritePack()
    {
        var path = "Assets/Editor/SpritePacking/SpriteSettings.asset";
        var so = AssetDatabase.LoadAssetAtPath<SpriteSettingConfig>(path);
 
        if (so == null)
        {
            return;
        }
 
        int index = 0;
        foreach (SpriteSettingConfig.SpriteSetting setting in so.spriteSettings)
        {
            var directoryPath = StringUtility.Contact(rootPath, Path.DirectorySeparatorChar, setting.folderName);
 
            if (!Directory.Exists(directoryPath))
            {
                continue;
            }
            
 
            spritefilePathName = setting.folderName;
 
            var filePath = spriteRelativePath + "/" + spritefilePathName + ".spriteatlasv2";
            SpriteAtlasAsset spriteAtlas = SpriteAtlasAsset.Load(filePath);
 
            // 不存在则创建后更新图集
            if (spriteAtlas == null)
            {
                spriteAtlas = CreateSpriteAtlas(spriteRelativePath, setting);
            }
 
            string spriteFilePath = rootPath + "/" + spritefilePathName;
            
 
            UpdateAtlas(spriteAtlas, spriteFilePath, new List<string>(), filePath);
 
            // 打包进度
            EditorUtility.DisplayProgressBar("打包图集中...", "正在处理:" + spritefilePathName, index / so.spriteSettings.Length);
            index++;
        }
 
        EditorUtility.ClearProgressBar();
        AssetDatabase.Refresh();
        UpdateSpriteSetting.SetAllSpriteAssetBundleName();
 
        Debug.Log("打包图集执行结束");
 
    }
 
 
 
    /// <summary>
    /// 每个需要打图集的文件夹名 -- 即图集名
    /// </summary>
    private static string spritefilePathName;
 
    /// <summary>
    /// 创建图集
    /// </summary>
    /// <param name="atlasName">图集名字</param>
    private static SpriteAtlasAsset CreateSpriteAtlas(string outPath, SpriteSettingConfig.SpriteSetting setting)
    {
        string atlasPath = outPath + "/" + setting.folderName + ".spriteatlasv2";
        CreateEmptySpriteAtlas(atlasPath);
 
        
        SpriteAtlasImporter atlasImporter = SpriteAtlasImporter.GetAtPath(atlasPath) as SpriteAtlasImporter;
 
        #region 图集基础设置
 
        SpriteAtlasPackingSettings packSetting = new SpriteAtlasPackingSettings()
        {
            blockOffset = setting.blockOffset,
            enableRotation = setting.enableRotation,
            enableTightPacking = setting.enableTightPacking,
            padding = setting.padding,
        };
        atlasImporter.packingSettings = packSetting;
 
        #endregion
 
        #region 图集纹理设置
 
        SpriteAtlasTextureSettings textureSettings = new SpriteAtlasTextureSettings()
        {
            readable = setting.readable,
            generateMipMaps = setting.generateMipMaps,
            sRGB = setting.sRGB,
            filterMode = setting.filterMode,
        };
        atlasImporter.textureSettings = textureSettings;
 
        #endregion
 
        #region 分平台设置图集格式
 
        foreach (SpriteSettingConfig.PlatformSetting platformSetting in setting.platformSettings)
        {
            TextureImporterPlatformSettings settingPlatform = atlasImporter.GetPlatformSettings(platformSetting.name);
            settingPlatform.overridden = true;
            settingPlatform.maxTextureSize = platformSetting.maxTextureSize;
            settingPlatform.textureCompression = platformSetting.textureCompression;
            settingPlatform.format = platformSetting.textureFormat;
            atlasImporter.SetPlatformSettings(settingPlatform);
        }
 
        #endregion
        atlasImporter.includeInBuild = true;
        atlasImporter.SaveAndReimport();
        //atlas.Add(objectsForPack);
        SpriteAtlasAsset atlas = SpriteAtlasAsset.Load(atlasPath);
        
 
        return atlas;
    }
 
    static void CreateEmptySpriteAtlas(string atlasPath)
    {
        SpriteAtlasAsset.Save(new SpriteAtlasAsset(), atlasPath);
        AssetDatabase.SaveAssets();
        AssetDatabase.Refresh();
    }
 
    
 
    /// <summary>
    /// 每个图集的所有图片路径  --  记得用之前清空
    /// </summary>
    private static List<string> textureFullName = new List<string>();
 
    /// <summary>
    /// 更新图集内容
    /// </summary>
    /// <param name="atlas">图集</param>
    static void UpdateAtlas(SpriteAtlasAsset atlas, string spriteFilePath, List<string> excludePng, string filePath)
    {
        textureFullName.Clear();
        FileName(spriteFilePath, excludePng);
 
        SpriteAtlas spriteAtlasTmp = AssetDatabase.LoadAssetAtPath(filePath, typeof(UnityEngine.Object)) as SpriteAtlas;
 
        // 获取图集下图片
        List<UnityEngine.Object> packables = new List<UnityEngine.Object>(spriteAtlasTmp.GetPackables());
 
        List<UnityEngine.Object> objectsForPack = new List<UnityEngine.Object>();
        foreach (string item in textureFullName)
        {
            // 加载指定目录
            UnityEngine.Object spriteObj = AssetDatabase.LoadAssetAtPath(item, typeof(UnityEngine.Object));
            //Debug.Log("存png和jpg后缀的图片: " + item + " , " + !packables.Contains(spriteObj));
            if (!packables.Contains(spriteObj))
            {
                atlas.Add(new UnityEngine.Object[] { spriteObj });
            }
            objectsForPack.Add(spriteObj);
        }
        //删除packables下不存在的图片
        foreach (var item in packables)
        {
            if (!objectsForPack.Contains(item))
            {
                atlas.Remove(new UnityEngine.Object[] { item });
                Debug.Log(filePath + " 删除不存在的图片: " + item.name);
            }
        }
 
 
        SpriteAtlasAsset.Save(atlas, filePath);
    }
 
    /// <summary>
    /// 递归文件夹下的图
    /// </summary>
    /// <param name="folderPath"></param>
    static void FileName(string folderPath, List<string> excludePng)
    {
        DirectoryInfo info = new DirectoryInfo(folderPath);
        string str = Application.dataPath.Replace(@"/", @"\");
 
        foreach (DirectoryInfo item in info.GetDirectories())
        {
            FileName(item.FullName, excludePng);
        }
        foreach (FileInfo item in info.GetFiles())
        {
            // 存png和jpg后缀的图片
            if (item.FullName.EndsWith(".png", StringComparison.Ordinal) && !excludePng.Contains(item.Name))
            {
                textureFullName.Add("Assets" + item.FullName.Replace(str, ""));
            }
            if (item.FullName.EndsWith(".PNG", StringComparison.Ordinal))
            { 
                Debug.LogError("请将图片后缀改为小写: " + item.FullName);
            }
        }
    }
 
   
 
}