三国卡牌客户端基础资源仓库
yyl
4 天以前 d5c61ed998a38d62a03f4d58e4d2fe9516d27ea3
自动图集设置

(cherry picked from commit 427d4b46d162bf5b3b50a4e681b7cc1e3d5c3c02)
4个文件已修改
74 ■■■■■ 已修改文件
Assets/Editor/Tool/FixSpriteAtlasWebGLAlpha.cs 6 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Assets/Editor/Tool/TexturePlatformAssetPostprocessor.cs 2 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Assets/Editor/Tool/TexturePlatformFormatHelper.cs 21 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Assets/Editor/Tool/UpdateSpritePackingSetting.cs 45 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Assets/Editor/Tool/FixSpriteAtlasWebGLAlpha.cs
@@ -36,14 +36,16 @@
                var webGLSettings = importer.GetPlatformSettings("WebGL");
                // 已有 WebGL 覆盖且已是目标格式则跳过
                if (webGLSettings.overridden && webGLSettings.format == TextureImporterFormat.ASTC_8x8)
                if (webGLSettings.overridden
                    && webGLSettings.maxTextureSize == TexturePlatformFormatHelper.ATLAS_MAX_SIZE
                    && webGLSettings.format == TextureImporterFormat.ASTC_8x8)
                {
                    continue;
                }
                // ASTC 8x8 — 含 Alpha 通道,与 Android/iPhone 保持一致
                webGLSettings.overridden = true;
                webGLSettings.maxTextureSize = 2048;
                webGLSettings.maxTextureSize = TexturePlatformFormatHelper.ATLAS_MAX_SIZE;
                webGLSettings.textureCompression = TextureImporterCompression.Compressed;
                webGLSettings.format = TextureImporterFormat.ASTC_8x8;
                webGLSettings.compressionQuality = 50;
Assets/Editor/Tool/TexturePlatformAssetPostprocessor.cs
@@ -13,7 +13,7 @@
public class TexturePlatformAssetPostprocessor : AssetPostprocessor
{
    private static readonly string TARGET_FOLDER = TexturePlatformFormatHelper.TARGET_FOLDER;
    private static readonly bool AUTO_APPLY_ATLAS_SETTINGS = false;
    private static readonly bool AUTO_APPLY_ATLAS_SETTINGS = true;
    void OnPreprocessTexture()
    {
Assets/Editor/Tool/TexturePlatformFormatHelper.cs
@@ -18,7 +18,7 @@
    public const string TARGET_FOLDER = "Assets/ResourcesOut";
    // ---- 图集固定尺寸 ----
    public const int ATLAS_MAX_SIZE = 2048;
    public const int ATLAS_MAX_SIZE = 1024;
    // ---- 压缩质量 ----
    public const int COMPRESSION_QUALITY = 50;
@@ -92,7 +92,7 @@
    /// </summary>
    public static bool ApplyAtlasPlatformSettings(SpriteAtlasImporter importer)
    {
        bool changed = false;
        bool changed = ApplyAtlasMaxTextureSize(importer);
        // Android → ETC2_RGBA8(图集始终含 Alpha)
        {
@@ -142,6 +142,23 @@
        return changed;
    }
    public static bool ApplyAtlasMaxTextureSize(SpriteAtlasImporter importer)
    {
        bool changed = false;
        foreach (var platform in new[] { "Standalone", "Android", "iPhone", "WebGL", "DefaultTexturePlatform" })
        {
            var settings = importer.GetPlatformSettings(platform);
            if (settings.maxTextureSize == ATLAS_MAX_SIZE)
                continue;
            settings.maxTextureSize = ATLAS_MAX_SIZE;
            importer.SetPlatformSettings(settings);
            changed = true;
        }
        return changed;
    }
    // ============================================================
    // 工具方法
    // ============================================================
Assets/Editor/Tool/UpdateSpritePackingSetting.cs
@@ -58,6 +58,18 @@
                    {
                        continue;
                    }
                    int sourceWidth;
                    int sourceHeight;
                    textureImporter.GetSourceTextureWidthAndHeight(out sourceWidth, out sourceHeight);
                    if (sourceWidth > TexturePlatformFormatHelper.ATLAS_MAX_SIZE
                        || sourceHeight > TexturePlatformFormatHelper.ATLAS_MAX_SIZE)
                    {
                        count++;
                        EditorUtility.DisplayProgressBar(StringUtility.Concat("设置图集标签:", setting.folderName), file.Name, count / (float)total);
                        continue;
                    }
                    bool isChange = false;
                    if (textureImporter.textureType != TextureImporterType.Sprite)
                    {
@@ -125,14 +137,14 @@
                    new SpriteSettingConfig.PlatformSetting()
                    {
                        name = "Android",
                        maxTextureSize = 2048,
                        maxTextureSize = TexturePlatformFormatHelper.ATLAS_MAX_SIZE,
                        textureCompression = TextureImporterCompression.Compressed,
                        textureFormat = TextureImporterFormat.ASTC_6x6
                    },
                    new SpriteSettingConfig.PlatformSetting()
                    {
                        name = "iPhone",
                        maxTextureSize = 2048,
                        maxTextureSize = TexturePlatformFormatHelper.ATLAS_MAX_SIZE,
                        textureCompression = TextureImporterCompression.Compressed,
                        textureFormat = TextureImporterFormat.ASTC_6x6
                    }
@@ -256,7 +268,7 @@
        {
            TextureImporterPlatformSettings settingPlatform = atlasImporter.GetPlatformSettings(platformSetting.name);
            settingPlatform.overridden = true;
            settingPlatform.maxTextureSize = platformSetting.maxTextureSize;
            settingPlatform.maxTextureSize = TexturePlatformFormatHelper.ATLAS_MAX_SIZE;
            settingPlatform.textureCompression = platformSetting.textureCompression;
            settingPlatform.format = platformSetting.textureFormat;
            atlasImporter.SetPlatformSettings(settingPlatform);
@@ -303,9 +315,28 @@
        List<UnityEngine.Object> objectsForPack = new List<UnityEngine.Object>();
        foreach (string item in textureFullName)
        {
            // 加载指定目录
            var textureImporter = AssetImporter.GetAtPath(item) as TextureImporter;
            if (textureImporter == null
                || textureImporter.textureType != TextureImporterType.Sprite
                || textureImporter.spriteImportMode == SpriteImportMode.None)
            {
                continue;
            }
            int sourceWidth;
            int sourceHeight;
            textureImporter.GetSourceTextureWidthAndHeight(out sourceWidth, out sourceHeight);
            if (sourceWidth > TexturePlatformFormatHelper.ATLAS_MAX_SIZE
                || sourceHeight > TexturePlatformFormatHelper.ATLAS_MAX_SIZE)
            {
                Debug.LogWarning($"[SpritePack] 跳过超过 {TexturePlatformFormatHelper.ATLAS_MAX_SIZE} 的贴图: {item} ({sourceWidth}x{sourceHeight})");
                continue;
            }
            UnityEngine.Object spriteObj = AssetDatabase.LoadAssetAtPath(item, typeof(UnityEngine.Object));
            //Debug.Log("存png和jpg后缀的图片: " + item + " , " + !packables.Contains(spriteObj));
            if (spriteObj == null)
                continue;
            if (!packables.Contains(spriteObj))
            {
                atlas.Add(new UnityEngine.Object[] { spriteObj });
@@ -324,6 +355,10 @@
        SpriteAtlasAsset.Save(atlas, filePath);
        var atlasImporter = SpriteAtlasImporter.GetAtPath(filePath) as SpriteAtlasImporter;
        if (atlasImporter != null && TexturePlatformFormatHelper.ApplyAtlasMaxTextureSize(atlasImporter))
            atlasImporter.SaveAndReimport();
    }
    /// <summary>