unity中webcamera的控制

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

public class WebCameraManager : MonoBehaviour
{

    public string DeviceName;
    public Vector2 CameraSize;
    public float CameraFPS;
    public Vector3 plane;

    //接收返回的图片数据 
    WebCamTexture _webCamera;
    public GameObject Plane;//作为显示摄像头的面板

    private void Start()
    {
        StartCoroutine("InitCameraCor");
    }

    public void PlayCamera()  //开启摄像头
    {
        Plane.GetComponent<MeshRenderer>().enabled = true;
        _webCamera.Play();
    }


    public void StopCamera()  // 关闭摄像头
    {
        Plane.GetComponent<MeshRenderer>().enabled = false;
        _webCamera.Stop();
    }

    /// <summary> 
    /// 初始化摄像头
    /// </summary> 
    public IEnumerator InitCameraCor()
    {
        yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);
        if (Application.HasUserAuthorization(UserAuthorization.WebCam))
        {
            WebCamDevice[] devices = WebCamTexture.devices;
            DeviceName = devices[0].name;
            print("camrera name:" + DeviceName);
            _webCamera = new WebCamTexture(DeviceName, (int)CameraSize.x, (int)CameraSize.y, (int)CameraFPS);

            Plane.GetComponent<Renderer>().material.mainTexture = _webCamera;
            Plane.transform.localScale = plane;

            _webCamera.Play();
        }
    }
}

猜你喜欢

转载自blog.csdn.net/qq_33174548/article/details/90031341