61 lines
1.6 KiB
C#
61 lines
1.6 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class MyPlaneManager : MonoBehaviour
|
|
{
|
|
public string DeviceName;
|
|
public float CameraFPS;
|
|
|
|
//接收返回的图片数据
|
|
public WebCamTexture _webCamera;
|
|
public RawImage rawImage;
|
|
|
|
|
|
public void Start()
|
|
{
|
|
StartCoroutine("DelayedInit");
|
|
}
|
|
|
|
private IEnumerator DelayedInit()
|
|
{
|
|
yield return new WaitForSeconds(0.5f);
|
|
StartCoroutine("InitCameraCor");
|
|
PlayCamera();
|
|
}
|
|
|
|
public void PlayCamera()
|
|
{
|
|
// rawImage.enabled = true;
|
|
// _webCamera.Play();
|
|
}
|
|
|
|
|
|
public IEnumerator InitCameraCor()
|
|
{
|
|
yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);
|
|
if (Application.HasUserAuthorization(UserAuthorization.WebCam))
|
|
{
|
|
WebCamDevice[] devices = WebCamTexture.devices;
|
|
if (devices.Length > 0)
|
|
{
|
|
DeviceName = devices[0].name;
|
|
}
|
|
else
|
|
{
|
|
Debug.Log("No webcam devices found.");
|
|
}
|
|
float screenWidth = Screen.width;
|
|
float screenHeight = Screen.height;
|
|
_webCamera = new WebCamTexture(DeviceName, (int)screenHeight, (int)screenWidth, 30);
|
|
rawImage.rectTransform.sizeDelta = new Vector2(screenHeight, screenWidth);
|
|
rawImage.texture = _webCamera;
|
|
//rawImage.rectTransform.localRotation = Quaternion.Euler(0, 0, -90);
|
|
_webCamera.Play();
|
|
}
|
|
}
|
|
}
|
|
|
|
|