-
-
Save favoyang/cd2cf2ed9df7e2538f3630610c604c51 to your computer and use it in GitHub Desktop.
/// <summary> | |
/// The script gives you choice to whether to build addressable bundles when clicking the build button. | |
/// For custom build script, call PreExport method yourself. | |
/// For cloud build, put BuildAddressablesProcessor.PreExport as PreExport command. | |
/// Discussion: https://forum.unity.com/threads/how-to-trigger-build-player-content-when-build-unity-project.689602/ | |
/// | |
/// License: The MIT License https://opensource.org/licenses/MIT | |
/// </summary> | |
using UnityEditor; | |
using UnityEditor.AddressableAssets; | |
using UnityEditor.AddressableAssets.Settings; | |
using UnityEngine; | |
using System.Collections; | |
class BuildAddressablesProcessor | |
{ | |
/// <summary> | |
/// Run a clean build before export. | |
/// </summary> | |
static public void PreExport() | |
{ | |
Debug.Log("BuildAddressablesProcessor.PreExport start"); | |
AddressableAssetSettings.CleanPlayerContent( | |
AddressableAssetSettingsDefaultObject.Settings.ActivePlayerDataBuilder); | |
AddressableAssetSettings.BuildPlayerContent(); | |
Debug.Log("BuildAddressablesProcessor.PreExport done"); | |
} | |
[InitializeOnLoadMethod] | |
private static void Initialize() | |
{ | |
BuildPlayerWindow.RegisterBuildPlayerHandler(BuildPlayerHandler); | |
} | |
private static void BuildPlayerHandler(BuildPlayerOptions options) | |
{ | |
if (EditorUtility.DisplayDialog("Build with Addressables", | |
"Do you want to build a clean addressables before export?", | |
"Build with Addressables", "Skip")) | |
{ | |
PreExport(); | |
} | |
BuildPlayerWindow.DefaultBuildMethods.BuildPlayer(options); | |
} | |
} |
Just a note to anyone who might be trying to use this script: It has to be inside a folder called Editor
, something that was not immediately obvious to me.
Just a note to anyone who might be trying to use this script: It has to be inside a folder called
Editor
, something that was not immediately obvious to me.
Thank you very much!
Thanks your code :)
Can I use your code in my commercial app?
Please let me know how I can use the code for commercial distribution.
Can I use your code in my commercial app?
My pleasure. Just added a note for the MIT license.
@favoyang when calling PreExport function it will build both local and remote groups, right?
@favoyang when calling PreExport function it will build both local and remote groups, right?
@xuyennguyen it builds your currently active profile.
https://docs.unity3d.com/Packages/[email protected]/manual/BuildPlayerContent.html
Unity officially supports build addressables in cloud build except for WebGL target. However the script can be still useful to trigger build addressables locally with app build (GUI only).