我正在创建一个使用设备相机的应用程序。 我需要实现一个功能,检查用户是否允许应用程序访问设备相机,如果他们没有,使用相机的场景只显示黑屏。Unity3D访问变量通过Xcode设置
因为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...
}
和follwoing代码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];
}
结果是每运行该应用程序的时间会弹出一个窗口,指示用户启动相机ch正在进步,但他们仍然可以访问显示黑屏而不是相机视图的场景。
有没有什么办法可以从Unity内部访问CameraIsAvilable变量?
任何建议将不胜感激! TIA!
太棒了。这是我需要的! – jsherk 2015-03-24 20:06:23
很高兴帮助你! :) – 2015-03-24 21:07:23