Skip to content

Instantly share code, notes, and snippets.

@brunomikoski
Last active September 18, 2021 15:12
Show Gist options
  • Save brunomikoski/b95de800b4e0ef7310897bc0fce68f9e to your computer and use it in GitHub Desktop.
Save brunomikoski/b95de800b4e0ef7310897bc0fce68f9e to your computer and use it in GitHub Desktop.
Optimize Android build settings for quick deployment on android
using UnityEditor;
using UnityEditor.Build;
using UnityEditor.Build.Reporting;
namespace BrunoMikoski.Framework.BuildTools
{
public sealed class RidiculousFastBuild : IPostprocessBuildWithReport
{
int IOrderedCallback.callbackOrder => 1000;
private const string RIDICULOUS_FAST_BUILD_PATH_KEY = "RidiculousFastBuildPathKey";
private static ManagedStrippingLevel? previousStripping;
private static ScriptingImplementation? previousBackend;
private static string BuildPath
{
set => EditorPrefs.SetString(RIDICULOUS_FAST_BUILD_PATH_KEY, value);
get => EditorPrefs.GetString(RIDICULOUS_FAST_BUILD_PATH_KEY, string.Empty);
}
[MenuItem("Tools/Ridiculous Fast Build/Fast Build")]
public static void FastBuild()
{
RidiculousFastBuild_Internal();
}
[MenuItem("Tools/Ridiculous Fast Build/Fast Build And Run")]
public static void FastBuildAndRun()
{
RidiculousFastBuild_Internal(BuildOptions.AutoRunPlayer);
}
[MenuItem("Tools/Ridiculous Fast Build/Clear Stored Path", priority = 30)]
public static void ClearPath()
{
BuildPath = string.Empty;
}
private static void RidiculousFastBuild_Internal(BuildOptions? additionalBuildOptions = null)
{
if (string.IsNullOrEmpty(BuildPath))
BuildPath = EditorUtility.SaveFilePanel("Build Location", "", "fast-build.apk", "apk");
if (string.IsNullOrEmpty(BuildPath))
return;
BuildTarget buildTarget = EditorUserBuildSettings.activeBuildTarget;
BuildTargetGroup buildTargetGroup = BuildPipeline.GetBuildTargetGroup(buildTarget);
previousBackend = PlayerSettings.GetScriptingBackend(buildTargetGroup);
previousStripping = PlayerSettings.GetManagedStrippingLevel(buildTargetGroup);
PlayerSettings.SetScriptingBackend(buildTargetGroup, ScriptingImplementation.Mono2x);
PlayerSettings.SetManagedStrippingLevel(buildTargetGroup, ManagedStrippingLevel.Disabled);
BuildPlayerOptions buildPlayerOptions = new BuildPlayerOptions
{
targetGroup = buildTargetGroup,
options = BuildOptions.Development
| BuildOptions.UncompressedAssetBundle
| BuildOptions.CompressWithLz4,
target = buildTarget,
locationPathName = BuildPath,
scenes = GetCurrentScenes()
};
if (additionalBuildOptions.HasValue)
buildPlayerOptions.options |= additionalBuildOptions.Value;
BuildPipeline.BuildPlayer(buildPlayerOptions);
}
private static string[] GetCurrentScenes()
{
string[] scenes = new string[EditorBuildSettings.scenes.Length];
for (int i = 0; i < EditorBuildSettings.scenes.Length; i++)
{
EditorBuildSettingsScene editorBuildSettingsScene = EditorBuildSettings.scenes[i];
scenes[i] = editorBuildSettingsScene.path;
}
return scenes;
}
void IPostprocessBuildWithReport.OnPostprocessBuild(BuildReport report)
{
if (!previousBackend.HasValue)
return;
PlayerSettings.SetScriptingBackend(report.summary.platformGroup, previousBackend.Value);
PlayerSettings.SetManagedStrippingLevel(report.summary.platformGroup, previousStripping.Value);
previousStripping = null;
previousBackend = null;
}
}
}
@dimmduh
Copy link

dimmduh commented Sep 18, 2021

What makes it fast?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment