Skip to content

Commit

Permalink
All asset bundles also in base_assets when using Unity 2021 #62
Browse files Browse the repository at this point in the history
  • Loading branch information
jelte.steijaert authored and jelte.steijaert committed Oct 4, 2022
1 parent 8ec2fd1 commit d5d05f4
Showing 1 changed file with 43 additions and 3 deletions.
46 changes: 43 additions & 3 deletions Editor/PlayAssetPackBundlesPreprocessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,53 @@
using System.Linq;
using Khepri.PlayAssetDelivery.Editor;
using UnityEditor;
using UnityEditor.AddressableAssets.Settings;
using UnityEditor.Build;
using UnityEditor.Build.Reporting;
using UnityEngine.AddressableAssets;
using UDebug = UnityEngine.Debug;

#if UNITY_2021_2_OR_NEWER
public class PlayAssetPackBundlesPreprocessor : BuildPlayerProcessor
{
public override int callbackOrder => -1;

public override void PrepareForBuild(BuildPlayerContext buildPlayerContext)
{
AddressablesPlayerBuildProcessor.BuildAddressablesOverride = settings =>
{
AddressableAssetSettings.BuildPlayerContent(out var result);
PostBuild(buildPlayerContext);
return result;
};
}

private void PostBuild(BuildPlayerContext buildPlayerContext)
{
var outputPathExtension = Path.GetExtension(buildPlayerContext.BuildPlayerOptions.locationPathName);
if (buildPlayerContext.BuildPlayerOptions.target != BuildTarget.Android || !outputPathExtension.Equals(".aab"))
{
AssetPackBuilder.ClearConfig();
return;
}
var bundles = AssetPackBuilder.GetBundles(Addressables.BuildPath, SearchOption.AllDirectories);
if (bundles.Length == 0)
{
UDebug.Log($"[{nameof(PlayAssetPackBundlesPreprocessor)}.{nameof(PrepareForBuild)}] No bundles removed.");
return;
}
foreach (var bundle in bundles)
{
bundle.DeleteFile();
}

UDebug.Log($"[{nameof(PlayAssetPackBundlesPreprocessor)}.{nameof(PrepareForBuild)}] Removed: \n -{string.Join("\n -", bundles.Select(bundle => bundle.Bundle))}");
}
}
#else
public class PlayAssetPackBundlesPreprocessor : IPreprocessBuildWithReport
{
public int callbackOrder => 2;
public int callbackOrder => 0;

public void OnPreprocessBuild(BuildReport report)
{
Expand All @@ -19,7 +58,7 @@ public void OnPreprocessBuild(BuildReport report)
AssetPackBuilder.ClearConfig();
return;
}
var bundles = AssetPackBuilder.GetBundles(Addressables.PlayerBuildDataPath, SearchOption.AllDirectories);
var bundles = AssetPackBuilder.GetBundles(Addressables.BuildPath, SearchOption.AllDirectories);
if (bundles.Length == 0)
{
UDebug.Log($"[{nameof(PlayAssetPackBundlesPreprocessor)}.{nameof(OnPreprocessBuild)}] No bundles removed.");
Expand All @@ -31,4 +70,5 @@ public void OnPreprocessBuild(BuildReport report)
}
UDebug.Log($"[{nameof(PlayAssetPackBundlesPreprocessor)}.{nameof(OnPreprocessBuild)}] Removed: \n -{string.Join("\n -", bundles.Select(bundle => bundle.Bundle))}");
}
}
}
#endif

0 comments on commit d5d05f4

Please sign in to comment.