How to Embed Unity in a Native iOS Swift Project

How to Embed Unity in a Native iOS Swift ProjectEmbed Unity in a Swift project’s View Controller with VuforiaVanessa LeungBlockedUnblockFollowFollowingMay 23I 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.

10Part 1: Export Unity projectThe export is a critical part of the process — make sure you follow all the Player Settings correctly.

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

Add post-build script to Assets/Editor folder2.

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’t4.

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

Uncheck Auto Graphics APIb.

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 CodeUncheck Auto Graphics API & Metal Editor Support, select Open GLES3, Open GLES2Uncheck Strip Engine Coded.

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 projectDownload the Unity folder and copy all the items inside it to the Unity folder under the Xcode target folder.

Open .

xcworkspace in the project navigator, drag the Unity folder you just downloaded to your target.

Select Copy items if needed and Create Groups.

Go to the Unity project export folder (not the one in step 2), drag Classes and Libraries folder to your target folder in the project navigator (one folder at a time), select Copy items if needed and Create Groups.

Drag Data folder too, uncheck Copy items if needed and select Create Group Reference.

Drag the Vuforia.

framework under the Framework folder, select Copy items if needed and Create Groups.

Your project’s hierarchy should now look like this6.

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 File7.

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

Set Enable Testability to No8.

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.

framework9.

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_CFLAGSAppend -DNET_4_0 to OTHER_CFLAGS11.

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.

plistPart 3: Edit native iOS Swift filesFollow this code to edit your AppDelegate.

swift .

Add a new View Controller for your Unity view.

Create a new UnityViewController.

swiftAdd these lines in func viewDidLoad():Part 4: Run the projectThat’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.

ReferencesHere 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 ProjectUnity-iOSHow to embed a Unity game into an iOS native Swift AppHow to use Unity 3D within an iOS app.

. More details

Leave a Reply