Unity发布ios和Android获取相机权限的相关问题
@TOC
<hr style=" border:solid; width:100px; height:1px;" color=#000000 size=1">
前言
最近用Unity做App的开发,发布Android和ios的时候遇到了相机权限的问题
<hr style=" border:solid; width:100px; height:1px;" color=#000000 size=1">
一、问题描述
因为app需要用到设备的相机,所以要获取设备的相机权限,之前的app的做法都是如果用户没有给相机权限,就退出app,直接不能玩,但是最近这种做法app提审无法通过,因为国家相关部门发布相关要求,app不能因为用户拒绝权限而不能进入app。
二、解决方法
1.关闭与权限相关的功能
如果用户没有给到权限,相机识别时,将相机背景变为黑色,并且出ui弹窗提示,但弹窗可以关闭,不相关的其他功能仍然可以正常使用。
2.每次挂起恢复进程时做权限判断
在进入app时,每次恢复挂起的app进程时,每次调用相应的权限时,都会去主动请求app是否已经有相关权限了。
三、具体实施
ios和Android获取相机权限的状体不一样,所以分开去写
1.ios获取用户是否给到了相机的权限
先写oc的代码,将下面的这两个“.h”和“.mm”的文件,放到unity 的Plugins文件夹下的ios文件夹下 JCamera.h
#import<Foundation/Foundation.h>
@interface JCamera:NSObject
@end
#import "JCamera.h"
#import <AVFoundation/AVFoundation.h>
#import <AVFoundation/AVCaptureDevice.h>
@implementation JCamera
extern "C" int GetAVCapturePermission()
{
NSString* mediaType = AVMediaTypeVideo;
NSInteger status = AVAuthorizationStatusAuthorized;
status = [AVCaptureDevice authorizationStatusForMediaType: mediaType];
if (status == AVAuthorizationStatusNotDetermined)
{
status=0;
return 0;
}
else if (status == AVAuthorizationStatusAuthorized)
{
status=1;
return 1;
}
else if(status==AVAuthorizationStatusDenied)
{
status=2;
return 2;
}else
{
status=0;
return 0;
}
//UnitySendMessage("PublicGameObject","GetAVCapturePermission",status)
}
@end
然后我们就可以在脚本里进行调用了
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.InteropServices;
using UnityEngine.Android;
public class IOSCameraAuthority : MonoSingleton<IOSCameraAuthority>
{
public static IOSCameraAuthority ins;
/// <summary>
/// 没有相机权限的提示
/// </summary>
public GameObject warningPanel;
public enum CameraStatus
{
UnAuthority = 0,
Authority = 1,
Denied = 2,
None = 3,
}
[DllImport("__Internal")]
public static extern int GetAVCapturePermission();
void Awake()
{
warningPanel.SetActive(false);
ins = this;
}
private void Start()
{
#if UNITY_IOS
if (GetCameraAuthorityStatus()==CameraStatus.Denied)
{
warningPanel.SetActive(true);
UserData.Instance.ifCameraPermissions = false;
}
else
{
warningPanel.SetActive(false);
UserData.Instance.ifCameraPermissions = true;
}
#endif
#if UNITY_ANDROID
if (!Permission.HasUserAuthorizedPermission(Permission.Camera))
{
warningPanel.SetActive(true);
UserData.Instance.ifCameraPermissions = false;
}
else
{
warningPanel.SetActive(false);
UserData.Instance.ifCameraPermissions = true;
}
#endif
}
public CameraStatus GetCameraAuthorityStatus()
{
int status = GetAVCapturePermission();
switch (status)
{
case 0:
return CameraStatus.UnAuthority;
case 1:
return CameraStatus.Authority;
case 2:
return CameraStatus.Denied;
default:
return CameraStatus.None;
}
}
}
2.Android获取用户是否给到了相机的权限
安卓就比较简单了,也已经写在上面的脚本里面了,就是下面贴的这一段
#if UNITY_ANDROID
if (!Permission.HasUserAuthorizedPermission(Permission.Camera))
{
warningPanel.SetActive(true);
UserData.Instance.ifCameraPermissions = false;
}
else
{
warningPanel.SetActive(false);
UserData.Instance.ifCameraPermissions = true;
}
#endif