You can automate your Unity builds on Bitrise.
How to add your Unity project to Bitrise?
Unity is not supported 1-on-1 on Bitrise, but there is a way to setup your projects and then to test, build and release your games as fast as any other apps of a different project type. As this is a cross-platform project, in the end youâll get an IPA and an APK too, hooray! It is really easy to release them, innit? Let us give you a hand to get there.
When you set up your project, donât use the scanner but choose the other/custom optionâŚ
âŚand choose a iOS stack.
Download and install
This is a script that downloads the version of Unity you choose, so you can choose whichever matches your project. Install the editor and then youâll need its Android and iOS platforms too.
Letâs see an example: if you want to use Unity2021.3.12f1, you need to look up the package hash at Download Archive. Look for the download link under the installer:
Example on how to install the editor and the two platform support packages (Android & iOS):
#workaround for .NET issue https://github.com/dotnet/runtime/issues/64103
export COMPlus_ReadyToRun=0
envman add --key COMPlus_ReadyToRun --value 0
#download unity pkg for Apple Silicon
curl -o ./unity.pkg http://download.unity3d.com/download_unity/8af3c3e441b1/MacEditorInstallerArm64/Unity-2021.3.12f1.pkg
#download android support platform
curl -o ./android.pkg http://download.unity3d.com/download_unity/8af3c3e441b1/MacEditorTargetInstaller/UnitySetup-Android-Support-for-Editor-2021.3.12f1.pkg
#download iOSle support
curl -o ./ios.pkg http://download.unity3d.com/download_unity/8af3c3e441b1/MacEditorTargetInstaller/UnitySetup-iOS-Support-for-Editor-2021.3.12f1.pkg
If you would like to use the rosetta supported unity MacEditorInstaller for compatibility with slower performance, use this command to install it:
#download unity pkg for Apple Silicon with Rosetta support
curl -o ./unity.pkg http://download.unity3d.com/download_unity/8af3c3e441b1/MacEditorInstaller/Unity-2021.3.12f1.pkg
After installation you will find Unity in the following location:
/Applications/Unity/Unity.app/Contents/MacOS/Unity
For running Unity we need Cacerts.pem to be placed at the right location. Unity will generate this file for us, so letâs run it for the first time.
/Applications/Unity/Unity.app/Contents/MacOS/Unity -logfile &
sleep 15
sudo killall Unity
License activation
Youâll need to have a valid license key to build Unity projects. Only paid plans are working through the command line - and therefore in Bitrise.
Running the Personal (free) version needs activation through the UI and using your personal serial number.
To activate your license key use the following code, which will activate the license and link the VM with your account:
/Applications/Unity/Unity.app/Contents/MacOS/Unity -quit -batchmode -serial "$UNITY_SERIAL" -username "$UNITY_EMAIL" -password "$UNITY_PW" -logfile
Building your project
Now letâs jump to a more interesting part, building your app!
By inserting this script to your workflow you can start building your Unity apps. In the end it will generate an APK for your Android app and an IPA for your iOS app.
Youâll need this for Android:
/Applications/Unity/Unity.app/Contents/MacOS/Unity -nographics -quit -batchmode -logFile -projectPath "$BITRISE_SOURCE_DIR" -executeMethod BitriseUnity.Build -androidSdkPath "$ANDROID_HOME" -buildOutput "$BITRISE_DEPLOY_DIR/mygame.apk" -buildPlatform android
⌠and this for iOS:
/Applications/Unity/Unity.app/Contents/MacOS/Unity -nographics -quit -batchmode -logFile -projectPath "$BITRISE_SOURCE_DIR" -executeMethod BitriseUnity.Build -buildOutput "$BITRISE_SOURCE_DIR/xcodebuild" -buildPlatform ios
An Xcode project will be generated, and that will have to be built in the usual way to obtain the IPA.
Deactivation
Weâve already connected your license with the actual virtual machine, but we have to make sure itâs deactivated at the end, so you will be able to re-use it. If you fail to deactivate the license you can still contact Unity helpdesk and get it sorted out, but it is way simpler to do it here
To make sure itâs deactivated add the following script step to your workflow:
/Applications/Unity/Unity.app/Contents/MacOS/Unity -quit -batchmode -logFile -returnlicense
Set the step to Run even if previous Step failed otherwise you risk that it wonât get deactivated if your build fails!
This is what a finished build looks like on Bitrise. Pretty cool, eh?
As you can see there is an iOS.ipa and an Android.apk generated in one build! Next time youâll use this, itâll be as quick as all your other apps!
Do you have any more questions? There is a more detailed description of Unity projects available.
Happy building!
This post originally appeared on our blog here.