OpenCamera.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. public class OpenCamera : MonoBehaviour
  6. {
  7. // UI 相关参数
  8. public RawImage rawImage;
  9. // 摄像机图片参数
  10. private WebCamTexture webCamTexture;
  11. // Start is called before the first frame update
  12. void Start()
  13. {
  14. StartCoroutine("OpenCameraTest");
  15. }
  16. IEnumerator OpenCameraTest()
  17. {
  18. // 申请相机权限
  19. yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);
  20. // 判断是否有相机权限
  21. if (Application.HasUserAuthorization(UserAuthorization.WebCam))
  22. {
  23. // 获取相机设备
  24. WebCamDevice[] webCamDevices = WebCamTexture.devices;
  25. // 判断是否有相机设别
  26. if (webCamDevices != null && webCamDevices.Length > 0)
  27. {
  28. // 把 0 号设备(移动端后置摄像头)名称赋值
  29. string webCamName = webCamDevices[0].name;
  30. // 设置相机渲染宽高,并运行相机
  31. webCamTexture = new WebCamTexture(webCamName, Screen.width, Screen.height);
  32. // webCamTexture.requestedFPS = 30;
  33. webCamTexture.Play();
  34. // 把获取的图像渲染到画布上
  35. rawImage.texture = webCamTexture;
  36. }
  37. }
  38. }
  39. }