Skip to content

Instantly share code, notes, and snippets.

@gecko655
Last active September 21, 2016 03:59
Show Gist options
  • Save gecko655/332123efa449f39507cb2f4c5bcaca5b to your computer and use it in GitHub Desktop.
Save gecko655/332123efa449f39507cb2f4c5bcaca5b to your computer and use it in GitHub Desktop.
Unityプロジェクトをコマンド1発でiOS実機にデプロイ&起動する ref: http://qiita.com/gecko655/items/d82568b83981a189af76
using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
public class BatchBuild : MonoBehaviour {
static private string ApplicationName = "Hoge-application";
//for Android
static void BuildProject_Android(){
Debug.Log ("AndroidBuild...");
string[] scenePaths = GetEnabledScenePaths();
string outputPath = Application.dataPath + "/../"+ApplicationName+".apk";
BuildTarget target = BuildTarget.Android;
BuildOptions opt = BuildOptions.None;
ApplicationBuild(scenePaths,outputPath,target,opt);
}
//for iOS Device
static void BuildProject_iOS(){
Debug.Log ("iOSBuild...");
string[] scenePaths = GetEnabledScenePaths();
string outputPath = "Device";
BuildTarget target = BuildTarget.iOS;
BuildOptions opt = BuildOptions.SymlinkLibraries;
//ProjectDataSize Reduction
EditorUserBuildSettings.symlinkLibraries = true;
//Prevent SimulatorBuild
PlayerSettings.iOS.sdkVersion = iOSSdkVersion.DeviceSDK;
ApplicationBuild(scenePaths,outputPath,target,opt);
}
//RunBuild
static void ApplicationBuild(string[] scenePaths,string outputPath,BuildTarget target,BuildOptions opt){
string error = BuildPipeline.BuildPlayer(scenePaths,outputPath,target,opt);
if (!string.IsNullOrEmpty(error))
Debug.LogError(error); //BuildFailed
EditorApplication.Exit(string.IsNullOrEmpty(error) ? 0 : 1);
}
//return Enabled Scene in BuildSettings's Scenes In Build
static string[] GetEnabledScenePaths() {
List<string> sceneList = new List<string>();
//Check Scenes Enabled
foreach(var scene in EditorBuildSettings.scenes){
if(scene.enabled)
sceneList.Add (scene.path);
}
return sceneList.ToArray();
}
}
#Locate this script under an empty directory.
PROJECT_BUILD_DIR=`pwd`/`dirname $BASH_SOURCE` # <= Must be absolute path
PROJECT_BASE=/path/to/unity-project-base-directory # <= Edit this
APP_NAME=Hoge-application.app # <= Edit this
cd $PROJECT_BUILD_DIR
echo "Copying sources"
rm -r Assets 2>/dev/null
rm -r ProjectSettings 2>/dev/null
cp -r $PROJECT_BASE/Assets/ Assets/
cp -r $PROJECT_BASE/ProjectSettings/ ProjectSettings/
echo "Building Unity"
/Applications/Unity/Unity.app/Contents/MacOS/Unity -batchmode -projectPath $PROJECT_BUILD_DIR -executeMethod BatchBuild.BuildProject_iOS -quit
if [ $? != 0 ]; then
echo "Unity Build failed"
exit $?
fi
echo "Building Xcode Project"
xcodebuild -project $PROJECT_BUILD_DIR/Device/Unity-iPhone.xcodeproj -target Unity-iPhone -configuration Release build
if [ $? != 0 ]; then
echo "Xcode Build failed"
exit $?
fi
echo "Deploying to the device"
ios-deploy --debug --bundle $PROJECT_BUILD_DIR/Device/build/Release-iphoneos/$APP_NAME
if [ $? -eq 254 ]; then
echo "Deploying finished, but couldn't run app"
exit $?
elif [ $? -ne 0 ]; then
echo "Deploying failed"
exit $?
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment