Created
June 24, 2021 21:59
-
-
Save mminer/9c8ea8b01a4368475b56384bed1b9d35 to your computer and use it in GitHub Desktop.
Unity Addressables Analyze rule that reports the size of bundles after building them.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Text.RegularExpressions; | |
using UnityEditor; | |
using UnityEditor.AddressableAssets.Build; | |
using UnityEditor.AddressableAssets.Build.AnalyzeRules; | |
using UnityEditor.AddressableAssets.Settings; | |
using UnityEngine; | |
/// <summary> | |
/// Addressables Analyze rule that reports the size of bundles after building them. | |
/// </summary> | |
class AddressablesSizeAnalyzeRule : AnalyzeRule | |
{ | |
public override bool CanFix => false; | |
public override string ruleName => "Built Bundle Sizes"; | |
public override List<AnalyzeResult> RefreshAnalysis(AddressableAssetSettings settings) | |
{ | |
ClearAnalysis(); | |
var projectPath = Path.GetDirectoryName(Application.dataPath); | |
var buildLayoutPath = Path.Combine(projectPath, "Library", "com.unity.addressables", "buildlayout.txt"); | |
if (!File.Exists(buildLayoutPath)) | |
{ | |
GenerateBuildLayout(); | |
} | |
var buildLayoutText = File.ReadAllText(buildLayoutPath); | |
return ParseBundleSizes(buildLayoutText) | |
.Select(bundle => new AnalyzeResult | |
{ | |
resultName = $"{bundle.bundleName} {bundle.size}", | |
}) | |
.ToList(); | |
} | |
static void GenerateBuildLayout() | |
{ | |
// Ideally we'd use BuildLayoutGenerationTask, but it doesn't appear to be designed to be called directly. | |
var previousGenerateBuildLayoutSetting = ProjectConfigData.GenerateBuildLayout; | |
ProjectConfigData.GenerateBuildLayout = true; | |
AddressableAssetSettings.BuildPlayerContent(); | |
ProjectConfigData.GenerateBuildLayout = previousGenerateBuildLayoutSetting; | |
} | |
static IEnumerable<(string bundleName, string size)> ParseBundleSizes(string buildLayoutText) | |
{ | |
// Example line: | |
// Archive defaultlocalgroup.bundle (Size: 3KB, Compression: Lz4HC, Asset Bundle Object Size: 2KB) | |
// As buildlayout.txt warns, this format is subject to change. | |
const string regexPattern = @"^\tArchive (?<bundleName>.+) \(Size: (?<size>.+?),"; | |
return Regex | |
.Matches(buildLayoutText, regexPattern, RegexOptions.Multiline) | |
.Cast<Match>() | |
.Select(match => ( | |
bundleName: match.Groups["bundleName"].Captures[0].Value, | |
size: match.Groups["size"].Captures[0].Value | |
)) | |
.OrderBy(bundle => bundle.bundleName); | |
} | |
} | |
[InitializeOnLoad] | |
class RegisterAddressablesSizeAnalyzeRule | |
{ | |
static RegisterAddressablesSizeAnalyzeRule() | |
{ | |
AnalyzeSystem.RegisterNewRule<AddressablesSizeAnalyzeRule>(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment