unity打开相机

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

public class opencam : MonoBehaviour {
    WebCamTexture _webCamera;
    string DeviceName; 
    public GameObject Plane;//作为显示摄像头的面板  在界面上拖动赋值

	// Use this for initialization
	void Start () {
        StartCoroutine("InitCameraCor");  
	}

//    void OnGUI()
//    {
//        PlayCamera();  
//    }
//
//    public void PlayCamera()
//    {
//        Plane.GetComponent<MeshRenderer>().enabled = true;
//        _webCamera.Play();
//    }

    public IEnumerator InitCameraCor()
    {
        yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);
        if (Application.HasUserAuthorization(UserAuthorization.WebCam))
        {
            WebCamDevice[] devices = WebCamTexture.devices;
            for (int i = 0; i < devices.Length; i++)
            {
                //print(devices[i].name);
                if (devices[i].name == "Creative GestureCam")
                    DeviceName = devices[i].name;
            }

            _webCamera = new WebCamTexture(DeviceName, 640, 480, 30);

            Plane.GetComponent<Renderer>().material.mainTexture = _webCamera;
           
            _webCamera.Play();

        }
    }  
}

猜你喜欢

转载自blog.csdn.net/moonlightpeng/article/details/80294089