CameraManager.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace MartellController
  5. {
  6. /// <summary>
  7. /// 相机管理器
  8. /// 并在相机初始化后返回贴图
  9. /// </summary>
  10. public class CameraManager : MonoBehaviour
  11. {
  12. public static CameraManager Instance;
  13. private WebCamDevice iosCamDevice;
  14. private static WebCamTexture iosCamTexture;
  15. private string cameraName = "Logitech BRIO";
  16. private int cameraWidth;
  17. private int cameraHeight;
  18. private int cameraFPS = 60;
  19. // ==================================================
  20. private void Awake()
  21. {
  22. Instance = this;
  23. }
  24. private void Start()
  25. {
  26. Init();
  27. }
  28. private void OnApplicationQuit()
  29. {
  30. UnInit();
  31. }
  32. // ==================================================
  33. private void Init()
  34. {
  35. StartCoroutine("RequestCameraAuthorization");
  36. return;
  37. }
  38. private void UnInit()
  39. {
  40. iosCamTexture.Stop();
  41. return;
  42. }
  43. /// <summary>
  44. /// 获取相机使用权限
  45. /// </summary>
  46. /// <returns></returns>
  47. private IEnumerator RequestCameraAuthorization()
  48. {
  49. yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);
  50. if (Application.HasUserAuthorization(UserAuthorization.WebCam))
  51. {
  52. LogManager.Log("已获取相机权限");
  53. CreateCamera();
  54. }
  55. else
  56. {
  57. LogManager.ErrorLog("无法获取相机权限");
  58. StartCoroutine("RequestCameraAuthorization");
  59. }
  60. yield break;
  61. }
  62. private static bool isCameraCreated = false;
  63. private void CreateCamera()
  64. {
  65. if (WebCamTexture.devices.Length <= 0)
  66. {
  67. LogManager.ErrorLog("设备无可用相机");
  68. return;
  69. }
  70. #if UNITY_EDITOR // 编辑器使用罗技
  71. foreach (WebCamDevice device in WebCamTexture.devices)
  72. {
  73. if (device.name == cameraName)
  74. {
  75. iosCamDevice = device;
  76. }
  77. }
  78. iosCamTexture = new WebCamTexture(iosCamDevice.name, cameraWidth, cameraHeight, cameraFPS);
  79. #else // IOS使用0号相机
  80. iosCamDevice = WebCamTexture.devices[0];
  81. iosCamTexture = new WebCamTexture(iosCamDevice.name);
  82. #endif
  83. iosCamTexture.Play();
  84. isCameraCreated = true;
  85. LogManager.Log($"Name :{iosCamTexture.deviceName} / Width:{iosCamTexture.width} / Height:{iosCamTexture.height} / FPS:{iosCamTexture.requestedFPS}");
  86. LogManager.Log("相机初始化完成");
  87. return;
  88. }
  89. /// <summary>
  90. /// 返回相机贴图
  91. /// </summary>
  92. /// <returns></returns>
  93. public static Texture GetCamTexture()
  94. {
  95. if (isCameraCreated)
  96. {
  97. return iosCamTexture;
  98. }
  99. else
  100. {
  101. return null;
  102. }
  103. }
  104. }
  105. }