Unity releases ios and Android to obtain camera permissions

Unity releases ios and Android to obtain camera permissions

preface

Recently, when using Unity to develop App and release Android and ios, we encountered the problem of camera permission

1, Problem description

Because the app needs to use the camera of the device, it is necessary to obtain the camera permission of the device. The previous practice of app is that if the user does not give the camera permission, he will exit the app and can't play directly. However, recently, this practice can't pass the examination of app. Because relevant national departments have issued relevant requirements, app can't enter the app because the user refuses permission.

2, Solution

1. Turn off functions related to permissions

If the user does not give permission, when the camera is recognized, the camera background will become black, and a ui pop-up prompt will appear, but the pop-up window can be closed, and other irrelevant functions can still be used normally.

2. Make permission judgment every time the recovery process is suspended

When entering the app, every time the suspended app process is resumed and the corresponding permission is called, it will actively request whether the app already has relevant permission.

3, Specific implementation

ios and Android are different in obtaining camera permissions, so they are written separately

1.ios obtains whether the user has given permission to the camera

First write the oc code and put the following two ". h" and ". mm" files into the ios folder under the Plugins folder of unity
JCamera.h

#import<Foundation/Foundation.h>
@interface JCamera:NSObject
@end
 

JCamera.mm

#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

Then we can call it in the script

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>
    ///Prompt without camera permission
    /// </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 gets whether the user has given permission to the camera

Android is relatively simple and has been written in the script above, which is the paragraph posted below

#if UNITY_ANDROID
        if (!Permission.HasUserAuthorizedPermission(Permission.Camera))
        {
            warningPanel.SetActive(true);
            UserData.Instance.ifCameraPermissions = false;
        }
        else
        {
            warningPanel.SetActive(false);
            UserData.Instance.ifCameraPermissions = true;
        }
#endif

summary

Let's stop here first. A good memory is not as good as a bad pen. If you have any questions, you can contact me at any time.
Welcome big guys to give more comments to Mengxin, and welcome everyone to discuss it together.
If you feel that the article is a little helpful, please kneel down and beg you to point out "one key three links". Thank you~

Pretend to state that this blog article is original unless otherwise noted. Please keep it if you need to reprint it Original link
https://blog.csdn.net/Wrinkle2017/article/details/117224541
And author information

Keywords: C# Unity Game Development Unity3d

Added by jcarver on Wed, 09 Feb 2022 00:35:46 +0200