Unity调用PC摄像头

转载于Unity3d圣典里面,具体哪位大侠写的我忘咯。

using UnityEngine;
using System.Collections;

public class CameraTest : MonoBehaviour {

	public string deviceName;
    WebCamTexture tex;
    // Use this for initialization
    IEnumerator Start()
    {
        //获取授权
        yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);
        if (Application.HasUserAuthorization(UserAuthorization.WebCam))
        {
            WebCamDevice[] devices = WebCamTexture.devices;
            deviceName = devices[0].name;
            tex = new WebCamTexture(deviceName, 400, 300, 12);
            renderer.material.mainTexture = tex;
            tex.Play();
        }
        else
        {
        }
    } 
}


绑定在一个带有Render组件的物体上就行了。

你可能感兴趣的:(Unity调用PC摄像头)