Unity cannot obtain the camera permission on a mobile device or solve the problem. unity camera

Source: Internet
Author: User

Unity cannot obtain the camera permission on a mobile device or solve the problem. unity camera
Problem description our team encountered a camera permission problem when working on the unity-android project. unity can export the apk directly and export the android project in two modes, the latter can be directly post-processed using the following compilation, directly in AndroidManifest. add the camera permission in xml, but this method still does not obtain the permission for the former-exporting the apk directly.

Public static void OnPostprocessBuild (BuildTarget buildTarget, string pathToBuiltProject) {// BuildTarget must be Android if (buildTarget! = BuildTarget. android) return; // read AndroidManifest. xml file XClass UnityAppController = new XClass (pathToBuiltProject + "/HelloAR U3D/src/main/AndroidManifest. xml "); // Add a line of code UnityAppController after the specified code. writeBelow ("","
 
  
");}
 
After finding information, the problem is solved. It is found that the permission mechanism of unity to export android projects is automatically generated based on the permissions required in the Code, whether to export apk directly or android projects. Therefore, you can simply add unnecessary camera calling code in the code. I added the following code:
        private IEnumerator OpenCamera()        {            yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);            if (!Application.HasUserAuthorization(UserAuthorization.WebCam)) yield break;            WebCamDevice[] devices = WebCamTexture.devices;        }
In this way, the camera permissions can be obtained without the previous post-compilation code, which perfectly solves the problem.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.