Skip to content

Instantly share code, notes, and snippets.

@lunarcloud
Last active February 25, 2022 00:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lunarcloud/0b399421bd7a8f17470503f754969b24 to your computer and use it in GitHub Desktop.
Save lunarcloud/0b399421bd7a8f17470503f754969b24 to your computer and use it in GitHub Desktop.
Adds Build Menu Options in Unity (No Need to Swap Platform to build) - ensure inside an "Editor" folder.
using UnityEngine;
using UnityEditor;
using UnityEditor.Build.Reporting;
public class Builder
{
static readonly string appName = "ExampleApp";
static readonly string[] appLevels = new[] {
"Assets/Scene1.unity",
"Assets/SceneB.unity"
};
[MenuItem("Build/Android/Build")]
public static void BuildMainAndroid() => BuildAndroid(appName, appLevels);
[MenuItem("Build/Android/Delpoy Prebuilt")]
public static void DelpoyMainAndroid() => DelpoyAndroid(appName, appLevels);
[MenuItem("Build/Android/Build And Run")]
public static void BuildMainAndroidAndRun() => BuildAndroidAndRun(appName, appLevels);
[MenuItem("Build/Build Windows")]
public static void BuildMainWindows() => BuildWin64(appName, appLevels);
[MenuItem("Build/Build MacOS")]
public static void BuildMainMac() => BuildMacOS(appName, appLevels);
[MenuItem("Build/Build Linux")]
public static void BuildMainLinux() => BuildLin64(appName, appLevels);
/* Generic Builder Funcitons */
private static void BuildWin64(string name, string[] levels, BuildOptions options = BuildOptions.ShowBuiltPlayer )
{
var report = BuildPipeline.BuildPlayer(levels, $"Build/Win/{name}.exe", BuildTarget.StandaloneWindows64, options);
PrintReport(report);
}
private static void BuildLin64(string name, string[] levels, BuildOptions options = BuildOptions.ShowBuiltPlayer)
{
var report = BuildPipeline.BuildPlayer(levels, $"Build/Linux/{name}.x86_64", BuildTarget.StandaloneLinux64, options);
PrintReport(report);
}
private static void BuildMacOS(string name, string[] levels, BuildOptions options = BuildOptions.ShowBuiltPlayer)
{
var report = BuildPipeline.BuildPlayer(levels, $"Build/MacOS/{name}.app", BuildTarget.StandaloneOSX, options);
PrintReport(report);
}
private static void BuildAndroid(string name, string[] levels, BuildOptions options = BuildOptions.ShowBuiltPlayer)
{
var report = BuildPipeline.BuildPlayer(levels, $"Build/{name}.apk", BuildTarget.Android, options);
PrintReport(report);
}
private static void DelpoyAndroid(string name, string[] levels)
{
var apkPath = System.IO.Path.GetFullPath( $"{Application.dataPath}/../Build/{name}.apk" );
if (!System.IO.File.Exists(apkPath)) {
Debug.LogError($"'Build/{name}.apk' has not been built");
return;
}
var adbPath = System.IO.Path.GetFullPath( EditorPrefs.GetString("AndroidSdkRoot") + "/platform-tools/adb" );
#if UNITY_EDITOR_WIN
adbPath += ".exe";
#endif
if (!System.IO.File.Exists(adbPath))
{
Debug.LogError($"'{adbPath}' doesn't exist (try toggling the adb option under preferences > external tools).");
return;
}
Debug.Log($"Found adb at path {adbPath}");
var process = new System.Diagnostics.Process();
process.EnableRaisingEvents = false;
process.StartInfo.FileName = adbPath;
process.StartInfo.Arguments = $"install \"{apkPath}\"";
process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Maximized;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.UseShellExecute = false;
Debug.Log($"Delpoying 'Build/{name}.apk' to device...");
process.Start();
string log = process.StandardOutput.ReadToEnd();
string errorLog = process.StandardError.ReadToEnd();
process.WaitForExit();
if (!string.IsNullOrEmpty(log)) Debug.Log(log);
if (process.ExitCode == 0) Debug.Log($"Deployed Successfully.");
else Debug.LogError($"Deploy Failed. \n{errorLog}");
process.Close();
}
private static void BuildAndroidAndRun(string name, string[] levels) => BuildAndroid(name, levels, BuildOptions.AutoRunPlayer);
private static void PrintReport(BuildReport report) {
if (report.summary.result == BuildResult.Succeeded) {
Debug.Log($"Build Succeeded {report.summary.totalSize} bytes");
} else {
Debug.LogError("Build Failed");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment