由于最近上有类似需求 所以这两天研究了下。
一、准备工作
unity导出的xcode项目
二、开始倒腾
1、将Unity3D中的以下文件导入到工程目录下
Data
Classes
MapFileParser.sh
Libraries
MapFileParser执行文件
注意:Data文件导入类型为Creat folde references Classes MapFileParser.sh Libraries MapFileParser执行文件 通过Creat groups导入
导入后如下图所示
2、删除引用
- 删除Libraries->libil2cpp的引用 选项为
Remove Refernces
- target -> Build Phases -> DynamicLibEngineAPI 移除。如果不移除 会报错Undefined symbols for architecture arm64: ...in ... from:DynamicLibEngineAPI.o
3、设置classes->prefix.pch添加pch路径
4、main.m操作
将classes中main.mm 中的代码复制到项目的main.m中 并把后缀也改为mm 并将
UIApplicationMain(argc, argv, nil, [NSString stringWithUTF8String: AppControllerClassName]);
修改为
UIApplicationMain(argc, argv, nil, [NSString stringWithUTF8String: @"AppDelegate"]);
5 修改AppDelegate
AppDelegate.h
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) UIWindow *unityWindow;
@property (strong, nonatomic) UnityAppController *unityController;
-(void)StartUnity; //开启unity
- (void)hideUnityWindow; //暂停
AppDelegate.m
#import "AppDelegate.h"
#import "SRViewController.h"
#import "UnityAppController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
-(UIWindow *)unityWindow{
UIWindow *window = UnityGetMainWindow();
return window;
}
-(void)showUnityWindow{
UnityPause(false);
}
-(void)hideUnityWindow{
UnityPause(true);
}
-(void)StartUnity {
[_unityController applicationDidBecomeActive:[UIApplication sharedApplication]];
[self showUnityWindow];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.unityController = [[UnityAppController alloc] init];
[_unityController application:application didFinishLaunchingWithOptions:launchOptions];
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.window.backgroundColor = [UIColor redColor];
SRViewController *vc = [[SRViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
self.window.rootViewController = nav;
[self.window makeKeyAndVisible];
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application {
[_unityController applicationWillResignActive:application];
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
[_unityController applicationDidEnterBackground:application];
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
[_unityController applicationWillEnterForeground:application];
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
[_unityController applicationDidBecomeActive:application];
}
- (void)applicationWillTerminate:(UIApplication *)application {
[_unityController applicationWillTerminate:application];
}
6 UnityAppController.h
将
inline UnityAppController* GetAppController()
{
return _UnityAppController;
}
修改为
#import "AppDelegate.h"
inline UnityAppController* GetAppController()
{
AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
return delegate.unityController;
}
7 调用
@implementation SRViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.navigationItem.title = @"123";
self.view.backgroundColor = [UIColor greenColor];
UIButton * btn = [[UIButton alloc] initWithFrame:CGRectMake(50, 400, 100, 50)];
[btn addTarget:self action:@selector(test:) forControlEvents:UIControlEventTouchUpInside];
[btn setTitle:@"开始" forState:UIControlStateNormal];
btn.tag = 1;
[self.view addSubview:btn];
UIButton * btn2 = [[UIButton alloc] initWithFrame:CGRectMake(50, 450, 100, 50)];
[btn2 addTarget:self action:@selector(test:) forControlEvents:UIControlEventTouchUpInside];
btn2.tag = 2;
[btn2 setTitle:@"结束" forState:UIControlStateNormal];
[self.view addSubview:btn2];
}
- (void)test:(UIButton *) btn{
AppDelegate *delegate = [UIApplication sharedApplication].delegate;
delegate.unityWindow.frame = CGRectMake(0, 0, 100, 200);
if (btn.tag == 1) {
[self.view addSubview:delegate.unityWindow];
delegate.unityWindow.hidden = NO;
[delegate StartMyUnity];
}else {
[delegate hideUnityWindow];
delegate.unityWindow.hidden = YES;
}
}
三、添加Framework以及Run Script
添加这两项的时候注意和unity的工程中保持一致
- Frameworkork 注意事项: 注意Status是Required还是Optional
- libiconv.2.dylib 通过 Add Other -> command+shift+G 搜索/usr/lib 找到
libiconv.2.dylib
添加
四、其他重要设置
- Enable BitCode = NO;
- Header/Library Search Paths 和unity保持一致
- Framework Search Paths 和unity保持一致
- Other linker Flags 和unity保持一致
- all_load 如果项目中有这个 记得删除 和unity不兼容
- Supported Platforms 根据unity导出项目保持一致 最好真机
- Other C Flags、Other C++ Flags 和untiy保持一致
- C Language Dialect 保持一致
- 添加User-Defined (target->Build Settings -> Leveles右侧的加号)
- GCC_THUMB_SUPPORT = NO
- PROVISIONING_PROFILE
- UNITY_RUNTIME_VERSION = unity版本号
- UNITY_SCRIPTING_BACKEND = il2cpp