How to Embed Unity in a Native iOS Swift Project

Embed Unity in a Swift project’s View Controller with Vuforia

Vanessa Leung
May 23 · 4 min read

I am using Unity to implement Vuforia’s Cloud Recognition and want to embed the scene in my Swift project’s View Controller.

Most of the tutorials I found online were either using old versions of Unity and Objective-C, or not using Vuforia. So, I created this tutorial.

Just for your reference, I am using: Xcode version: Version 10.2.1, Unity version: 2018.3.8f1, Vuforia version: 8.0.10

Part 1: Export Unity project

The export is a critical part of the process — make sure you follow all the Player Settings correctly.

  1. Copy this post build script to YourUnityProjName/Assets/Editor folder.
Add post-build script to Assets/Editor folder

2. Find and edit the two lines below, according to the comments I have made in the script:

3. File -> Build Settings -> Switch to iOS the platform if you haven’t

4. File -> Build Settings -> Player Settings -> Other Settings

a. Uncheck Auto Graphics API

b. If you are using Vuforia, in Graphics API, delete Metal, add OpenGLES3, OpenGLES2. If you are not using Vuforia you may skip this step.

c. Uncheck Metal Editor Support& Strip Engine Code

Uncheck Auto Graphics API & Metal Editor Support, select Open GLES3, Open GLES2
Uncheck Strip Engine Code

d. Build and make a note of where you export your Unity project.

5. Go to your Xcode target folder (the folder with the YOUR_PROJ_NAME.xcodeproj file) — a Unity folder has been created containing Exports.xcconfig.

Part 2: Embed Unity in the iOS project

  1. Download the Unity folder and copy all the items inside it to the Unity folder under the Xcode target folder.
Your project’s hierarchy should now look like this

6. Select your project in the project navigator and go to the info tab. UnderConfigurations, you should see Pods-YOUR_PROJ_NAME.debug and Pods-yourproj.release are set as your target’s Debug and Release configuration files by default. Change them both toUnity.

Change Configuration File

7. Go to the Build Settings tab, search for Enable Testability and set it to No.

Set Enable Testability to No

8. Click on your target and go to the General tab. Under Embedded binaries, add Vuforia.framework. Under Linked Frameworks and Libraries, add AVKit.framework.

Add Vuforia.framework and AVKit.framework

9. If you’re using Cocoapods in your native iOS project, add the following lines to the beginning of the Unity.xcconfig file.

#include "../../Pods/Target Support Files/Pods-YOUR_PROJ_NAME/Pods-YOUR_PROJ_NAME.debug.xcconfig"
#include "../../Pods/Target Support Files/Pods-YOUR_PROJ_NAME/Pods-YOUR_PROJ_NAME.release.xcconfig"

10. If you are using .NET 4.0, in the Unity.xcconfig, append -DNET_4_0 to OTHER_CFLAGS

Append -DNET_4_0 to OTHER_CFLAGS

11. Using the filter in the project navigator to search for these two files: DynamicLibEngineAPI-functions.h, DynamicLibEngineAPI.mm under the Unity/Classes folder, and delete them using Remove references only.

12. Search for SplashScreen.mm under Unity/Classes/UI and replace #include “Classes/Unity/UnitySharedDecls.h” with #include “../Classes/Unity/UnitySharedDecls.h”.

13. Search for DeviceSettings.mm under Unity/Classes/Unity and add return deviceUnknown; between line 280 (#endif) and line 281 (}).

Add `return deviceUnknown;’

14. If you are using Vuforia, go to your Info.plist, add key Privacy — Camera Usage Description. Here you can add a message that tells users that the app requires use of the camera.

Add Privacy — Camera Usage Description to Info.plist

Part 3: Edit native iOS Swift files

  1. Follow this code to edit your AppDelegate.swift .

Part 4: Run the project

That’s it — you should be able to run your project on your device!

Note: if you are using Vuforia you will not be able to run it in the simulator — you will get countless error messages.

References

Here are some other tutorials that I have found to be useful. If you still need help, try these resources:

Integrating Unity Project into Xcode Swift Project

Unity-iOS

How to embed a Unity game into an iOS native Swift App

How to use Unity 3D within an iOS app

Better Programming

Advice for programmers.

Vanessa Leung

Written by

M.S. in Business Analytics @UC Davis ’20 | Actively seeking an analytical position | Newbie in Software Programming & Data Science

Better Programming

Advice for programmers.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade