原生iOS嵌入Unity匯出的Xcode工程
阿新 • • 發佈:2019-12-25
由於最近上有類似需求 所以這兩天研究了下。
一、準備工作
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,[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,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,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