集成步骤
所有的配置参考Unity导出来的工程的配置
1. iOS工程中新建Vendor group
2. 添加Unity的文件至iOS工程中
将Unity工程下的3个文件夹Classes、Libraries、Data添加至iOS工程中,添加时注意选择Copy items if needed选项,Classes和Libraries文件夹选择Create groups, 而Data文件夹选择Create folder references选项。
添加完毕以后可以把Unity文件夹中的一些无关文件清理掉,它们会影响编译速度,删除时选择Remove Reference:
Classes->Native文件夹下的*.h文件
Libraries文件夹下的libil2cpp文件夹(可选)
3. 添加相关框架
在iOS工程中新建Frameworks group, 将需要添加的framework放在这个group下,至于需要添加哪些framework可以参见Unity工程的TARGETS -> General -> Linked Frameworks and Libraries,如图所示:
4. 配置Build Settings相关选项
(1) Enable Bitcode -> NO
(2) Other Linker Flags
添加-weak_framework CoreMotion -weak-lSystem
(3) Header Search Paths
添加以下选项:
"$(SRCROOT)"
"$(SRCROOT)/Unity5Native/Classes"
$(SRCROOT)/Unity5Native/Classes/Native
$(SRCROOT)/Unity5Native/Libraries/libil2cpp/include
$(SRCROOT)/Unity5Native/Libraries
注意:根据工程实际路径来配置,$(SRCROOT)是指.xcproj文件所在的路径,Classes和Libraries文件夹是在与Unity5Native.xcproj文件同级的Unity5Native文件夹下面的,所以SRCROOT下面要再添加Unity5Native层级。如果此处路径配置不正确,可能会出现诸如il2cpp-config.h file not found的编译错误。
(4)Library Search Paths
添加以下选项:
"$(SRCROOT)"
"$(SRCROOT)/Unity5Native/Libraries"
(5)Custom Compiler Flags -> Other C Flags
添加-DINIT_SCRIPTING_BACKEND=1
(6) C Language Dialect选择C99 [-std=c99]
(7) 新建pch文件:PrefixHeader.pch
(8) 设置Prefix Header的路径
根据pct文件所在路径实际设置,由于我是放在Supporting Filegroup组下面的,实际在与Unity5Native.xcproj文件同目录下的Unity5Native文件夹下。
(9)Precompile Prefix Header选择YES
(10) C++ Language Dialect选择C++11 [-std=c++11]
(11)Enable C++ Runtime Types选择NO
(12) Warnings-Objective C->Overriding Deprecated Objective-C Methods选择YES
(13) Warnings-Objective C->Unintentional Root Classes选择YES
(14) User-Defined
添加如下选项:
GCC_THUMB_SUPPORT->NO
GCC_USE_INDIRECT_FUNCTION_CALLS->NO
UNITY_RUNTIME_VERSION->5.5.2f1
UNITY_SCRIPTING_BACKEND->il2cpp
5. pch文件内容替换
将Classes文件夹下的Prefix.pch文件中的内容全部复制粘贴到PrefixHeader.pch文件的
之间,并且引入UnityAppController.h文件如图所示:
6. Supporting Files下面的main.m
重命名成main.mm
将Classes文件夹下的main.mm文件中的全部内容复制到Supporting Files文件夹下的main.mm文件中,并进行修改:
//const char* AppControllerClassName = @”UnityAppController”;const char* AppControllerClassName = “AppDelegate”;
Build Phases中移除Classes下的main.mm
7. UnityAppController.h
作如下修改:
//inline UnityAppController GetAppController()
//{
// return (UnityAppController)[UIApplication sharedApplication].delegate;
//}
#import "AppDelegate.h"
inline UnityAppController* GetAppController()
{
AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
return delegate.unityController;
}
8. AppDelegate.h和AppDelegate.m
AppDelegate.h文件作如下修改:
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) UIWindow *unityWindow;
@property (nonatomic, strong) UnityAppController *unityController;
- (void)showUnityWindow;
- (void)hideUnityWindow;
@end
AppDelegate.m文件作如下修改:
#import “AppDelegate.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (UIWindow *)unityWindow {
return UnityGetMainWindow();
}
- (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.backgroundColor = [UIColor redColor];
self.unityController = [[UnityAppController alloc] init];
[self.unityController application:application didFinishLaunchingWithOptions:launchOptions];
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.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 inactive 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. ViewController.m
作如下修改:
#import “ViewController.h"
#import “AppDelegate.h"
#import “UnityView.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.cent er;
[self.showUnityButton addTarget:self action:@selector(showUnityView:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.showUnityButton];
}
- (void)showUnityView:(id)sender {
[(AppDelegate *)[UIApplication sharedApplication].delegate showUnityWindow];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(100, 200, 200, 50);
[btn setTitle:@"Exit" forState:UIControlStateNormal];
[btn setTintColor:[UIColor whiteColor]];
[btn setBackgroundColor:[UIColor blueColor]];
//[delegate.unityController.window addSubview:btn];
[delegate.unityController.unityView addSubview:btn];
[btn addTarget:self action:@selector(exitAR) forControlEvents:UIControlEventTouchUpInside];
} - (void)exitAR
{
//调用Unity方法
// void UnitySendMessage(constchar* obj, constchar* method, constchar* msg);
UnitySendMessage("Manager", "End", "");
AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
[delegate hideUnityWindow];
}
@end