Unity自动化打图集

2023/07 19 17:07
// 创建图集对象
public static SpriteAtlas NewEmptySpriteAtlas(int maxTexSize = 2048)
{
    SpriteAtlas spriteAtlas = new SpriteAtlas();
    SpriteAtlasPackingSettings packingSettings = new SpriteAtlasPackingSettings()
    {
        blockOffset = 1,
        padding = 2,
        enableRotation = false,
        enableTightPacking = false,
        enableAlphaDilation = true,
    };

    spriteAtlas.SetPackingSettings(packingSettings);

    SpriteAtlasTextureSettings textureSettings = new SpriteAtlasTextureSettings()
    {
        readable = false,
        generateMipMaps = false,
        sRGB = true,
        filterMode = FilterMode.Bilinear,
    };
    spriteAtlas.SetTextureSettings(textureSettings);

    TextureImporterPlatformSettings defaultSetting = new TextureImporterPlatformSettings();
    defaultSetting.maxTextureSize = maxTexSize;
    defaultSetting.crunchedCompression = true;
    defaultSetting.textureCompression = TextureImporterCompression.Compressed;
    defaultSetting.format = TextureImporterFormat.Automatic;
    spriteAtlas.SetPlatformSettings(defaultSetting);

    TextureImporterPlatformSettings androidSetting = new TextureImporterPlatformSettings();
    androidSetting.name = BuildPipeline.GetBuildTargetName(BuildTarget.Android);
    androidSetting.overridden = true;
    androidSetting.maxTextureSize = maxTexSize;
    androidSetting.textureCompression = TextureImporterCompression.Compressed;
    androidSetting.format = TextureImporterFormat.ASTC_6x6;
    spriteAtlas.SetPlatformSettings(androidSetting);

    TextureImporterPlatformSettings iosSetting = new TextureImporterPlatformSettings();
    iosSetting.name = BuildPipeline.GetBuildTargetName(BuildTarget.iOS);
    iosSetting.overridden = true;
    iosSetting.maxTextureSize = maxTexSize;
    iosSetting.textureCompression = TextureImporterCompression.Compressed;
    iosSetting.format = TextureImporterFormat.ASTC_6x6;
    spriteAtlas.SetPlatformSettings(iosSetting);

    return spriteAtlas;
}

// 清空图集缓存,打图集前必须清一下缓存,否则各种异常
private static void ClearAtlasCache()
{
    const string atlasCacheDir = "Library/AtlasCache/";
    var fullPath = System.IO.Path.GetFullPath(atlasCacheDir);
    if (!System.IO.Directory.Exists(fullPath))
    {
        return;
    }
    var files = System.IO.Directory.GetFiles(fullPath, "*.*", System.IO.SearchOption.AllDirectories);
    foreach(var filename in files)
    {
        System.IO.File.Delete(filename);
    }
}

// 执行打图集操作
// inputSpriteFiles 输入的Sprite文件列表
// outputAtlasFilename 输出的图集文件名
private static void DoPackSpriteAtlas(List<string> inputSpriteFiles, string outputAtlasFilename)
{
    ClearAtlasCache();

    UnityEngine.Object[] LoadAssets()
    {
        List<UnityEngine.Object> objs = new List<UnityEngine.Object>();
        foreach(var asset in inputSpriteFiles)
        {
            UnityEngine.Object obj = AssetDatabase.LoadAssetAtPath<UnityEngine.Object>(asset);
            objs.Add(obj);
        }
        return objs.ToArray();
    }

    List<SpriteAtlas> atlasList = new List<SpriteAtlas>();
    SpriteAtlas spobj = UISpriteAtlasCfgGroup.NewEmptySpriteAtlas();
    spobj.Add( LoadAssets() );
    atlasList.Add(spobj);
    AssetDatabase.CreateAsset(spobj, outputAtlasFilename);
    SpriteAtlasUtility.PackAtlases( atlasList.ToArray(), BuildTarget.StandaloneWindows64 );
}