Skip to content

Instantly share code, notes, and snippets.

@edwardrowe
Last active April 8, 2018 21:48
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edwardrowe/cdcf164d78920132e6e3 to your computer and use it in GitHub Desktop.
Save edwardrowe/cdcf164d78920132e6e3 to your computer and use it in GitHub Desktop.
Unity BuildScript for Mobile
/*The MIT License (MIT)
Copyright (c) 2016 Edward Rowe (@edwardlrowe)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
using UnityEngine;
using UnityEditor;
namespace RedBlueTools
{
public class BuildScripts : EditorWindow
{
const int NumDigitsPerVersionIteration = 2;
const string companyDisplayName = "Red Blue Games";
static string companyName = companyDisplayName.ToLower().Replace(" ", null);
string filename = "ministackem-v" + PlayerSettings.bundleVersion;
string appName = "Mini Stackem";
string bundleIdentifier = "com." + companyName + ".ministackem";
bool isDevelopmentBuild;
string version = PlayerSettings.bundleVersion;
string savePath;
bool buildAndroid = true;
bool buildIOS;
bool iOSSimulationBuild = false;
string androidKeystorePath = PlayerSettings.Android.keystoreName;
string androidKeystorePassword;
string androidKeyAlias = PlayerSettings.Android.keyaliasName;
string androidKeyAliasPassword;
BuildType buildType;
public enum BuildType
{
Development,
Release
}
void ConfigureBuildOptionsForBuildType(BuildType type)
{
switch (type) {
case BuildType.Development:
isDevelopmentBuild = true;
break;
case BuildType.Release:
isDevelopmentBuild = false;
break;
default:
Debug.LogError("Unrecognized build type selected for build.");
return;
}
}
[MenuItem("Build/ExportBuild")]
public static void ExportBuild()
{
EditorWindow.GetWindow<BuildScripts>("Export Builds");
}
void OnGUI()
{
GUILayout.Label("BuildSettings", EditorStyles.boldLabel);
filename = EditorGUILayout.TextField("Filename: ", filename);
version = EditorGUILayout.TextField("Version: ", version);
buildType = (BuildType)EditorGUILayout.EnumPopup("Build Type: ", buildType);
// Handle App Name and Bundle ID
appName = EditorGUILayout.TextField("App Name: ", appName);
bundleIdentifier = EditorGUILayout.TextField("Bundle Identifier: ", bundleIdentifier);
EditorGUILayout.LabelField("Platform Settings - iOS", EditorStyles.boldLabel);
iOSSimulationBuild = EditorGUILayout.Toggle("iOS Simulation", iOSSimulationBuild);
GUILayout.Label("Build Targets", EditorStyles.boldLabel);
buildAndroid = EditorGUILayout.Toggle("Android", buildAndroid);
buildIOS = EditorGUILayout.Toggle("iOS", buildIOS);
if (buildAndroid && buildType == BuildType.Release) {
EditorGUILayout.LabelField("Android Publishing Settings", EditorStyles.boldLabel);
bool PressedKeystore = GUILayout.Button("Locate Keystore");
androidKeystorePath = EditorGUILayout.TextField("Path To Keystore: ", androidKeystorePath);
androidKeystorePassword = EditorGUILayout.PasswordField("Keystore Password: ", androidKeystorePassword);
androidKeyAlias = EditorGUILayout.TextField("Key Alias: ", androidKeyAlias);
androidKeyAliasPassword = EditorGUILayout.PasswordField("Key Alias Password: ", androidKeyAliasPassword);
if (PressedKeystore) {
androidKeystorePath = EditorUtility.OpenFilePanel("Open Keystore", ".", "keystore");
}
} else {
// Clear out our keystore info when BuildType is development
androidKeystorePath = string.Empty;
androidKeystorePassword = string.Empty;
androidKeyAlias = string.Empty;
androidKeyAliasPassword = string.Empty;
}
bool UpdatePressed = GUILayout.Button("Update");
bool BuildPressed = GUILayout.Button("Build");
if (BuildPressed || UpdatePressed) {
try {
PrepareUniversalBuild();
if (BuildPressed) {
savePath = PromptUserForSaveLocation();
if (string.IsNullOrEmpty(savePath)) {
throw new System.ArgumentException("No save path provided");
}
}
} catch (System.FormatException formatException) {
Debug.LogError("Error during update: " + formatException.Message);
return;
}
ConfigureUniversalBuildSettings();
if (BuildPressed && buildIOS == false && buildAndroid == false) {
Debug.LogError("Tried to do build without specifying a build target.");
return;
}
if (buildIOS) {
ConfigureiOSBuildSettings();
if (BuildPressed) {
ExportiOSBuild();
}
}
if (buildAndroid) {
ConfigureAndroidBuildSettings();
if (BuildPressed) {
ExportAndroidBuild();
}
}
}
}
#region Field Validation
void ValidateAllEditorFields()
{
ValidateStringAsFilename(filename);
ValidateStringAsBundleIdentifier(bundleIdentifier);
ValidateStringAsVersionNumber(version);
}
void ValidateStringAsFilename(string inputString)
{
if (string.IsNullOrEmpty(inputString)) {
throw new System.FormatException("Filename string formatted incorrectly. Must be non-empty.");
}
}
void ValidateStringAsBundleIdentifier(string inputString)
{
if (string.IsNullOrEmpty(inputString)) {
throw new System.FormatException("BundleIdentifier string formatted incorrectly. Must be non-empty.");
}
System.Text.RegularExpressions.Regex stringFormat =
new System.Text.RegularExpressions.Regex("^com\\." + companyName + "\\.[\\w\\d]+$");
if (!stringFormat.IsMatch(inputString)) {
throw new System.FormatException("BundleIdentifier string formatted incorrectly. Must be of the regex format: "
+ stringFormat.ToString());
}
}
void ValidateStringAsVersionNumber(string inputString)
{
if (string.IsNullOrEmpty(inputString)) {
throw new System.FormatException("Version number is not a valid format. Must be non-empty");
}
// Valid format of version number is ##.##.## or 1.12.1
string numDigitsPerIterationAsChar = NumDigitsPerVersionIteration.ToString();
string regexNoMoreThanXDigits = "[0-9]{1," + numDigitsPerIterationAsChar + "}";
System.Text.RegularExpressions.Regex stringFormat =
new System.Text.RegularExpressions.Regex("^" + regexNoMoreThanXDigits +
"\\." + regexNoMoreThanXDigits + "\\."
+ regexNoMoreThanXDigits + "$");
if (!stringFormat.IsMatch(inputString)) {
throw new System.FormatException("Version number is not a valid format." +
" Please match the format ##.##.##, or as a regular expression: " + stringFormat.ToString());
}
}
#endregion
#region Build Configuration
void PrepareUniversalBuild()
{
ValidateAllEditorFields();
}
string PromptUserForSaveLocation()
{
string path = EditorUtility.SaveFolderPanel("Choose location to save build(s)", "", "");
if (string.IsNullOrEmpty(path)) {
return string.Empty;
}
return path;
}
void ConfigureUniversalBuildSettings()
{
// Configure PlayerSettings that never change //
PlayerSettings.companyName = companyDisplayName;
PlayerSettings.defaultInterfaceOrientation = UIOrientation.Portrait;
PlayerSettings.allowedAutorotateToPortrait = true;
PlayerSettings.allowedAutorotateToPortraitUpsideDown = true;
PlayerSettings.allowedAutorotateToLandscapeLeft = false;
PlayerSettings.allowedAutorotateToLandscapeRight = false;
PlayerSettings.bundleIdentifier = bundleIdentifier;
PlayerSettings.bundleVersion = version;
PlayerSettings.productName = appName;
}
void ConfigureAndroidBuildSettings()
{
PlayerSettings.Android.androidTVCompatibility = false;
PlayerSettings.Android.bundleVersionCode = ConvertVersionStringToVersionCode(version);
ConfigurePlatformDefinesForBuiltTarget(BuildTargetGroup.Android);
PlayerSettings.Android.keystoreName = androidKeystorePath;
PlayerSettings.Android.keystorePass = androidKeystorePassword;
PlayerSettings.Android.keyaliasName = androidKeyAlias;
PlayerSettings.Android.keyaliasPass = androidKeyAliasPassword;
}
int ConvertVersionStringToVersionCode(string inputString)
{
// Version string must be the correct format
// MAJOR_VERSION . MINOR_VERSION . BUILD
// 4.1.1 = 4 * 10000, 1 * 100 + build = 40101
string[] parsedVersion = inputString.Split('.');
int maxNumIterationsInVersion = (int)Mathf.Pow(10, NumDigitsPerVersionIteration);
int versionOrder = 0;
int iteration = 0;
for (int i = parsedVersion.Length - 1; i >= 0; i--) {
int iterationValue = int.Parse(parsedVersion[i]) * (int)Mathf.Pow(maxNumIterationsInVersion, iteration);
versionOrder += iterationValue;
iteration++;
}
return versionOrder;
}
void ConfigurePlatformDefinesForBuiltTarget(BuildTargetGroup targetGroup)
{
string defineSymbols = string.Empty;
// Turn off Google Play Game Services for iOS
if (targetGroup == BuildTargetGroup.iOS) {
defineSymbols="NO_GPGS";
}
PlayerSettings.SetScriptingDefineSymbolsForGroup(targetGroup, defineSymbols);
}
void ConfigureiOSBuildSettings()
{
if (iOSSimulationBuild) {
PlayerSettings.iOS.sdkVersion = iOSSdkVersion.SimulatorSDK;
} else {
PlayerSettings.iOS.sdkVersion = iOSSdkVersion.DeviceSDK;
}
ConfigurePlatformDefinesForBuiltTarget(BuildTargetGroup.iOS);
}
#endregion
#region Export Functions
void ExportAndroidBuild()
{
BuildPipeline.BuildPlayer(GetLevelsForBuild(), savePath + "/" + filename + ".apk", BuildTarget.Android, GetBuildOptionsForBuild());
}
void ExportiOSBuild()
{
BuildPipeline.BuildPlayer(GetLevelsForBuild(), savePath + "/" + filename, BuildTarget.iOS, GetBuildOptionsForBuild());
}
string[] GetLevelsForBuild()
{
return SceneManager.GetAllPaths();
}
BuildOptions GetBuildOptionsForBuild()
{
BuildOptions buildOptions;
if (isDevelopmentBuild) {
buildOptions = BuildOptions.Development;
} else {
buildOptions = BuildOptions.None;
}
return buildOptions;
}
#endregion
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment