← Files UnityARCHIVED FILE

skills/manage-sprite-atlas/resources/variantatlas.cs

1.28 KB · Oct 5, 2026 · 18:34 UTC

↓ Download file

void GenerateVariantAtlas(string masterPath, string variantPath, float scale)
{
    // Load master atlas (must exist and be imported first)
    SpriteAtlas masterRuntime = AssetDatabase.LoadAssetAtPath<SpriteAtlas>(masterPath);
    if (masterRuntime == null)
    {
        Debug.LogError($"Master atlas not found: {masterPath}");
        return;
    }

    // Get original packables from master
    Object[] originalPackables = masterRuntime.GetPackables();

    // Create variant
    SpriteAtlasAsset variantAsset = new SpriteAtlasAsset();
    variantAsset.SetIsVariant(true);
    variantAsset.Add(originalPackables);
    variantAsset.SetMasterAtlas(masterRuntime);

    SpriteAtlasAsset.Save(variantAsset, variantPath);
    AssetDatabase.ImportAsset(variantPath);

    // Configure variant scale
    SpriteAtlasImporter importer = AssetImporter.GetAtPath(variantPath) as SpriteAtlasImporter;
    if (importer != null)
    {
        importer.variantScale = scale;
        importer.includeInBuild = true;
        importer.SaveAndReimport();
    }
}

// Usage in OnPreprocessBuild:
GenerateAtlasByFolder("Assets/Art/UI", "Assets/Atlases/UI_Master.spriteatlasv2");
GenerateVariantAtlas(
    "Assets/Atlases/UI_Master.spriteatlasv2",
    "Assets/Atlases/UI_HD.spriteatlasv2",
    0.5f  // HD variant at 50% scale
);

SHA-256: 2c0cac488c6ee4c805d9f88884f5aa476104c787e6c6e7853d1514cfda57cc86