【发布时间】:2014-10-06 12:00:56
【问题描述】:
我正在创建一个使用设备相机的应用程序。 我需要实现一个函数来检查用户是否允许应用访问设备摄像头,如果没有,使用摄像头的场景只会显示黑屏。
由于Unity在webplayer平台只支持Application.HasUserAuthorization,所以需要在构建后通过XCode给项目添加授权检查。
首先,我在 UnityAppController.mm 文件中的 didFinishLaunchingWithOptions 中添加了以下代码:
// Determine camera access on iOS >= 7
if ([AVCaptureDevice respondsToSelector:@selector(requestAccessForMediaType:completionHandler:)]) {
// Completion handler will be dispatched on a separate thread
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
if (YES == granted) {
// User granted access to the camera, continue with app launch
CameraIsAvailable = YES;
CameraCheckDone = YES;
}
else {
CameraIsAvailable = NO;
CameraCheckDone = YES;
}
}];
}
else {
// iOS < 7 (camera access always OK)
CameraCheckDone = YES;
// Continue with app launch...
}
以及applicationDidBecomeActive的以下代码
while (!CameraCheckDone) { }
if(!CameraIsAvailable){
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Authorize Camera"
message:@"Instructions go here"
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertView show];
[alertView release];
}
结果是,每次运行应用程序时,都会出现一个弹出窗口,指示用户激活摄像头,这是一个进度,但他们仍然可以访问显示黑屏的场景,而不是摄像头视图。
有什么方法可以让我在 Unity 中访问 CameraIsAvilable 变量?
任何建议将不胜感激!蒂亚!
【问题讨论】: