打开摄像头

unity打开摄像头并将图片显示出来

using UnityEngine;
using System.Collections.Generic;
using System.Collections;
using UnityEngine.UI;
using UnityEditor;
public class WebCamManger : MonoBehaviour
{
    private string deviceName;

    private WebCamTexture webCam;

    // 图片组件
    private RawImage rawImage;

    void Start()
    {
        StartCoroutine(Call());

    }

    private IEnumerator Call()
    {
        // 请求权限
        yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);

        if (Application.HasUserAuthorization(UserAuthorization.WebCam))
        {
            // 获取相机设备
            WebCamDevice[] devices = WebCamTexture.devices;

            // 取第一个相机名称
            deviceName = devices[0].name;

           // 创建相机贴图
            webCam = new WebCamTexture(deviceName, Screen.width, Screen.height, 60);

            // 
            rawImage.texture = webCam;

            // 相机开启
            webCam.Play();
        }
    }
}

你可能感兴趣的:(unity)