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.