Skip to content

Instantly share code, notes, and snippets.

@Long18
Created January 23, 2024 04:13
Show Gist options
  • Save Long18/351c5b7e0ab880fa9596515007cc4966 to your computer and use it in GitHub Desktop.
Save Long18/351c5b7e0ab880fa9596515007cc4966 to your computer and use it in GitHub Desktop.
The BuildAddressablesEditorWindow class streamlines Unity Addressable Assets building in the Editor, providing a shortcut (Alt+ B) under "Tools." It automates asset cleanup, initiates the build, and logs success/errors, enhancing the efficiency of the Addressable Assets build process.
public class BuildAddressablesEditorWindow: EditorWindow
{
[MenuItem("Tools/BuildAddressable &b")]
public static void BuildAndRun()
{
var window = GetWindow<BuildAddressablesEditorWindow>();
window.Close();
}
private void OnEnable()
{
Clean();
if(!Build()) return;
Debug.Log($"<color=green>[BuildAddressable]</color> Build Completed");
}
private void Clean()
{
// Clean up player content using the active player data builder.
// This ensures that only necessary assets are included in the built player.
// AddressableAssetSettings.CleanPlayerContent(AddressableAssetSettingsDefaultObject.Settings.ActivePlayerDataBuilder);
// Clean up player content using default settings.
// This removes any unnecessary data or assets from the built player.
AddressableAssetSettings.CleanPlayerContent();
}
private bool Build()
{
AddressableAssetSettings.BuildPlayerContent(out AddressablesPlayerBuildResult result);
bool success = string.IsNullOrEmpty(result.Error);
if (success) return true;
Debug.Log($"<color=red>[BuildAddressable]</color> Build error: {result.Error}");
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment