Building Unity iOS workers is experimental.
iOS support has not been tested with Unity 2018.x.
This page describes the required steps for a SpatialOS Unity project to build for iOS build targets. It assumes that you have an existing SpatialOS project, which you are intending to add iOS support using Unity.
While following this guide, you will adjust some of the SpatialOS scripts and Unity settings to prepare everything for iOS. This is needed to ensure that the Xcode project contains the necessary code to run your iOS worker successfully. The resulting Xcode project suffers from cyclic dependencies due to a bug in Unity and we provide you with a script to fix this.
At the end of the guide, you will be able to run and connect your iOS worker to a running deployment.
This guide assumes that you have an existing SpatialOS project, which you are intending to add iOS support using Unity.
First, determine if you have automatically generated build scripts for Unity turned on:
- Locate the
workers/unityfolder of your SpatialOS project. - Open
spatialos.UnityClient.worker.jsonin a text editor. - Open
spatialos.UnityWorker.worker.jsonin a text editor. - If either of the above files have the line
"generated_build_scripts_type" : "unity", automatically generated build scripts are turned on.
Next, check if the following three files already exist in the workers/unity folder of your SpatialOS project:
spatialos.unity.client.build.jsonspatialos.unity.worker.build.jsonspatialos_worker_packages.json
Do you see all three of those files?
-
I see all three of those files:
- Remove the line
"generated_build_scripts_type" : "unity"from BOTH of the build scripts (spatialos.UnityClient.worker.jsonandspatialos.UnityWorker.worker.json) as described above. This will prevent them from being over-written each time you build.
- Remove the line
-
I don't see any of those files:
- Run
spatial worker buildonce - The files should now be generated.
- Remove the line
"generated_build_scripts_type" : "unity"from BOTH of the build scripts i.e.spatialos.UnityClient.worker.jsonandspatialos.UnityWorker.worker.jsonas described above. This will prevent them from being over-written each time you build.
- Run
Add an additional worker package to your Unity worker's worker packages JSON file:
-
Locate the
workers/unityfolder of your SpatialOS project. -
Open
spatialos_worker_packages.jsonin a text editor. -
By default, you would have several
targetsspecified, each with apath,type, and a list ofpackages. -
Inside the
targetsarray, add the following:{ "path": "Assets/Plugins/Improbable/CoreSDK/core-dynamic-arm-ios", "type": "worker_sdk", "packages": [ { "name": "core-dynamic-arm-ios" } ] }, { "path": "Assets/Plugins/Improbable/CoreSDK/core-dynamic-x86_64-ios", "type": "worker_sdk", "packages": [ { "name": "core-dynamic-x86_64-ios" } ] }
- Run
spatial codegen. - To be able to run a cloud deployment, run
spatial worker build. You will need to extract the login token from that deployment before building the iOS worker. To extract the login token, you need to do the following:- Go to your cloud deployment console overview page
- Click on the
Launchbutton - Right click on "Launch your deployment" and choose "Copy Link Address"
- Copy the string that appears after
token=in the copied link address. This is your login token.
As Unity automatically strips out code and assemblies for iOS workers, it is important to add a linker configuration
link.xml: it prevents some code from being stripped out by Unity.
For more information about iOS Worker optimizations and how to customize the linker configuration, see the Unity Documentation.
Download this file and put it into the workers/unity/Assets folder. If you are already using a link.xml, add the following lines to it:
<assembly fullname="Generated.Code" preserve="all"/>
<assembly fullname="Improbable.WorkerSdkCsharp" preserve="all"/>-
in Unity, open the
ClientSceneorUnityClientscene found in the Assets folder of the project navigator. -
Click on the
GameEntrygame object in the scene hierarchy. -
Open the
Bootstrap.csMonoBehaviour script by double clicking on the script in the Inspector. -
By default, you will see the line:
SpatialOS.ApplyConfiguration(Configuration);
Delete and replace any occurrence of it with the following code snippet:
List<string> commandLineArguments = null; #if UNITY_IOS // Set to the name of your SpatialOS project here. // This should match the project name as specified in spatialos.json const string APP_NAME = "<insert_your_project_name_here>"; commandLineArguments = new List<string>(System.Environment.GetCommandLineArgs()); commandLineArguments.AddRange(new List<string>() { "+" + CommandLineConfigNames.AppName, APP_NAME }); #endif SpatialOS.ApplyConfiguration(Configuration, commandLineArguments);
-
IMPORTANT: Make sure to set the
APP_NAMEconstant to your project name. This should match the project name as specified inspatialos.json. -
In the
Bootstrap (Script)panel of the Inspector:-
Click to expand
Configuration->Debugging.- For
Infra Service URL:-
If testing locally (
spatial local launch), set to:http://127.0.0.1:21000 -
If testing against a deployment (
spatial cloud launch), set to:https://api.spatial.improbable.io
-
- For
-
Click to expand
Configuration->SpatialOS Application.- For
Assembly Name:- If testing locally (
spatial local launch), set to:local_assembly - If testing against a deployment , leave it blank.
- If testing locally (
- For
Login Token:- If testing locally (
spatial local launch), leave it blank. - If testing against a deployment, set it to a valid login token.
- If testing locally (
These settings need to be applied whenever you switch between using cloud and local deployments.
- For
-
This image highlights the key settings required on your Bootstrap game object:
-
The following build settings are recommended for compatibility with SpatialOS.
-
Within Unity, access the build settings via:
File->Build Settings... -
Under
Platform, selectiOSsuch that it is highlighted. -
Click on
Player Settings.... This should show thePlayer Settingsin the Inspector. -
Configure
Other Settingswith the following settings:-
Configuration:
- Scripting Backend:
IL2CPP - API Compatibility Level:
.NET 2.0 - Target SDK:
Device SDKorSimulator SDK - Target minimum iOS Version:
10.2or higher - Allow downloads over HTTP (nonsecure): Checked
- Architecture:
Universal(Device) orx86_64(Simulator) - Scripting Define Symbols:
CROSS_PLATFORM_INPUT;MOBILE_INPUT
- Scripting Backend:
-
Optimization
- Strip Engine Code: Un checked
- Script Call Optimization:
Slow and safe
-
When you're finished, your settings should look like this:
-
Open workers/unity/Assets/player-build-config.json and update the targets for the UnityClient. It should look like this:
"UnityClient": {
"Targets": [
"iOS"
],
"Assets": "Streaming"
}
You can add additional build targets, if you want to build your client on multiple platforms.
When building for iOS you need to change the plugin settings for the Windows CoreSdkDll plugin packages, to avoid a name collision when building the client.
-
In Unity, open
Assets/Plugins/Core/Windows/Plugins/x86and select theCoreSdkDll.dllplugin. -
Change the settings in the Unity inspector so that the plugin will not be included for the iOS platform. This should look as followed:
-
Repeat step 1 and 2 for the
x86_64version of the plugin inAssets/Plugins/Core/Windows/Plugins/x86_64.
Now you can use Unity to generate the Xcode project.
To do this:
- Use the
Improbable->Build->Build development/deployment workersmenu in Unity, or, - Run
spatial worker build UnityClient.
This will create a Xcode project in workers/unity/build/worker/UnityClient@iOS/UnityClient@iOS/
Once Unity has completed building the iOS worker, you will need to use Xcode to build and run the application on your iOS device or simulator. There are several settings to configure in the generated Xcode project.
-
Open the Xcode project located in:
workers/unity/build/worker/UnityClient@iOS/UnityClient@iOS/Unity-iPhone.xcodeproj -
In the
Project Navigatorlocated on the left panel of Xcode, click onUnity-iPhone: -
On the main pane of Xcode, click on Unity-iPhone listed under
Targets(notProject!) -
Click on the
Build Settingstab.Make sure that both
AllandCombinedtab settings are active. Then, make the following changes:- Architectures
Architectures:x86_64(Simulator) ORarmv7,armv7s,arm64(Device).Base SDK: Change toLatest iOSfrom the dropdown menu (e.g.,Latest iOS (iOS 10.2)).Valid Architectures:x86_64(Simulator) ORarmv7,armv7s,arm64(Device).
- Linking
Runpath Search Paths: Add@executable_path.
- Search Paths
Library Search Paths: Remove all double quotes from the entries listed there. e.g.,$(SRCROOT)/Librariesshould be just$(SRCROOT)/Libraries
- ENABLE_BITCODE
- Set to
NO
- Set to
- Architectures
Hint: Use the search bar to help find these settings.
-
Locate the appropriate platform version of the
libCoreSdkDll.dylibin your project.- For iOS Simulators, locate it in
workers/unity/Assets/Plugins/Improbable/CoreSDK/core-dynamic-x86_64-ios/. - For iOS Devices, locate it in
workers/unity/Assets/Plugins/Improbable/CoreSDK/core-dynamic-arm-ios/.
- For iOS Simulators, locate it in
-
Copy the selected
libCoreSdkDll.dylibinto the folder:unity/build/worker/UnityClient@iOS/UnityClient@iOS/Data.You can do this also by drag-and-dropping the file into the
Datafolder in the Xcode project navigator. The image below shows how your project and file system should look:
-
Inside your Xcode project, go to the Build Phases Tab and add
libCoreSdkDll.dylibto theCopy Filessection as an Executable.If you don't see a
Copy Filessection, select your application target and go toEditor->Add Build Phase->Add Copy Files Build Phasein the Xcode menu. -
Codesign the
libCoreSdkDll.dylibin your project using your Apple Certificates. This can be done by running the following command in the terminal:codesign -s "iPhone Developer" <path_to_project>/workers/unity/build/worker/UnityClient@iOS/UnityClient@iOS/Data/libCoreSdkDll.dylib
-
In Xcode, start building your project using the
Product->Buildmenu or by usingCMD+B. -
If you observe build errors related to
unknown type names, this is due to a bug in Unity's IL2CPP code generation. We provide a script to fix these errors automatically:-
Ensure you have
pcregrepinstalled to run the script successfully. -
Download this script, if you use Unity 5.6.0.
-
Download this script, if you use Unity 2017.3.
-
Place the script into
workers/unityand run it. -
You might see the following error when running the script:
"xcode-select: error: tool 'xcodebuild' requires Xcode, but active developer directory '/Library/Developer/CommandLineTools' is a command line tools instance".This can be fixed by running
xcode-select -s /Applications/Xcode.app/Contents/Developer.
-
-
Run the project from Xcode by using the
Product->Runmenu, or usingCMD+R, or pressing the play button.Note: As with all client workers, make sure that your server is running, either locally (
spatial local launch) or from a deployment (spatial cloud launch).
If successful, Xcode will complete the build and begin running the application on your iOS device or iOS Simulator.














