Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save PatHightree/3498504 to your computer and use it in GitHub Desktop.
Save PatHightree/3498504 to your computer and use it in GitHub Desktop.
Unity build menu with 2 options: 'Build Current Target' and 'push latest build to Android'.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using UnityEditor;
using UnityEditor.Callbacks;
using UnityEngine;
using System.Linq;
using Debug = UnityEngine.Debug;
public class BuildMenu : MonoBehaviour {
private static readonly Dictionary<BuildTarget, string> Destinations = new Dictionary<BuildTarget, string> {
{ BuildTarget.Android, @"- Publish -\game.apk" },
{ BuildTarget.StandaloneWindows, @"- Publish -\game.exe" }
};
[MenuItem("Build/Build Current Target")]
public static void BuildCurrentTarget() {
UpdateScenesList();
// Collect the names of all scenes listed in the Build Settings dialog.
string[] levels = new string[EditorBuildSettings.scenes.Length];
for (int i = 0; i < EditorBuildSettings.scenes.Length; i++) {
EditorBuildSettingsScene scene = EditorBuildSettings.scenes[i];
levels[i] = scene.path;
}
BuildPipeline.BuildPlayer(
levels,
Destinations[EditorUserBuildSettings.activeBuildTarget],
EditorUserBuildSettings.activeBuildTarget,
BuildOptions.None);
}
[MenuItem("Build/Android/Push Latest Build")]
public static void PushToAndroid() {
// Determine the location of the Android Debug Bridge.
string adbLocation = PlayerPrefs.GetString("Android debug bridge location");
if (string.IsNullOrEmpty(adbLocation) || !File.Exists(adbLocation))
adbLocation = EditorUtility.OpenFilePanel(@"Locate Android Debug Bridge (android-sdk\platform-tools\adb.exe)", Environment.CurrentDirectory, "exe");
if (string.IsNullOrEmpty(adbLocation) || !File.Exists(adbLocation)) {
Debug.LogError("Cannot find adb.exe.");
return;
}
PlayerPrefs.SetString("Android debug bridge location", adbLocation);
ProcessStartInfo info = new ProcessStartInfo {
FileName = adbLocation,
Arguments = string.Format("install -r \"{0}\"", Path.GetFullPath(Destinations[BuildTarget.Android])),
WorkingDirectory = Path.GetDirectoryName(adbLocation),
};
Process.Start(info);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment