Hey all, I was wondering is there a way to use Addressables analyze tools specially “Check Duplicate Bundle Dependencies” via code?
I’m currently working on a tool that create and builds bundles automatically and I need to fix duplicate dependencies in bundles.
I’m using unity 2019.4.0f1 and addressables package version 1.20.0
You can take a look at CheckBundleDupeDependencyTests.cs, for example:
[Test]
public void CheckDupeNoIssuesIfValid()
{
var group1 = Settings.CreateGroup("CheckDupeDepencency1", false, false, false, null, typeof(BundledAssetGroupSchema));
var group2 = Settings.CreateGroup("CheckDupeDepencency2", false, false, false, null, typeof(BundledAssetGroupSchema));
Settings.CreateOrMoveEntry(AssetDatabase.AssetPathToGUID(k_CheckDupePrefabA), group1, false, false);
Settings.CreateOrMoveEntry(AssetDatabase.AssetPathToGUID(k_CheckDupePrefabB), group2, false, false);
Settings.CreateOrMoveEntry(AssetDatabase.AssetPathToGUID(k_CheckDupeMyMaterial), group2, false, false);
var groupCount = Settings.groups.Count;
var rule = new CheckBundleDupeDependencies(); // <------
rule.FixIssues(Settings); // <------
Assert.AreEqual(groupCount, Settings.groups.Count);
//Cleanup
Settings.RemoveGroup(group1);
Settings.RemoveGroup(group2);
}
Thank you for your reply.
I have an issue with this code, I’m just using Line 13,14 in my code and I get exceptions, but when I create a new group and use these lines I don’t have a problem.
Am I doing something wrong or is this a bug?
code with error:
public void FixDuplicate()
{
var assetSettings = AddressableAssetSettingsDefaultObject.Settings;
var rule = new CheckBundleDupeDependencies();
rule.FixIssues(assetSettings);
}
code without error:
public void FixDuplicate()
{
var assetSettings = AddressableAssetSettingsDefaultObject.Settings;
var group = assetSettings.CreateGroup("EmptyGroup", false, false, false, null, typeof(BundledAssetGroupSchema));
var rule = new CheckBundleDupeDependencies();
rule.FixIssues(assetSettings);
assetSettings.RemoveGroup(group);
}
exceptions:
Build Task CalculateAssetDependencyData failed with exception:
Object reference not set to an instance of an object
at BuildCacheUtility.GetCacheEntry (System.Type type, System.Int32 version) [0x00022] in D:\Project\Library\PackageCache\com.unity.scriptablebuildpipeline@1.19.6\Editor\Utilities\BuildCacheUtility.cs:94
at UnityEditor.Build.Pipeline.Utilities.BuildCache.GetCacheEntry (System.Type type, System.Int32 version) [0x00001] in D:\Project\Library\PackageCache\com.unity.scriptablebuildpipeline@1.19.6\Editor\Utilities\BuildCache.cs:203
at (wrapper delegate-invoke) System.Func`3[System.Type,System.Int32,UnityEditor.Build.Pipeline.Utilities.CacheEntry].invoke_TResult_T1_T2(System.Type,int)
at System.Linq.Enumerable+<SelectIterator>d__154`2[TSource,TResult].MoveNext () [0x0005a] in <351e49e2a5bf4fd6beabb458ce2255f3>:0
at System.Collections.Generic.HashSet`1[T].UnionWith (System.Collections.Generic.IEnumerable`1[T] other) [0x00026] in <351e49e2a5bf4fd6beabb458ce2255f3>:0
at UnityEditor.Build.Pipeline.Utilities.ExtensionMethods.ExtractCommonCacheData (UnityEditor.Build.Pipeline.Interfaces.IBuildCache cache, System.Collections.Generic.IEnumerable`1[T] includedObjects, System.Collections.Generic.IEnumerable`1[T] referencedObjects, System.Collections.Generic.HashSet`1[T] uniqueTypes, System.Collections.Generic.List`1[T] objectTypes, System.Collections.Generic.HashSet`1[T] dependencies) [0x000bf] in D:\Project\Library\PackageCache\com.unity.scriptablebuildpipeline@1.19.6\Editor\Utilities\ExtensionMethods.cs:78
at UnityEditor.Build.Pipeline.Tasks.CalculateAssetDependencyData.GetCachedInfo (UnityEditor.Build.Pipeline.Interfaces.IBuildCache cache, UnityEditor.GUID asset, UnityEditor.Build.Content.AssetLoadInfo assetInfo, UnityEditor.Build.Content.BuildUsageTagSet usageTags, UnityEditor.Build.Pipeline.Interfaces.SpriteImporterData importerData, UnityEditor.Build.Pipeline.Interfaces.ExtendedAssetData assetData, System.Boolean NonRecursiveDependencies) [0x00029] in D:\Project\Library\PackageCache\com.unity.scriptablebuildpipeline@1.19.6\Editor\Tasks\CalculateAssetDependencyData.cs:103
at UnityEditor.Build.Pipeline.Tasks.CalculateAssetDependencyData.RunInternal (UnityEditor.Build.Pipeline.Tasks.CalculateAssetDependencyData+TaskInput input, UnityEditor.Build.Pipeline.Tasks.CalculateAssetDependencyData+TaskOutput& output) [0x004b6] in D:\Projects\TerraLand_Streaming\Library\PackageCache\com.unity.scriptablebuildpipeline@1.19.6\Editor\Tasks\CalculateAssetDependencyData.cs:316
at UnityEditor.Build.Pipeline.Tasks.CalculateAssetDependencyData.Run () [0x000fc] in D:\Project\Library\PackageCache\com.unity.scriptablebuildpipeline@1.19.6\Editor\Tasks\CalculateAssetDependencyData.cs:130
at UnityEditor.Build.Pipeline.BuildTasksRunner.Run (System.Collections.Generic.IList`1[T] pipeline, UnityEditor.Build.Pipeline.Interfaces.IBuildContext context) [0x000cd] in D:\Project\Library\PackageCache\com.unity.scriptablebuildpipeline@1.19.6\Editor\Shared\BuildTasksRunner.cs:56
UnityEditor.AddressableAssets.Build.AnalyzeRules.CheckBundleDupeDependencies:FixIssues(AddressableAssetSettings)
Analyze build failed. Exception
UnityEditor.AddressableAssets.Build.AnalyzeRules.CheckBundleDupeDependencies:FixIssues(AddressableAssetSettings)
NullReferenceException: Object reference not set to an instance of an object
UnityEditor.AddressableAssets.Build.AnalyzeRules.CheckBundleDupeDependencies.FixIssues (UnityEditor.AddressableAssets.Settings.AddressableAssetSettings settings) (at Library/PackageCache/com.unity.addressables@1.20.0/Editor/Build/AnalyzeRules/CheckBundleDupeDependencies.cs:274)
Sorry for big chunks of errors.
Hi @MGhasemi ! Hmm looks like the Build Task exception is happening when accessing the Scriptable Build Pipeline cache (Library/BuildCache), does clearing the cache help? You can do this through in the Groups window toolbar: Build > Clear Build Cache > Build Pipeline Cache
If that doesn’t work, it’s kind of hard to tell what the issue is without the full project. Is it possible to submit the project in a bug report?
The NullReferenceException happening in CheckBundleDupeDependencies.cs:274 does appear to be a bug. It tries to access the size of m_ImplicitAssets, but it’s possible for m_ImplicitAssets to be null at this point. I’ll create a bug ticket for that exception.
Thank you, Clearing Scriptable Build Pipeline cache resolved the issues, Is there a way to clean cache via code? something like:
AddressableAssetSettings.CleanPlayerContent();
P.S: I can’t upload full project but I’ll try to replicate the issue in new project and submit it.
Yes you can do BuildCache.PurgeCache(false); to purge the build pipeline cache.
https://docs.unity3d.com/Packages/com.unity.scriptablebuildpipeline@1.20/api/UnityEditor.Build.Pipeline.Utilities.BuildCache.html#UnityEditor_Build_Pipeline_Utilities_BuildCache_PurgeCache_System_Boolean_
AddressableAssetSettings.CleanPlayerContent(); will clear the cache for Addressables build scripts (i.e. BuildScriptPackedMode).
Thank you @pillakirsten , this solution works kinda fine.
But still there is a issue:
My workflow has two scenes, first scene to create prefabs and add each one to separate addressable groups and second scene to fix duplicate dependencies and build asset bundles with default build script.
If I combine functionality of both scenes and purge build pipeline and fix rule in the same scene that creates prefabs it works with following steps:
1.first generate prefabs and groups with script (no use of unity editor)
2.open “Addressables Group Window” in editor (or keep it open during prefab generation process)
3.use a script to purge build pipeline cache and fix duplicate dependencies rule (no use of unity editor)
With these three steps I can fix duplicate issue and build without exceptions, But if I skip step no.2 sometimes I get exceptions no matter I purge cache or not (via code).
Using 2 scene always results in exceptions.