一直想抽空整理一下unity原生工程導入iOS原生工程中的詳細步驟。做iOS+vuforia+unity開發這么長時間了。從最初的小小白到現在的小白。中間趟過了好多的坑。也有一些的小小收貨。做一個喜歡總結的人。好了廢話不多說了。上干貨。本人用的是unity 5.4.0f3 + Xcode 7.2。個人認為這個版本的影響不是很大的。以下的導入資源過程大家根據實際需要去選擇是否拷貝資源到原生工程中。(copy if need)
一、unity導出工程一開始是這樣子的:
二、unity導入原生工程的准備工作。
1.將一些必要的文件導入iOS Native工程中去
1>將Classes 和 Libraries 文件夾以 下面的方式導入自己的工程中
2>將Data 文件夾以下面的方式導入工程中
3>刪除Native文件夾下的所有.h文件的引用。並將cpp文件導入工程引入。
清除native中所有的.h引用。為了更快的刪除定位到這些.h文件。首先選定Native文件夾
然后進行這樣的操作,注意一定要選擇Remove Reference。千萬不要選擇Move to Trash(下面截圖只是為了演示一下刪除框。類名忽略不要對號刪除,並不是Native中的頭文件)然后再將Native中的cpp文件拖入到工程。勾選1、2、4方框。
4>.然后對應刪除Libraries-->libil2cpp文件夾。刪除方法同Native中的.h文件的移除。
三、添加引用庫文件
其中因為我用了pod的緣故。libPods庫可以忽略。
四、關閉bitcode
在build settings中bitcode關閉。
五、在 other Linker Flags 添加
-weak_framework CoreMotion -weak-lSystem
六、在Header Search Path 添加下面這些頭文件引用~
SRCROOT/../../iOS/unity2iOSSRCROOT/../../iOS/unity2iOS{SRCROOT}/../../iOS/unity2iOS/Classes
SRCROOT/../../iOS/unity2iOS/Classes/NativeSRCROOT/../../iOS/unity2iOS/Classes/Native{SRCROOT}/../../iOS/unity2iOS/Libraries
SRCROOT/../../iOS/unity2iOS/Libraries/libil2cpp/includeSRCROOT/../../iOS/unity2iOS/Libraries/libil2cpp/include{SRCROOT}/../../iOS/unity2iOS/Libraries/Plugins/iOS
七、在Library Search Path 中添加
SRCROOT/../../iOS/unity2iOSSRCROOT/../../iOS/unity2iOS{SRCROOT}/../../iOS/unity2iOS/Libraries
${SRCROOT}/../../iOS/unity2iOS/Libraries/Plugins/iOS
八、在other C Flags 中添加 -DINIT_SCRIPTING_BACKEND=1 同是在 other C++ Flags中出現
改為C99
改PCH(將兩個pch文件合並)
跟着下面的圖片做設置更改
在user-Defined 添加如下
GCC_THUMB_SUPPORT NO
GCC_USE_INDIRECT_FUNCTION_CALLS NO
UNITY_RUNTIME_VERSION 5.4.0f3
UNITY_SCRIPTING_BACKEND il2cpp
以上都要根據自己工程的具體情況添加。大家切記。
九、添加配置腳本RUN SCRIPT(注意按照自己工程中的腳本位置配置)
十、更改main.m文件為main.mm文件。將Classes中的main.mm中的內容合並到原來工程的main.mm工程中。然后刪除Classes中的main文件。
// // main.m //// // Created by Aaron on 16/11/17. // Copyright © 2016年 Aaron. All rights reserved. // #import <UIKit/UIKit.h> #import "AppDelegate.h" #include "RegisterMonoModules.h" #include "RegisterFeatures.h" #include <csignal> // Hack to work around iOS SDK 4.3 linker problem // we need at least one __TEXT, __const section entry in main application .o files // to get this section emitted at right time and so avoid LC_ENCRYPTION_INFO size miscalculation static const int constsection = 0; void UnityInitTrampoline(); // WARNING: this MUST be c decl (NSString ctor will be called after +load, so we cant really change its value) /** * VuforiaNativeRendererController */ const char* AppControllerClassName = "AppDelegate"; int main(int argc, char* argv[]) { @autoreleasepool { UnityInitTrampoline(); UnityParseCommandLine(argc, argv); RegisterMonoModules(); NSLog(@"-> registered mono modules %p\n", &constsection); RegisterFeatures(); std::signal(SIGPIPE, SIG_IGN); NSString *ss = NSStringFromClass([AppDelegate class]); UIApplicationMain(argc, argv, nil,ss); } return 0; } #if TARGET_IPHONE_SIMULATOR && TARGET_TVOS_SIMULATOR #include <pthread.h> extern "C" int pthread_cond_init$UNIX2003(pthread_cond_t *cond, const pthread_condattr_t *attr) { return pthread_cond_init(cond, attr); } extern "C" int pthread_cond_destroy$UNIX2003(pthread_cond_t *cond) { return pthread_cond_destroy(cond); } extern "C" int pthread_cond_wait$UNIX2003(pthread_cond_t *cond, pthread_mutex_t *mutex) { return pthread_cond_wait(cond, mutex); } extern "C" int pthread_cond_timedwait$UNIX2003(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime) { return pthread_cond_timedwait(cond, mutex, abstime); } #endif // TARGET_IPHONE_SIMULATOR && TARGET_TVOS_SIMULATOR
十一、在pch文件中添加 #import "UnityAppController.h"。然后更改AppDelegate
// // AppDelegate.h // // // Created by Aaron on 16/11/17. // Copyright © 2016年 Aaron. All rights reserved. // #import <UIKit/UIKit.h> @class UnityAppController; @interface AppDelegate : UIResponder <UIApplicationDelegate> @property (strong, nonatomic) UIWindow *window; @property (nonatomic , strong)UIWindow *unityWindow; @property (nonatomic , strong)UnityAppController *unityVC; - (void)showUnityWindow; - (void)hideUnityWindow; - (void)shouldAttachRenderDelegate; @end
在AppDelegate.mm(因為有混編,本來變一個main.mm文件就可以了。這里也變過來雙保險)
首先添加:
// // AppDelegate.m // // // Created by Aaron on 16/11/17. // Copyright © 2016年 Aaron. All rights reserved. // #import "AppDelegate.h" #import "HomeViewController.h" #import "UnityAppController.h" #import "VuforiaRenderDelegate.h" extern "C" void VuforiaSetGraphicsDevice(void* device, int deviceType, int eventType); extern "C" void VuforiaRenderEvent(int marker); @interface AppDelegate () { UIButton *_backBtn; } @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. self.window.backgroundColor = [UIColor whiteColor]; [application setStatusBarStyle:UIStatusBarStyleLightContent]; //unity self.unityVC = [[UnityAppController alloc] init]; [self.unityVC application:application didFinishLaunchingWithOptions:launchOptions]; HomeViewController *homeVC = [[HomeViewController alloc] init]; BaseNavViewController *mainNav = [[BaseNavViewController alloc] initWithRootViewController:homeVC]; self.window.rootViewController = mainNav; [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 throttle down OpenGL ES frame rates. Games should use this method to pause the game. [self.unityVC 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.unityVC applicationDidEnterBackground:application]; } - (void)applicationWillEnterForeground:(UIApplication *)application { // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. [self.unityVC applicationWillEnterForeground:application]; } - (void)applicationDidBecomeActive:(UIApplication *)application { [self.unityVC applicationDidBecomeActive:application]; } - (void)applicationWillTerminate:(UIApplication *)application { // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. [self.unityVC applicationWillTerminate:application]; } #pragma mark - #pragma mark ---------------unity開啟與隱藏 - (UIWindow *)unityWindow { if (!_unityWindow) { _unityWindow = UnityGetMainWindow(); } return _unityWindow; } - (void)showUnityWindow { [self.unityWindow makeKeyAndVisible]; _backBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [_backBtn setImage:[UIImage imageNamed:@"返回icon"] forState:UIControlStateNormal]; [self.unityWindow addSubview:_backBtn]; [_backBtn addTarget:self action:@selector(hideUnityWindow) forControlEvents:UIControlEventTouchUpInside]; [_backBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(_unityWindow).offset(30); make.left.equalTo(_unityWindow).offset(15); make.width.mas_equalTo(50); make.height.mas_equalTo(50); }]; } - (void)hideUnityWindow { [self.window makeKeyAndVisible]; } - (void)shouldAttachRenderDelegate { UnityRegisterRenderingPlugin(&VuforiaSetGraphicsDevice, &VuforiaRenderEvent); } @end
十二、在unityappcontroller中需要做的配置就是,首先在unityAppController.h中更改GetAppController()方法(當然要先引入#import "AppDelegate.h")
NS_INLINE UnityAppController* GetAppController() { AppDelegate *dele = (AppDelegate *)[UIApplication sharedApplication].delegate; return (UnityAppController *)dele.unityVC; }
在.mm文件中重寫shouldAttachRenderDelegate
方法
- (void)shouldAttachRenderDelegate { AppDelegate *deleg = [UIApplication sharedApplication].delegate; [deleg shouldAttachRenderDelegate]; }
至此,導入工作已經做完了。愉快地去玩耍吧。