Integrating Unity Project into Swift XCode Project

By StartxLabs
Date 27-06-19
Integrating Unity Project into Swift XCode Project
" Integrating Unity Project into Swift XCode Project"

Get your awesome Unity3D project ready, after it just follow below mentioned steps before exporting it as XCode Project:

- Go to File > Build Settings, switch platform to iOS.
- Now tap on Player settings and verify it with below-mentioned property, if not same then update those values to these values

 

-> Scripting Backend = il2cpp

-> API compatibility = .NET 4.x

-> Target Device = iPhone+iPad

-> Target SDK = Device SDK

-> Target minimum iOS Version = 9.0

-> script call optimization = slow and safe

-> Architecture = Universal

Build the Project, an Xcode project will get generated after this.(Create a Zip for backup purpose, you may need in future if some step messes up anything for you)

 

We are done from Unity Part

Now get your XCode project ready, after it just follow the below-mentioned steps for the integration of Unity with your awesome project:

 

- Create an empty folder named 'Unity' under your Xcode project folder
- Drag and drop below mentioned files and folder in your native XCode project
- Libraries folder
- Classes folder
- Data folder

 

Please confirm that classes and libraries folder has reference in native Xcode project and Data folder has reference in Unity generated Xcode project.

 

Add these 3 files in your XCode Project as it is
Bridging-Header.h
UnityBridge.h
UnityBridge.mm

 

Now setup environment for your native XCode project 

Build Options

- Enable Bitcode = No

 

Linking

- Other Linker Flags = $(inherited) -weak_framework CoreMotion -weak-lSystem $(OTHER_LDFLAGS_FRAMEWORK) -Wl,-undefined,dynamic_lookup

- Runpath search paths = $(inherited) @executable_path/Frameworks

 

Search Paths

- Header search Path = $(inherited),"$(PRODUCT_NAME)/Unity","$(PRODUCT_NAME)/Unity/Classes","$(PRODUCT_NAME)/Unity/Libraries/libil2cpp/include","$(PRODUCT_NAME)/Unity/Libraries"

- Library Search path = $(inherited),"$(PRODUCT_NAME)/Unity/Libraries","$(PRODUCT_NAME)/Unity/Libraries/libil2cpp/include"

 

Apple Clang - Code Generation

- Optimization Level = None[-00]

 

Apple Clang - Custom Compiler Flags

 

For Real Device

- Other C flags = $(inherited) -DINIT_SCRIPTING_BACKEND=1 -fno-strict-overflow -DNET_4_0 -DRUNTIME_IL2CPP=1

 

For Simulator

- Other C flags = $(inherited) -DINIT_SCRIPTING_BACKEND=1 -fno-strict-overflow -DNET_4_0 -DRUNTIME_IL2CPP=1 -DTARGET_IPHONE_SIMULATOR=1 

 

Apple Clang - Language

- C Language Dialect = C99[-std = c99] 

- Precompile Prefix header = Yes

 - Prefix header = $(PRODUCT_NAME)/Unity/Classes/Prefix.pch

 

Apple Clang - Language - Modules

- Enable Modules ( c and objective-c) = Yes

 

Apple Clang - Preprocessing

- Enable Strict Checking of objc_msgSend Calls = No

 

Apple Clang - Warnings - All Languages

- Mismatched return types = Yes

 

Swift Compiler - Code Generation

- Optimization Level = No Optimization [-Onone]

 

Swift Compiler - General

- Objective-C bridging header = $(PRODUCT_NAME)/Bridging-Header.h

 

User-Defined

- GCC_THUMB_SUPPORT = NO

- GCC_USE_INDIRECT_FUNCTION_CALLS =. NO

- UNITY_RUNTIME_VERSION =. `Your Unity Version`

- UNITY_SCRIPTING_BACKEND = Il2cpp

 

 

Other than above configuration you'll have to add some line code to below-mentioned file

AppDelegate

 

import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    
    
var window: UIWindow?
var application: UIApplication?
var unityController: UnityAppController?
    
    
    
func unityWindow() -> UIWindow? {
return UnityGetMainWindow()
}
    
func showUnityWindow() {
unityWindow()?.makeKeyAndVisible()
}
    
func hideUnityWindow() {
window!.makeKeyAndVisible()
}
    
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
// Override point for customization after application launch.
sleep(3)
self.application = application
        
unity_init(CommandLine.argc, CommandLine.unsafeArgv)
        
unityController = UnityAppController.init()
        
unityController!.application(application, didFinishLaunchingWithOptions: launchOptions)
        
return true
        
}
    
func applicationWillResignActive(_ application: UIApplication) {
        
unityController!.applicationWillResignActive(application)
}
    
func applicationDidEnterBackground(_ application: UIApplication) {
        
unityController!.applicationDidEnterBackground(application)
}
    
func applicationWillEnterForeground(_ application: UIApplication) {
        
unityController!.applicationWillEnterForeground(application)
}
    
func applicationDidBecomeActive(_ application: UIApplication) {
        
unityController!.applicationDidBecomeActive(application)
}
    
func applicationWillTerminate(_ application: UIApplication) {
        
unityController!.applicationWillTerminate(application)
}
}

 

 

 

In your ViewController add below-mentioned code to load Unity Project

 

override func viewDidLoad() {
super.viewDidLoad()
        
var appDelegate = UIApplication.shared.delegate as? AppDelegate
appDelegate?.unityWindow()!.frame = CGRect(x: 0, y: 0, width: self.view.bounds.width, height: self.view.bounds.height)
appDelegate?.showUnityWindow()

}

 

You awesome Project is all set now.

 

 

 

 

"We transform your idea into reality, reach out to us to discuss it.

or wanna join our cool team email us at [email protected] or see careers at startxlabs"

subscribe to startxlabs

startxlabs