unity摄像头的操作

版权声明:转载请注明,谢谢 https://blog.csdn.net/qq_38655924/article/details/83505591

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

[RequireComponent(typeof(RawImage))]  //脚本挂在RawImage  
public class CameraDevice : MonoBehaviour
{
    private List<WebCamTexture> webCamTextures = new List<WebCamTexture>();
    private RawImage image;
    #region Unity Method
    // Called once before Start
    void Awake()
    {
        image = GetComponent<RawImage>();
    }

    // Use this for initialization
    IEnumerator Start()
    {
        //获取授权  
        yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);
        if (Application.HasUserAuthorization(UserAuthorization.WebCam))
        {
            WebCamDevice[] devices = WebCamTexture.devices;
            for (int i = 0; i < devices.Length; i++)
            {
                WebCamTexture webCamTexture = new WebCamTexture(devices[i].name, 400, 300, 12);
                webCamTexture.Play();
                webCamTextures.Add(webCamTexture);
            }
        }
    }

    // Update is called once per frame
    void Update()
    {

    }

    // OnDestroy is called when the App is killed
    void OnDestroy()
    {

    }
    #endregion

    public void SetDevice(int index)  //按钮调用
    {
        image.texture = webCamTextures[index];
    }
}

猜你喜欢

转载自blog.csdn.net/qq_38655924/article/details/83505591