Unity-基础篇-摄像头调用

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

public class CameraCall : MonoBehaviour
{
    WebCamTexture tex;
    public RawImage rawImgl;
    void Start()
    {
        StartCoroutine(OpenCamera());
    }
    IEnumerator OpenCamera()
    {
        yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);
        if (Application.HasUserAuthorization(UserAuthorization.WebCam))
        {
            WebCamDevice[] device = WebCamTexture.devices;
            string deviceName = device[0].name;  
            tex = new WebCamTexture(deviceName,1920,1080);
            rawImgl.texture = tex;
            tex.Play();
            //如果想获取摄像头的分辨率,需要在它play后才能获取。否则值是错误的
 			Debug.Log(camTexture.width + "dddddd" + camTexture.height);
        }
    }
    void StopCamera()
    {
        if (Application.HasUserAuthorization(UserAuthorization.WebCam))
        {
            WebCamDevice[] device = WebCamTexture.devices;
            string deviceName = device[0].name;
            tex.Stop();
        }
    }
}


版权声明:本文为qq_30868065原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。