Skip to content

Instantly share code, notes, and snippets.

@TsubameUnity
Last active March 19, 2019 03:49
Show Gist options
  • Save TsubameUnity/c7bfab72e36a50e6370ff2fe9261519b to your computer and use it in GitHub Desktop.
Save TsubameUnity/c7bfab72e36a50e6370ff2fe9261519b to your computer and use it in GitHub Desktop.
[Unity 2019.1.0b5で確認] エディタのツールバーにAndroidビルド、インストール、実行を行うボタンを追加するスクリプト
using System;
using UnityEngine;
using UnityEditor;
using UnityEditor.EditorTools;
using System.IO;
using UnityEditor.Build.Reporting;
using System.Linq;
// 初回起動のみ実行、初回のツールバー設定で起動しないように仕組む
class PlatformToolInitializer : ScriptableSingleton<PlatformToolInitializer>
{
public bool Init { get; private set; }
[InitializeOnLoadMethod]
static void Initialize()
{
if (!instance.Init)
{
// EditorTools.activeToolTypeが初期化されていないので、少し待つ
EditorApplication.delayCall += () => {
var toolType = EditorTools.activeToolType;
EditorTools.SetActiveTool(typeof(PlatformTool));
EditorTools.SetActiveTool(toolType);
instance.Init = true;
};
}
}
}
// APKを作成し、実機にインストールと起動を自動化させる
[EditorTool("Platform Tool")]
class PlatformTool : EditorTool
{
[SerializeField]
Texture2D _toolIcon;
GUIContent _iconContent;
Type _toolType;
public override GUIContent toolbarIcon => _iconContent;
void OnEnable()
{
_iconContent = new GUIContent()
{
image = _toolIcon,
text = "Android Build And Install",
tooltip = "Android Build And Install"
};
_toolType = EditorTools.activeToolType;
EditorTools.activeToolChanged += OnChanged;
}
void OnDisable()
{
EditorTools.activeToolChanged -= OnChanged;
}
// Bat実行コマンド apkをインストールし、インストールに成功したら実行させる
// 環境変数設定が無い場合でもできるように指定
// インストール完了後にビープ音が鳴るように設定
const string Command = "/c cd \"{0}\" && " +
"adb install -r \"{1}\" && " +
"adb shell am start -n {2}/com.unity3d.player.UnityPlayerActivity --activity-clear-top && " +
"@echo  && " +
"pause";
void OnChanged()
{
// 初回のセットアップで起動しないように
if (!PlatformToolInitializer.instance.Init)
{
return;
}
if (EditorTools.IsActiveTool(this))
{
EditorApplication.delayCall += () => EditorTools.SetActiveTool(_toolType);
var projectDir = Directory.GetCurrentDirectory();
var apkPath = Path.Combine(projectDir, "test.apk");
var buildReport = BuildPipeline.BuildPlayer(
EditorBuildSettings.scenes,
apkPath,
BuildTarget.Android,
BuildOptions.Development
);
if (buildReport.summary.result != BuildResult.Succeeded)
{
Debug.LogError(buildReport.summary.result.ToString());
return;
}
// 環境変数を列挙
string environmentPaths = Environment.GetEnvironmentVariable("Path", EnvironmentVariableTarget.Machine) +
Environment.GetEnvironmentVariable("Path", EnvironmentVariableTarget.User);
// adbの環境変数があるか調査
string adbPath = environmentPaths.Split(Path.PathSeparator)
.Where(path => File.Exists(Path.Combine(path, "adb.exe")))
.FirstOrDefault();
// システム、ユーザー環境変数にadb.exeへのパスがない
if (string.IsNullOrWhiteSpace(adbPath))
{
// 無ければadb.exeの規定パスを調査
var localPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
adbPath = Path.Combine(localPath, "Android/Sdk/platform-tools");
if (!Directory.Exists(adbPath) && !File.Exists(Path.Combine(adbPath, "adb.exe")))
{
Debug.LogError("adb.exeが見つかりませんでした");
return;
}
}
var process = new System.Diagnostics.Process();
process.StartInfo.FileName = Environment.GetEnvironmentVariable("ComSpec");
process.StartInfo.Arguments = string.Format(Command, adbPath, apkPath, Application.identifier);
process.Start();
return;
}
_toolType = EditorTools.activeToolType;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment