BITKit/Packages/Runtime~/Unity/Scripts/Camera/CameraProvider.cs

48 lines
1.5 KiB
C#
Raw Normal View History

2023-06-07 02:02:14 +08:00
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using Cysharp.Threading.Tasks;
using UnityEngine.UI;
namespace BITKit
{
public class CameraProvider : MonoBehaviour
{
[SerializeField] private string cameraName;
[SerializeField] private RenderTexture cameraTexture;
[SerializeField] private RawImage rawImage;
// Start is called before the first frame update
async void Start()
{
await Application.RequestUserAuthorization(UserAuthorization.WebCam);
if (Application.HasUserAuthorization(UserAuthorization.WebCam))
{
WebCamDevice[] devices = WebCamTexture.devices;//获取可用设备
if (WebCamTexture.devices.Length <= 0)
{
Debug.LogError("没有摄像头设备,请检查");
return;
}
BIT4Log.Log<CameraProvider>($"已获取到摄像头:{string.Join(" and ",devices.Select(x=>x.name))}");
string devicename =string.IsNullOrEmpty(cameraName) ? devices.First().name : cameraName;
var webCamTexture = new WebCamTexture(devicename, 256, 256, 30)
{
wrapMode = TextureWrapMode.Repeat
};
rawImage.texture = webCamTexture;
webCamTexture.Play();
}
else
{
2023-08-23 01:59:26 +08:00
BIT4Log.Warning<CameraProvider>("未获取到WebCam授权");
2023-06-07 02:02:14 +08:00
}
}
}
}