版本說明:Xcode版本7.3.1 Unity版本5.3.5f1
Xcode_Unity_Version
最近發現做AR、VR在遷移項目時候遇到開啟攝像頭黑屏的問題,后來我在研究過程中發現,從UIApplicationMain啟動Unity導出的UnityAppController是不會在開啟攝像頭時出現黑屏,又因為如果把Unity集成進公司項目的話,Unity其實是不會關閉的,只是主Window的顯示問題,因此可以用兩個獨立的Window通過設置hidden屬性解決這個問題。。。。。下面給出集成步驟->
1. 拷貝Unity導出Xcode項目中Classes
、Data
、Libraries
、MapFileParser
、MapFileParser.sh
文件或文件夾到想要集成的項目中
注意:拷貝到對應項目的根目錄中(跟.xcodeproj
同級)
拷貝文件
2. 把其中的四個文件夾拖到項目中
注意:Data
要選擇Create folder references,另外在/Data/Raw/QCAR
路徑下的QCAR
文件夾也要選擇Create folder references,Classes
和Libraries
選擇Create groups
`Data`
`Classes` and `Libraries`
目錄結構
PS:不要破壞文件路徑,Unity和FrameWorks文件夾是為了方便查看目錄創建的非實體文件夾(New Group)
3. 提前編譯
Build Phases
-
添加Run Script:
"$PROJECT_DIR/MapFileParser.sh"
Run Script - 添加Link Binary With Libraries:
AudioToolbox.framework
AVFoundation.framework
CFNetwork.framework
CoreText.framework
CoreGraphics.framework
Corelocation.framework
CoreMedia.framework
CoreMotion.framework
CoreVideo.framework
OpenAL.framework
OpenGLES.framework
iAd.framework
MediaPlayer.framework
QuartzCore.framework
SystemConfiguration.framework
libiconv.2.tbd
Link Binary With Libraries
4. 編譯設置
Build Setting
-
添加Search Paths
-
**Header Search Paths **
"$(SRCROOT)/Classes"
"$(SRCROOT)"
$(SRCROOT)/Classes/Native
$(SRCROOT)/Libraries/bdwgc/include
$(SRCROOT)/Libraries/libil2cpp/include
Header Search Paths -
Library Search Paths
"$(SRCROOT)"
"$(SRCROOT)/Libraries"
Library Search Paths
-
-
PCH文件路徑:
Classes/Prefix.pch
Prefix Header -
Other C Flags:
-DINIT_SCRIPTING_BACKEND=1
Other C Flags - 修改Enable Bitcode為NO
Enable Bitcode
5. 文件修改
-
刪掉
main.m
Remove_main.m -
修改代理
UnityAppController.h
添加成員變量AppDelegate *wpDelegate
@class
unity_ShowOrHidden -
修改代理
UnityAppController.m
導入自己代理的頭文件#import "AppDelegate.h"
在- application: didFinishLaunchingWithOptions:方法中隱藏Unity的Window,顯示自己想要的Window。
didFinishLaunchingWithOptions
跳轉邏輯
unity_ShowOrHidden
//添加返回按鈕
CGFloat height = 50;
CGFloat width = 100;
UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height - height, width, height)];
[btn setBackgroundColor:[UIColor redColor]];
[btn setTitle:@"返回" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(unityHidden) forControlEvents:UIControlEventTouchUpInside];
[self.unityView addSubview:btn];
self.wpDelegate = [[AppDelegate alloc] init];
[self.wpDelegate application:application didFinishLaunchingWithOptions:launchOptions];
[self unityHidden];
//實現方法
- (void)unityShow{
self.wpDelegate.window.hidden = YES;
[self.window makeKeyAndVisible];
}
- (void)unityHidden{
self.window.hidden = YES;
[self.wpDelegate.window makeKeyAndVisible];
}
- 修改代理
AppDelegate.m
創建wpDelegate的Window,想要什么樣的RootViewController自己定義就好,或者繼續用main.storyboard。。。。開心就好。。。
AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
RedVC *red = [[RedVC alloc] init];
BlueVC *blue = [[BlueVC alloc] init];
UINavigationController *navRed = [[UINavigationController alloc] initWithRootViewController:red];
UINavigationController *navBlue = [[UINavigationController alloc] initWithRootViewController:blue];
UITabBarController *tab = [[UITabBarController alloc] init];
[tab addChildViewController:navRed];
[tab addChildViewController:navBlue];
self.window.rootViewController = tab;
return YES;
}
6. 補充說明:
- 顯示和隱藏Unity可以直接調用UnityAppController中unityShow和unityHidden
eg:在紅色VC中監聽屏幕點擊跳轉unity
- 存在一些性能問題,unity不能完全關閉,顯示unity時我明顯感覺到延遲
- 每次unity有改動的話 都需要重新集成一次。。。。麻煩。。。
- 底層一些原理鄙人尚不太清楚,希望有大神可以指導一下,不勝感激。。。臨書涕零。。。。本文如有不當之處也歡迎拍磚。。。咳咳。。謝謝。。。