Unity调用外接摄像头的实例代码(C#)

using UnityEngine;
using System.Collections;
using System.Threading;

public class BtnControl : MonoBehaviour
{	
	public WebCamTexture webTex;
	public string deviceName;
	
	void Start()
	{
		
	}
	void Update()
	{
		
	}
	
	//绘制按钮
	void OnGUI()
	{
		//开始按钮
		if(GUI.Button(new Rect(0,10,100,30),"click"))
		{
			//调用启动那个协程,开启摄像头
			StartCoroutine(CallCamera());
		}
		//重启开始  
		if (GUI.Button(new Rect(0,80,100,30), "restart"))  
		{  
			webTex.Play();  
		}
		//绘制摄像头的显示区域以及大小
		if(webTex!=null)
			GUI.DrawTexture(new Rect(110,0,200,200), webTex);
	}
///
///调用摄像头
///
	IEnumerator CallCamera()  
	{
		yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);  
		if(Application.HasUserAuthorization(UserAuthorization.WebCam))  
		{  
			WebCamDevice[] devices = WebCamTexture.devices;      
			deviceName = devices[0].name;  
			//设置摄像机摄像的区域  
			webTex=new WebCamTexture(deviceName,10,10,10);     
			webTex.Play();//开始摄像  
		}  
	} 
}

运行结果:


学习ing...

你可能感兴趣的:(Unity)