iOS原生集成Unity相互跳轉流程
PS:使用2018.3.5f1版本進行生產Unity工程献联,Xcode版本10.1
Unity工程生成設置
- 打開Unity創(chuàng)建一個工程劝篷,然后點擊上方的File->BuildSetting,在彈出的設置框中巴粪,選擇iOS然后點擊player Setting進行設置
-
設置如圖(需要注意這邊的Bundle Identifier與原生工程應該保持一致):Unity Setting
- 設置完畢之后選擇Build進行導出通今,導出名稱和位置放在自己方便拷貝和運行的地方
- 使用Xcode運行Unity工程粥谬,確保導出的工程沒有問題
原生工程集成Unity
PS:(如有用刪除操作全部使用Remove Reference)
- 首先新建一個工程項目
- 設置新項目的<strong>Bundle Identifier</strong>,跟之前的Unity工程一樣
- 在我們從Unity里面導出來ios工程中找到Libraries辫塌、Classes帝嗡、MapFileParse.sh和Data拖進工程,此時要注意 Libraries璃氢、MapFileParse.sh和Classes過程中Copy items if needed -->不選 Create groups--> 選 哟玷; 在<strong>Data</strong>時 Copy items if needed --> 不選 ,Create folder references -->選一也。(PS:比較舊版的Unity可能在Classes中生成很多".h"文件巢寡,需要進行刪除操作,但是最新的Unity版本沒有生成這些文件椰苟,沒有比較進行操作)
-
如果原生工程沒有.pch文件抑月,可以直接在Build Settings -> Precomplies Prxfix Header --> YES, 并且Prefix Header直接指向Classes的Prefix.pch文件;如果原來工程存在.pch文件舆蝴,則復制Prefix.pch的內容到原工程的.pch中谦絮,刪除Prefix.pch。在關聯的.pch文件中導入Unity控制器文件導入文件
- Enable Bitcode --> YES, Enable Testability --> NO
- Other Linker Flags設置4個屬性 $(inherited) -weak_framework CoreMotion -weak-ISystem
-
Header Search Paths和Library Search Paths:頭文件包
- Other C Flags設置為:$(inherited) -DINIT_SCRIPTING_BACKEND=1 -fno-strict-overflow -DNET_4_0 -DRUNTIME_IL2CPP=1
- C Language Dialect --> GNU99[-std=gnu99] (最新版本的才設置為這個值)
- C++ Language Dialect --> C++11[-std=c++11]
- Enable C++ Runtime Types --> NO
- Overriding Deprecated Objective-C Methods --> YES
- Unintentional Root Class --> YES
-
添加User-defined設置洁仗,需要注意版本設置需要用你導出Unity工程的版本层皱,我的為最新2018.3.5f1版本:User-defined
-
導入使用到的庫文件:庫文件
- 修改<strong>Main.m</strong>為<strong>Main.mm</strong>,將Classes里面的Main.mm文件內容復制到你修改的文件里面,并且刪除Classes的Main.mm
到這個步驟基本的工程導入已經完成赠潦,接下來要增加和修改的就是跳轉的代碼
原生和Unity互相跳轉
- 修改AppDelegate.h文件:
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (nonatomic,strong) UnityAppController *unityController;
@property (nonatomic,strong) UIWindow *unityWindow;
//在ViewController實現也可
-(void)showUnityWindow;
-(void)hideUnityWindow;
@end
新增Unity的控制器和控制窗口
- 修改AppDelegate.m文件:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
ViewController *viewController = [[ViewController alloc] init];
viewController.view.backgroundColor = [UIColor whiteColor];
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.window.rootViewController = viewController;
self.unityController = [[UnityAppController alloc] init];
[self.unityController application:application didFinishLaunchingWithOptions:launchOptions];
[self.window makeKeyAndVisible];
// Override point for customization after application launch.
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application {
[self.unityController applicationWillResignActive:application];
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
[self.unityController applicationDidEnterBackground:application];
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
[self.unityController applicationWillEnterForeground:application];
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
[self.unityController applicationDidBecomeActive:application];
}
- (void)applicationWillTerminate:(UIApplication *)application {
[self.unityController applicationWillTerminate:application];
}
-(UIWindow *)unityWindow {
return UnityGetMainWindow();
}
-(void)showUnityWindow {
[self.unityWindow makeKeyAndVisible];
}
-(void)hideUnityWindow {
[self.window makeKeyAndVisible];
}
- 修改Main.mm文件:在文件中找到const char AppControllerClassName = "UnityAppController"* 和 UIApplicationMain(argc, argv, nil, [NSString stringWithUTF8String: AppControllerClassName]) 修改為
//const char* AppControllerClassName = "UnityAppController";
const char* AppControllerClassName = "AppDelegate";
//UIApplicationMain(argc, argv, nil, [NSString stringWithUTF8String: AppControllerClassName]);
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
- 修改UnityAppController.h
#import "AppDelegate.h"
extern UnityAppController* _UnityAppController;
inline UnityAppController* GetAppController()
{
AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
return delegate.unityController;
// return _UnityAppController;
}
- 修改ViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
self.showUnityButton = [UIButton buttonWithType:UIButtonTypeSystem];
self.showUnityButton.frame = CGRectMake(0, 0, 100, 50);
self.showUnityButton.tintColor = [UIColor blueColor];
[self.showUnityButton setTitle:@"Go To Unity" forState:UIControlStateNormal];
self.showUnityButton.center = self.view.center;
[self.showUnityButton addTarget:self action:@selector(showUnityView:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.showUnityButton];
}
-(void)showUnityView:(id)sender {
AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(0, 0, 44, 44);
button.backgroundColor = [UIColor redColor];
button.center = delegate.unityWindow.center;
[button setTitle:@"返回" forState:UIControlStateNormal];
[button addTarget:self action:@selector(hideUnityWindow) forControlEvents:UIControlEventTouchUpInside];
[delegate.unityWindow addSubview:button];
[delegate.unityWindow makeKeyAndVisible];
}
-(void)hideUnityWindow {
AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
[delegate.window makeKeyAndVisible];
}