Unity 开启外部摄像头并渲染到UI上

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class OpenCamera : MonoBehaviour
{

    public RawImage tt;//相机渲染的UI
    private WebCamTexture webCam;

    void Start()
    {
        OpenCameraBackground();
    }

    /// 
    /// 打开摄像机
    /// 
    public void OpenCameraBackground()
    {
        StartCoroutine("StartCam");
    }
    public IEnumerator StartCam()
    {
        int maxl = Screen.width;
        if (Screen.height > Screen.width)
        {
            maxl = Screen.height;
        }
        yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);
        if (Application.HasUserAuthorization(UserAuthorization.WebCam))
        {
            if (webCam != null)
            {
                webCam.Stop();
            }
            tt.gameObject.SetActive(true);//打开渲染图
            WebCamDevice[] devices = WebCamTexture.devices;//获取可用设备
            string devicename = devices[0].name;
            webCam = new WebCamTexture(devicename, maxl, maxl, 12)
            {
                wrapMode = TextureWrapMode.Repeat
            };
            tt.texture = webCam;
            webCam.Play();
        }
    }
}

Unity 开启外部摄像头并渲染到UI上_第1张图片

Unity 开启外部摄像头并渲染到UI上_第2张图片

 

你可能感兴趣的:(Unity,unity,摄像头)