Skip to content

Instantly share code, notes, and snippets.

@JBurkeKF
Last active March 15, 2024 06:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JBurkeKF/99fc5427cb90a693af46dcbc0900c5d5 to your computer and use it in GitHub Desktop.
Save JBurkeKF/99fc5427cb90a693af46dcbc0900c5d5 to your computer and use it in GitHub Desktop.
Custom schema and build script for Unity Addressable Groups to override packing settings per platform. (Written against Unity Addressables 1.19.19)
using UnityEngine;
using UnityEditor.AddressableAssets.Build;
using UnityEditor.AddressableAssets.Build.DataBuilders;
using UnityEditor.AddressableAssets.Settings;
using UnityEditor.AddressableAssets.Settings.GroupSchemas;
// Written against Unity Addressables 1.19.19
[CreateAssetMenu( fileName = "PlatformBuildScriptPacked.asset", menuName = "Addressables/Content Builders/Platform Build Script Packed" )]
public class PlatformBuildScriptPackedMode : BuildScriptPackedMode
{
public override string Name
{
get
{
return "Platform Build Script Packed Mode";
}
}
protected override TResult BuildDataImplementation<TResult>( AddressablesDataBuilderInput builderInput )
{
if ( builderInput.AddressableSettings != null && builderInput.AddressableSettings.groups != null )
{
for ( int i = 0; i < builderInput.AddressableSettings.groups.Count; i++ )
{
AddressableAssetGroup assetGroup = builderInput.AddressableSettings.groups[i];
if ( assetGroup != null )
{
if ( assetGroup.HasSchema<BundledAssetGroupSchema>() )
{
BundledAssetGroupSchema bundledAssetGroupSchema = assetGroup.GetSchema<BundledAssetGroupSchema>();
if ( bundledAssetGroupSchema.IncludeInBuild && assetGroup.HasSchema<PlatformBundledAssetGroupSchema>() )
{
assetGroup.GetSchema<PlatformBundledAssetGroupSchema>().OverrideBundledAssetGroupSchema( bundledAssetGroupSchema, builderInput.Target );
}
}
}
}
}
TResult result;
try
{
result = base.BuildDataImplementation<TResult>( builderInput );
}
catch ( System.Exception e )
{
if ( builderInput.AddressableSettings != null && builderInput.AddressableSettings.groups != null )
{
for ( int i = 0; i < builderInput.AddressableSettings.groups.Count; i++ )
{
AddressableAssetGroup assetGroup = builderInput.AddressableSettings.groups[i];
if ( assetGroup != null )
{
if ( assetGroup.HasSchema<BundledAssetGroupSchema>() )
{
BundledAssetGroupSchema bundledAssetGroupSchema = assetGroup.GetSchema<BundledAssetGroupSchema>();
if ( bundledAssetGroupSchema.IncludeInBuild && assetGroup.HasSchema<PlatformBundledAssetGroupSchema>() )
{
assetGroup.GetSchema<PlatformBundledAssetGroupSchema>().RestoreBundledAssetGroupSchema( bundledAssetGroupSchema );
}
}
}
}
}
throw e;
}
if ( builderInput.AddressableSettings != null && builderInput.AddressableSettings.groups != null )
{
for ( int i = 0; i < builderInput.AddressableSettings.groups.Count; i++ )
{
AddressableAssetGroup assetGroup = builderInput.AddressableSettings.groups[i];
if ( assetGroup != null )
{
if ( assetGroup.HasSchema<BundledAssetGroupSchema>() )
{
BundledAssetGroupSchema bundledAssetGroupSchema = assetGroup.GetSchema<BundledAssetGroupSchema>();
if ( bundledAssetGroupSchema.IncludeInBuild && assetGroup.HasSchema<PlatformBundledAssetGroupSchema>() )
{
assetGroup.GetSchema<PlatformBundledAssetGroupSchema>().RestoreBundledAssetGroupSchema( bundledAssetGroupSchema );
}
}
}
}
}
return result;
}
}
using UnityEngine;
using UnityEditor;
using UnityEditor.AddressableAssets.Settings;
using UnityEditor.AddressableAssets.Settings.GroupSchemas;
// Written against Unity Addressables 1.19.19
public class PlatformBundledAssetGroupSchema : AddressableAssetGroupSchema
{
[System.Serializable]
public class PlatformData
{
public bool shouldOverride = false;
public BundledAssetGroupSchema.BundleCompressionMode compression = BundledAssetGroupSchema.BundleCompressionMode.LZ4;
public bool useAssetBundleCRC = true;
public bool useAssetBundleCRCForCachedBundles = true;
public BundledAssetGroupSchema.BundleNamingStyle bundleNaming = BundledAssetGroupSchema.BundleNamingStyle.AppendHash;
}
[System.Serializable]
public class Platforms
{
public PlatformData Android = null;
public PlatformData iOS = null;
public PlatformData Standalone = null;
public PlatformData tvOS = null;
public PlatformData WebGL = null;
public PlatformData Get( BuildTarget buildTarget )
{
switch ( buildTarget )
{
case BuildTarget.Android:
return Android;
case BuildTarget.iOS:
return iOS;
case BuildTarget.tvOS:
return tvOS;
case BuildTarget.WebGL:
return WebGL;
default:
return Standalone;
}
}
}
[SerializeField]
private Platforms platforms = null;
private bool hasBackup = false;
private BundledAssetGroupSchema.BundleCompressionMode backupCompression = BundledAssetGroupSchema.BundleCompressionMode.LZ4;
private bool backupUseAssetBundleCRC = true;
private bool backupUseAssetBundleCRCForCachedBundles = true;
private BundledAssetGroupSchema.BundleNamingStyle backupBundleNaming = BundledAssetGroupSchema.BundleNamingStyle.AppendHash;
public void OverrideBundledAssetGroupSchema( BundledAssetGroupSchema bundledAssetGroupSchema, BuildTarget buildTarget )
{
if ( bundledAssetGroupSchema != null )
{
hasBackup = true;
backupCompression = bundledAssetGroupSchema.Compression;
backupUseAssetBundleCRC = bundledAssetGroupSchema.UseAssetBundleCrc;
backupUseAssetBundleCRCForCachedBundles = bundledAssetGroupSchema.UseAssetBundleCrcForCachedBundles;
backupBundleNaming = bundledAssetGroupSchema.BundleNaming;
if ( platforms != null )
{
PlatformData data = platforms.Get( buildTarget );
if ( data != null && data.shouldOverride )
{
bundledAssetGroupSchema.Compression = data.compression;
bundledAssetGroupSchema.UseAssetBundleCrc = data.useAssetBundleCRC;
bundledAssetGroupSchema.UseAssetBundleCrcForCachedBundles = data.useAssetBundleCRCForCachedBundles;
bundledAssetGroupSchema.BundleNaming = data.bundleNaming;
}
}
}
}
public void RestoreBundledAssetGroupSchema( BundledAssetGroupSchema bundledAssetGroupSchema )
{
if ( hasBackup && bundledAssetGroupSchema != null )
{
hasBackup = false;
bundledAssetGroupSchema.Compression = backupCompression;
bundledAssetGroupSchema.UseAssetBundleCrc = backupUseAssetBundleCRC;
bundledAssetGroupSchema.UseAssetBundleCrcForCachedBundles = backupUseAssetBundleCRCForCachedBundles;
bundledAssetGroupSchema.BundleNaming = backupBundleNaming;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment