2016-12-17/Unity5.5.0f3/Xcode_8.1(8B62)/MacBook/OS X 10.11.6
參考地址:
https://the-nerd.be/2015/11/13/integrate-unity-5-in-a-native-ios-app-with-xcode-7/
http://www.jianshu.com/p/715adc38e451 最近接到不少單子的需求都是要求把AR模組嵌入原有APP,而不開發新的APP,那不管是高通的Vuforia還是視+的EasyAR,若要實現模型動畫,要麼自己用OpenGLES寫渲染,要麼直接用Unity,顯然,用OpenGLES從頭寫渲染的工程量很大,而嵌入Unity的工作量則小得多,且渲染效果也要更好,而OpenGLES方法的優勢可能也就是比較穩定這一點了。這篇部落客要是依照老外的視頻做的,就是把視頻內容文字化而已,和簡書上那位大大寫得差不多。推薦直接看視頻,手把手教。
那麼,正式開始: Unity匯出iOS工程,為方便,稱匯出的iOS工程為iUnity工程。匯出時PlayerSetting設定要注意的部分主要是Scripting Backend為IL2CPP,至於Graphics API基本不用理會,如為保險起見,可按以前的教程取消勾選,刪除Metal只保留OpenGL ES2,但我此次並未作此修改。
此時,準備工作結束,到XCode中建立一個Single View(不用引用UI和Unit包) 或者開啟已有工程,為方便,稱原生工程為iNative工程。
將iUnity工程中的Classes和Libraries引入到iNative工程中,選擇Create Groups。
3.1將iUnity工程中的Data引入到iNative中,選擇Create folder reference。
為了安全和編譯速度,可以刪除iNative中Classes/Native下的所有.h檔案。
4.1同樣,也可以刪除iNative中Libraries/libil2cpp檔案夾。
在iNative中添加pct檔案,此次命名為PrefixHeader.pch。
5.1將Classes中Prefix.pch檔案的內容複寫到Prefixheader.pch中(不是完全覆蓋)。
5.2在PrefixHeader.pch中添加“#import “UnityAppController.h” ”。
Classes中的main.mm中的內容全部拷貝到Supporting Files中的main.m中(全部替換,如是已有工程,手動修改)。
6.1修改main.m為main.mm(.mm尾碼才能編譯c,oc,c++),到build phase的compile sources中移除Classes路徑的main.mm編譯。
引用Framework(注意Optional的項)
配置Build Setting,若教程失效時可以開啟自己匯出的iUnity工程,這一步不過是要將iNative的Build Setting設定為iUnity的Build Setting。
還可參考簡書大大的那篇配圖。
8.1 Header Search Paths
8.2 Library Search Paths
8.3 C++ Language Dialect :C++11,Enable C++ Runtime Types No
8.4 Precompile Prefix Header :Yes ,Prefix Header :iNative/PrefixHeader.pch(根據自己的路徑調節)。
8.5 C Language Dialect : C99
8.6 Other C Flags : -DINIT_SCRIPTING_BACKEND=1
8.7 User-Defined:
GCC_THUMB_SUPPORT : NO
GCC_USE_INDIRECT_FUNCTION_CALLS : NO
UNITY_RUNTIME_VERSION : 5.5.0f3
UNITY_SCRIPTING_BACKEND : il2cpp
8.8 Other Linker Flags: -weak_framework CoreMotion -weak-lSystem 代碼修改部分:
9.1UnityAppController.h(位於Classes中)
9.2Appdelegate.h
9.3Appdelegate.m
//// AppDelegate.m// iOSNativeUnity//// Created by Develop on 2016/12/16.// Copyright © 2016年 Develop. All rights reserved.//#import "AppDelegate.h"@interface AppDelegate ()@property (nonatomic ,strong) UINavigationController *navVC;@end@implementation AppDelegate-(UIWindow *) unityWindow{ return UnityGetMainWindow();}//Add by PGW-(void) showUnityWindow{ [self.unityWindow makeKeyAndVisible];}-(void) hideUnityWindow{ [self.window makeKeyAndVisible];}- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; self.window.backgroundColor = [UIColor redColor]; ViewController *viewcontroller = [[ViewController alloc] initWithNibName:nil bundle:nil]; self.navVC = [[UINavigationController alloc] initWithRootViewController:viewcontroller]; self.window.rootViewController = self.navVC; self.unityController = [[UnityAppController alloc] init]; [self.unityController application:application didFinishLaunchingWithOptions:launchOptions]; [self.window makeKeyAndVisible]; return YES;}- (void)applicationWillResignActive:(UIApplication *)application { // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game. [self.unityController applicationWillResignActive:application];}- (void)applicationDidEnterBackground:(UIApplication *)application { // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. [self.unityController applicationDidEnterBackground:application];}- (void)applicationWillEnterForeground:(UIApplication *)application { // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. [self.unityController applicationWillEnterForeground:application];}- (void)applicationDidBecomeActive:(UIApplication *)application { // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. [self.unityController applicationDidBecomeActive:application];}- (void)applicationWillTerminate:(UIApplication *)application { // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. [self.unityController applicationWillTerminate:application];}@end
9.4ViewController.m
//// ViewController.m// iOSNativeUnity//// Created by Develop on 2016/12/16.// Copyright © 2016年 Develop. All rights reserved.//#import "ViewController.h"@interface ViewController ()@property (nonatomic, strong) UIButton *showUnityButton;@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.view.backgroundColor = [UIColor blueColor]; self.showUnityButton = [UIButton buttonWithType:UIButtonTypeSystem]; [self.showUnityButton setTitle:@"Show Unity" forState:UIControlStateNormal]; self.showUnityButton.frame = CGRectMake(0, 0, 100, 44); self.showUnityButton.center = self.view.center; [self.view addSubview:self.showUnityButton]; [self.showUnityButton addTarget:self action:@selector(showunitybuttonTouched:) forControlEvents:UIControlEventTouchUpInside];}-(void)showunitybuttonTouched:(UIButton *)sender{ NSLog(@"[Viewcontroller Button Touch]"); [(AppDelegate *)[UIApplication sharedApplication].delegate showUnityWindow];}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}@end 最後:
10.1一定要用真機調試,沒有真機的話不管是build還是怎麼樣,一定會報類似“referenced from”的錯誤。
10.2 懶人樣本:連結: http://pan.baidu.com/s/1gfoidJP 密碼: smjp