Skip to content

Instantly share code, notes, and snippets.

@JVinceW
Created October 8, 2022 15:01
Show Gist options
  • Save JVinceW/e4ca602df653ca1d63634e47830c151c to your computer and use it in GitHub Desktop.
Save JVinceW/e4ca602df653ca1d63634e47830c151c to your computer and use it in GitHub Desktop.
Simple fast build script for Unity application
using System.Collections;
using System.IO;
using UnityEditor;
using UnityEditor.AddressableAssets;
using UnityEditor.AddressableAssets.Build;
using UnityEditor.AddressableAssets.Settings;
using UnityEngine;
namespace Scripts.AppBuilder
{
public static class AppBuilder
{
private static string s_packageName = "com.default.company";
private static string s_productName = "Product Name";
private static string s_outputPath = "Builds/Standalone64/Game";
private const string COMPANY_NAME = "Amazing Company";
private static string s_version = "1.0.0";
private const string ENTRY_SCENE_NAME = "MainScene";
private const string ASSET_BUNDLE_SCENE_FOLDER = "Assets/Assetbundles/common/scenes";
[MenuItem("Project/Build/WebGL")]
public static void BuildWebGL()
{
EditorUserBuildSettings.SwitchActiveBuildTarget(BuildTargetGroup.WebGL,
BuildTarget.WebGL);
PlayerSettings.SetScriptingBackend(BuildTargetGroup.Standalone, ScriptingImplementation.IL2CPP);
GetArgs();
CommonSetting();
BuildAddressable();
var opt = BuildOptions.None | BuildOptions.DetailedBuildReport;
#if DEVELOPMENT_BUILD
opt |= BuildOptions.AllowDebugging | BuildOptions.ConnectWithProfiler | BuildOptions.Development;
#endif
Debug.Log(
$"Scripting Symbol: {PlayerSettings.GetScriptingDefineSymbolsForGroup(BuildTargetGroup.WebGL)}");
BuildPipeline.BuildPlayer(GetBuildScene(), s_outputPath, BuildTarget.WebGL, opt);
}
private static void BuildAddressable()
{
// Fix scene addressable if mistaken setup on build player setting
Debug.Log("[AppBuild] Reimport scene assetbundles");
AddressableImporter.FolderImporter.ReimportFolders(new[] { ASSET_BUNDLE_SCENE_FOLDER });
AddressableAssetSettings.CleanPlayerContent();
AddressableAssetSettings.BuildPlayerContent();
// var path = ContentUpdateScript.GetContentStateDataPath(false);
// if (!string.IsNullOrEmpty(path))
// {
// Debug.Log($"Has Content update script, we will build update: {path}");
// ContentUpdateScript.BuildContentUpdate(AddressableAssetSettingsDefaultObject.Settings, path);
// } else
// {
// Debug.Log("Dont have content update state file, build new");
// AddressableAssetSettings.CleanPlayerContent();
// AddressableAssetSettings.BuildPlayerContent();
// }
}
private static void GetArgs()
{
// TODO Vince | this is use for CI batch file. Will be setup later
}
private static void CommonSetting() {
PlayerSettings.companyName = COMPANY_NAME;
PlayerSettings.SetApplicationIdentifier(BuildTargetGroup.WebGL, s_packageName);
PlayerSettings.bundleVersion = s_version;
PlayerSettings.productName = s_productName;
PlayerSettings.SplashScreen.showUnityLogo = false;
PlayerSettings.SplashScreen.show = false;
#if DEVELOPMENT_BUILD
PlayerSettings.usePlayerLog = true;
PlayerSettings.SetIl2CppCompilerConfiguration(BuildTargetGroup.WebGL, Il2CppCompilerConfiguration.Debug);
#else
PlayerSettings.usePlayerLog = false;
PlayerSettings.SetIl2CppCompilerConfiguration(BuildTargetGroup.WebGL, Il2CppCompilerConfiguration.Release);
#endif
PlayerSettings.runInBackground = true;
}
private static string[] GetBuildScene() {
var scenes = new ArrayList();
// Other scene will use addressable so we only have to build MainScene
foreach (var scene in EditorBuildSettings.scenes)
{
if (!scene.enabled || scene.path.Length <= 0)
{
continue;
}
if (!scene.path.Contains(ENTRY_SCENE_NAME))
{
continue;
}
scenes.Add(scene.path);
break;
}
Debug.Log($"Build Scene List count: {scenes.Count}");
return (string[]) scenes.ToArray(typeof(string));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment