Unity3D – Tips when building your game with an Editor script

, ,

Here are some basic tricks when you need to build your game by code.

First you can launch a build process with a single line of code, for example :

[MenuItem("Exoa/Build/Android")]
public static void PerformBuildAndroid()
{
	string fullPath = Application.dataPath.Replace("/Assets", "/") + "_BUILDS/android/build.apk";
	// Build Game
	BuildPipeline.BuildPlayer(allScenes, fullPath, BuildTarget.StandaloneWindows, BuildOptions.None);
}

For Android

Then you can install your game on the connected Android device like this :

[MenuItem("Exoa/Build/Install build on Android")]
public static void InstallAndroid()
{
	// Run the game (Process class from System.Diagnostics).
	string fullPath = Application.dataPath.Replace("/Assets", "/") + "_BUILDS/android/build.apk"; 
	proc = new System.Diagnostics.Process();
	proc.StartInfo.FileName = "D:/SDK/android/sdk/platform-tools/adb.exe"; // replace with your adb exe path
	proc.StartInfo.Arguments = "-d install -r " + fullPath;
	proc.Start();
}

Then you can launch the application on the android device like this :

[MenuItem("Exoa/Build/Launch on Android")]
public static void LaunchAndroid()
{
	// Run the game (Process class from System.Diagnostics).
	string fullPath = Application.dataPath.Replace("/Assets", "/") + "_BUILDS/android/build.apk";
	string appid = PlayerSettings.bundleIdentifier;
	proc = new System.Diagnostics.Process();
	proc.StartInfo.FileName = "D:/SDK/android/sdk/platform-tools/adb.exe"; // replace with your adb exe path
	proc.StartInfo.Arguments = "shell am start -n " + appid + "/com.unity3d.player.UnityPlayerNativeActivity"; 
	proc.Start();
}

For Desktop

For desktop releases, you can automatically launch the build with the Process() class :

[MenuItem("Exoa/Build/Run Exe %&r")]
public static void RunGame()
{
	string fullPath = Application.dataPath.Replace("/Assets", "/") + "_BUILDS/win/build.exe";

	// Run the game (Process class from System.Diagnostics).
	Process proc = new Process();
	proc.StartInfo.FileName = fullPath;
	proc.Start();
}

Opening build folder

You can finally automatically open the build folder like Unity do :

[MenuItem("Exoa/Build/Open Build Folder")]
public static void OpenBuildFolder()
{
	string fullPath = Application.dataPath.Replace("/Assets", "/") + "_BUILDS/";
	fullPath = fullPath.Replace("/","\\");
	Debug.Log(fullPath);
	proc = new System.Diagnostics.Process();
	proc.StartInfo.FileName = "explorer.exe";
	proc.StartInfo.Arguments = fullPath;
	proc.Start();
}