49 lines
1.6 KiB
C#
49 lines
1.6 KiB
C#
|
using System.Collections;
|
||
|
using System.Collections.Generic;
|
||
|
using System.Linq;
|
||
|
using UnityEngine;
|
||
|
using Cysharp.Threading.Tasks;
|
||
|
using UnityEngine.UI;
|
||
|
using UnityEngine.Windows.WebCam;
|
||
|
|
||
|
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
|
||
|
{
|
||
|
BIT4Log.Warnning<CameraProvider>("未获取到WebCam授权");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
}
|
||
|
}
|
||
|
|