using System; using System.Collections.Generic; using System.Linq; using UnityEditor.AddressableAssets.Settings; using UnityEditor.SceneManagement; using UnityEngine; namespace UnityEditor.AddressableAssets.Build.AnalyzeRules { class BuildBundleLayout : BundleRuleBase { /// /// Result data for assets included in the bundle layout /// protected internal struct BuildBundleLayoutResultData { public string AssetBundleName; public string AssetPath; public bool Explicit; } /// public override bool CanFix { get { return false; } } /// public override string ruleName { get { return "Bundle Layout Preview"; } } private List m_ResultData = null; /// /// Results of the build Layout. /// protected IEnumerable BuildBundleLayoutResults { get { if (m_ResultData == null) { if (ExtractData == null) { Debug.LogError("RefreshAnalysis needs to be called before getting results"); return new List(0); } m_ResultData = new List(512); foreach (var bundleBuild in AllBundleInputDefs) { foreach (string assetName in bundleBuild.assetNames) { m_ResultData.Add(new BuildBundleLayoutResultData() { AssetBundleName = bundleBuild.assetBundleName, AssetPath = assetName, Explicit = true }); } } List assets = new List(); foreach (KeyValuePair fileToBundle in ExtractData.WriteData.FileToBundle) { assets.Clear(); string assetBundleName = fileToBundle.Value; var implicitGuids = GetImplicitGuidsForBundle(fileToBundle.Key); foreach (GUID guid in implicitGuids) { string assetPath = AssetDatabase.GUIDToAssetPath(guid.ToString()); if (AddressableAssetUtility.IsPathValidForEntry(assetPath)) m_ResultData.Add(new BuildBundleLayoutResultData() { AssetBundleName = assetBundleName, AssetPath = assetPath, Explicit = false }); } } } return m_ResultData; } } /// public override List RefreshAnalysis(AddressableAssetSettings settings) { ClearAnalysis(); if (!BuildUtility.CheckModifiedScenesAndAskToSave()) { Debug.LogError("Cannot run Analyze with unsaved scenes"); m_Results.Add(new AnalyzeResult {resultName = ruleName + "Cannot run Analyze with unsaved scenes"}); return m_Results; } CalculateInputDefinitions(settings); var context = GetBuildContext(settings); RefreshBuild(context); ConvertBundleNamesToGroupNames(context); foreach (BuildBundleLayoutResultData result in BuildBundleLayoutResults) { m_Results.Add(new AnalyzeResult { resultName = result.AssetBundleName + kDelimiter + (result.Explicit ? "Explicit" : "Implicit") + kDelimiter + result.AssetPath }); } if (m_Results.Count == 0) m_Results.Add(noErrors); AddressableAnalytics.ReportUsageEvent(AddressableAnalytics.UsageEventType.RunBundleLayoutPreviewRule); return m_Results; } /// public override void ClearAnalysis() { m_ResultData = null; base.ClearAnalysis(); } [InitializeOnLoad] class RegisterBuildBundleLayout { static RegisterBuildBundleLayout() { AnalyzeSystem.RegisterNewRule(); } } } }