Skip to content

Instantly share code, notes, and snippets.

@larryhou
Last active March 31, 2023 11:07
Show Gist options
  • Save larryhou/3c6f5dab38acd4130e12 to your computer and use it in GitHub Desktop.
Save larryhou/3c6f5dab38acd4130e12 to your computer and use it in GitHub Desktop.
Command-line building tool for Unity project
using UnityEditor;
using System.Collections.Generic;
// Put this memu command script into Assets/Editor/
class ExportTool
{
static void ExportXcodeProject ()
{
EditorUserBuildSettings.SwitchActiveBuildTarget (BuildTarget.iOS);
EditorUserBuildSettings.symlinkLibraries = true;
EditorUserBuildSettings.development = true;
EditorUserBuildSettings.allowDebugging = true;
List<string> scenes = new List<string>();
for (int i = 0; i < EditorBuildSettings.scenes.Length; i++)
{
if (EditorBuildSettings.scenes [i].enabled)
{
scenes.Add (EditorBuildSettings.scenes [i].path);
}
}
BuildPipeline.BuildPlayer (scenes.ToArray (), "iOSProj", BuildTarget.iOS, BuildOptions.None);
}
}
#!/bin/bash
PROJ_DIR=.
PRODUCT_NAME='RollBall'
IOS_PROJ='iOSProj'
IDENTIFIER="com.larryhou.samples.rollball"
while getopts :n:p:i:h OPTION
do
case ${OPTION} in
i) IDENTIFIER=${OPTARG};;
n) PRODUCT_NAME=${OPTARG};;
f) IOS_PROJ=${OPTARG};;
p) PROJ_DIR=${OPTARG};;
h) echo "Usage: $(basename $0) -i [IDENTIFIER] -n [PRODUCT_NAME] -p [IOS_PROJ] -h [HELP]"
exit;;
:) echo "ERR: -${OPTARG} 缺少参数, 详情参考: $(basename $0) -h" 1>&2
exit 1;;
?) echo "ERR: 输入参数-${OPTARG}不支持, 详情参考: $(basename $0) -h" 1>&2
exit 1;;
esac
done
cd "${PROJ_DIR}"
if [ -d "${IOS_PROJ}" ]
then
rm -fr "${IOS_PROJ}"
fi
mkdir "${IOS_PROJ}"
# Export unity project to xcode project
# ExportTool.ExportXcodeProject is defined in [Assets/Editor/ExportTool.cs]
/Applications/Unity/Unity.app/Contents/MacOS/Unity -batchmode -quit -projectPath "${PWD}" -executeMethod ExportTool.ExportXcodeProject -logFile "${PWD}/${IOS_PROJ}/export.log"
if [ ! -d "${IOS_PROJ}/Unity-iPhone.xcodeproj" ]
then
echo "[ERR]Exporting unity project to Xcode failed."
exit
fi
# Update PRODUCT_NAME
sed -i '' "s/PRODUCT_NAME\ =\ ProductName/PRODUCT_NAME\ =\ \"${PRODUCT_NAME}\"/g" "${IOS_PROJ}/Unity-iPhone.xcodeproj/project.pbxproj"
# Update PRODUCT_BUNDLE_IDENTIFIER
sed -i '' "s/com.Company.\${PRODUCT_NAME}/${IDENTIFIER}/g" "${IOS_PROJ}/Info.plist"
# Build for *.app
# CODE_SIGN_IDENTITY="iPhone Developer"
xcodebuild -project "${IOS_PROJ}/Unity-iPhone.xcodeproj"
# Package for *.ipa
xcrun -sdk iphoneos PackageApplication -v "${PWD}/${IOS_PROJ}/build/Release-iphoneos/${PRODUCT_NAME}.app" -o "${PWD}/${IOS_PROJ}/${PRODUCT_NAME}.ipa"
@qcha0s
Copy link

qcha0s commented Jun 15, 2021

Suggest minor edit to keep the whole thing in a single language

case ${OPTION} in
        i) IDENTIFIER=${OPTARG};;
	n) PRODUCT_NAME=${OPTARG};;
	f) IOS_PROJ=${OPTARG};;
	p) PROJ_DIR=${OPTARG};;
	h) echo "Usage: $(basename $0) -i [IDENTIFIER] -n [PRODUCT_NAME] -p [IOS_PROJ] -h [HELP]"
		  exit;;
	:) echo "ERR: -${OPTARG} Missing parameters, reference for details: $(basename $0) -h" 1>&2
		  exit 1;;
	?) echo "ERR: Input parameters-${OPTARG}Not supported, reference for details: $(basename $0) -h" 1>&2
		  exit 1;;
esac

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment