1. Download this project
2. Get xCode project exported from Unity
Since iOS project exported from Unity contains large files it can't be uploaded to github. You can download
xCode project exported from Unity from here, or get Unity project from github
and make export yourself.
3. Setup Xcode workspace
Xcode workspace allows to work on multiple projects simultaneously and combine their products
- open NativeiOSApp.xcodeproj from Xcode
- create workspace and save it at UaaLExample/both.xcworkspace. (File / New / Workspace)
- close NativeiOSApp.xcodeproj project all Next steps are done from just created Workspace project
- add NativeiOSApp.xcodeproj and generated Unity-iPhone.xcodeproj from step #2 to workspace on a same level ( File / Add Files to βbothβ )
4. Add UnityFramework.framework
With this step we add Unity player in the form of a framework to NativeiOSApp, it does not change the behavior of NativeiOSApp yet
- select NativeiOSApp target from NativeiOSApp project
- in "General / Frameworks, Libraries, and Embedded Content" press +
- select Unity-iPhone/UnityFramework.framework
- in "Build Phases" tab, expand "Link Binary With Libraries"
- remove UnityFramework.framework from the list (select it and press - )
5. Expose NativeCallProxy.h
Native application implements NativeCallsProtocol defined in following file:
- find and select Unity-iPhone / Libraries / Plugins / iOS / NativeCallProxy.h
- enable UnityFramework in Target Membership and set Public header visibility (small dropdown on right side to UnityFramework)
6. Make Data folder to be part of the UnityFramework
By default Data folder is part of Unity-iPhone target, we change that to make everything encapsulated in one single framework file.
- change Target Membership for Data folder to UnityFramework
- (optional) If you want to use Unity-iPhone sheme you need to point UnityFramework to a new place where Data is located by calling from Unity-iPhone/MainApp/main.mm:
[ufw setDataBundleId: "com.unity3d.framework"]; // On Demand Resources are not supported in this case. To make them work instead of the calls above // you need to copy Data folder to your native application (With script at Build Phases) and // skip a calls above since by default Data folder expected to be in mainBundle.
Everything is ready to build, run and debug for both projects: Unity-iPhone and NativeiOSApp (select NativeiOSApp scheme to run Native App with integrated Unity or Unity-iPhone to run just Unity App part) If all went successfully at this point you should be able to run NativeiOSApp:
Loading Unity player is controlled with UnityFramework object. To get it you call UnityFrameworkLoad (it loads UnityFramework.framework if it wasn't, and returns singleton instance to UnityFramework class observe Unity-iPhone/UnityFramework/UnityFramework.h for its API ). Observe UnityFrameworkLoad in: NativeiOSApp/NativeiOSApp/MainViewController.mm or in Unity-iPhone/MainApp/main.mm
#include <UnityFramework/UnityFramework.h>
UnityFramework* UnityFrameworkLoad()
{
NSString* bundlePath = nil;
bundlePath = [[NSBundle mainBundle] bundlePath];
bundlePath = [bundlePath stringByAppendingString: @"/Frameworks/UnityFramework.framework"];
NSBundle* bundle = [NSBundle bundleWithPath: bundlePath];
if ([bundle isLoaded] == false) [bundle load];
UnityFramework* ufw = [bundle.principalClass getInstance];
if (![ufw appController])
{
// Initialize Unity for a first time
[ufw setExecuteHeader: &_mh_execute_header];
// Keep in sync with Data folder Target Membership setting
[ufw setDataBundleId: "com.unity3d.framework"];
}
return ufw;
}