做一个VRAR的app,用Vuforia识别出模型之后通过点击模型切换到GoogleCardboard 的界面,两边都有SDK,实现起来应该不难,单纯是两个SDK的跳接,然而却遇到黑屏的问题。
详情:
版本:Vuforia 5.5.9 UnitySDK; Cardboard 0.5.0 UnitySDK;Unity 5.3.5;
只要引用进Cardboard就会出现Black camera,但是却能识别出东西,就像这样:
1. 我猜想应该是cardboard的什么东西影响到了vuforia,但还没跳转到cardboard啊,只是识别而已。查看cardboard UnitySDK,发现CardboardAppController是继承自UnityAppController的,还重写了两个方法preStartUnity 和 CreateUnityView。
- (void)preStartUnity {
[super preStartUnity];
syncProfile();
}
- (UnityView *)createUnityView {
UnityView* unity_view = [super createUnityView];
createSettingsButton(self, (UIView *)unity_view);
return unity_view;
}
防止覆盖父类的方法,子类方法都有调用父类的方法,而且注释掉之后无法解决black camera。
2.查找Vuforia方面的问题,发现Vuforia有VRAR的demo:Integrating Cardboard to the AR/VR Sample
该demo实现的是用谷歌的cardboard双屏模式打开摄像头进行识别,识别完之后进去模型,并没有提到black camera的问题。跳过~
3.再查找Cardboard在github上面的地址,googlevr/gvr-unity-sdk,在issue里面找到一个同道中人,地址grr-unity-idk的issue,他解释说:
It was due to the way both Vuforia and Cardboard override the AppController. If you manually merge them together then it works correctly (Unity 5.3).
有道理,既然继承UnityAppController会有问题,那为何不直接写进去,尝试了把CardboardAppController的方法,写到UnityAppController里面,成功解决了。但此时还有疑问,为什么继承就不行,重写的两个方法都包含父类方法的调用。
仔细看了一下所有代码,发现还有一句很奇怪的东西:
#import "CardboardAppController.h"
extern "C" {
extern void readProfile();
extern void syncProfile();
extern void createSettingsButton(id app, UIView* view);
extern UIViewController* createSettingsDialog(id app);
extern UIViewController* createOnboardingDialog(id app);
bool isOpenGLAPI() {
#if UNITY_VERSION < 463
return true;
#else
CardboardAppController* app = (CardboardAppController *)GetAppController();
UnityRenderingAPI api = [app renderingAPI];
return api == apiOpenGLES2 || api == apiOpenGLES3;
#endif
}
void launchSettingsDialog() {
CardboardAppController* app = (CardboardAppController *)GetAppController();
[app launchSettingsDialog];
}
void launchOnboardingDialog() {
CardboardAppController* app = (CardboardAppController *)GetAppController();
[app startSettingsDialog:createOnboardingDialog(app)];
}
void endSettingsDialog() {
CardboardAppController* app = (CardboardAppController *)GetAppController();
[app stopSettingsDialog];
}
} // extern "C"
@implementation CardboardAppController
- (void)preStartUnity {
[super preStartUnity];
syncProfile();
}
- (UnityView *)createUnityView {
UnityView* unity_view = [super createUnityView];
createSettingsButton(self, (UIView *)unity_view);
return unity_view;
}
- (void)launchSettingsDialog {
[self startSettingsDialog:createSettingsDialog(self)];
}
- (void)startSettingsDialog:(UIViewController*)dialog {
[self pause:YES];
[self.rootViewController presentViewController:dialog animated:NO completion:nil];
}
- (void)stopSettingsDialog {
[[self rootViewController] dismissViewControllerAnimated:NO completion:nil];
[self pause:NO];
}
- (void)pause:(bool)paused {
#if UNITY_VERSION < 462
UnityPause(paused);
#else
self.paused = paused;
#endif
}
@end
IMPL_APP_CONTROLLER_SUBCLASS(CardboardAppController)
最后一句的IMPL_APP_CONTROLLER_SUBCLASS(CardboardAppController)是宏定义,在UnityAppController.h文件里可以找到
#define IMPL_APP_CONTROLLER_SUBCLASS(ClassName) \
@interface ClassName(OverrideAppDelegate) \
{ \
} \
+(void)load; \
@end \
@implementation ClassName(OverrideAppDelegate) \
+(void)load \
{ \
extern const char* AppControllerClassName; \
AppControllerClassName = #ClassName; \
} \
@end \
貌似是用分类的方法,帮子类写了一个load方法,然而在VuforiaNativeRendererController.mm也有一句这样的宏,VuforiaNativeRendererController.mm也是UnityAppController的子类。
我尝试把CardboardAppController里面的load方法宏注释掉,black camera也能解决了。
到底是不是这个宏的影响,还在研究中。。。
网友评论