#if UNITY_EDITOR using System.Collections.Generic; using UnityEditor; using UnityEditor.AddressableAssets.Build; using UnityEditor.AddressableAssets.Build.AnalyzeRules; using UnityEditor.AddressableAssets.Settings; using UnityEditor.AddressableAssets.Settings.GroupSchemas; public class CheckBundleDupeDependenciesMultiIsolatedGroups : CheckBundleDupeDependencies { protected internal struct GroupComparator : IEqualityComparer> { public bool Equals(List x, List y) { foreach (AddressableAssetGroup group in x) { if (y.Find(i => i.Guid == group.Guid) == null) return false; } return true; } public int GetHashCode(List obj) { int hashCode = obj.Count > 0 ? 17 : 0; foreach (AddressableAssetGroup group in obj) hashCode = hashCode * 31 + group.Guid.GetHashCode(); return hashCode; } } /// public override string ruleName { get { return "Check Duplicate Bundle Dependencies Multi-Isolated Groups"; } } /// /// Fix duplicates by moving them to new groups. /// /// The current Addressables settings object /// Duplicates referenced by the same groups will be moved to the same new group. public override void FixIssues(AddressableAssetSettings settings) { if (CheckDupeResults == null) CheckForDuplicateDependencies(settings); Dictionary> implicitAssetsToGroup = GetImplicitAssetsToGroup(CheckDupeResults); var groupsToAssets = new Dictionary, List>(new GroupComparator()); foreach (KeyValuePair> pair in implicitAssetsToGroup) { if (!groupsToAssets.TryGetValue(pair.Value, out List assets)) { assets = new List(); groupsToAssets.Add(pair.Value, assets); } groupsToAssets[pair.Value].Add(pair.Key); } foreach (KeyValuePair, List> pair in groupsToAssets) { var group = settings.CreateGroup("Duplicate Asset Isolation", false, false, false, null, typeof(BundledAssetGroupSchema), typeof(ContentUpdateGroupSchema)); group.GetSchema().StaticContent = true; foreach (GUID asset in pair.Value) settings.CreateOrMoveEntry(asset.ToString(), group, false, false); } settings.SetDirty(AddressableAssetSettings.ModificationEvent.BatchModification, null, true, true); } protected Dictionary> GetImplicitAssetsToGroup(IEnumerable checkDupeResults) { var implicitAssetsToGroup = new Dictionary>(); foreach (var checkDupeResult in checkDupeResults) { GUID assetGuid = checkDupeResult.DuplicatedGroupGuid; if (!implicitAssetsToGroup.TryGetValue(assetGuid, out List groups)) { groups = new List(); implicitAssetsToGroup.Add(assetGuid, groups); } implicitAssetsToGroup[assetGuid].Add(checkDupeResult.Group); } return implicitAssetsToGroup; } } [InitializeOnLoad] class RegisterCheckBundleDupeDependenciesMultiIsolatedGroups { static RegisterCheckBundleDupeDependenciesMultiIsolatedGroups() { AnalyzeSystem.RegisterNewRule(); } } #endif