素材巴巴 > 程序开发 >

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

程序开发 2023-09-05 16:45:00

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...


标签:

素材巴巴 Copyright © 2013-2021 http://www.sucaibaba.com/. Some Rights Reserved. 备案号:备案中。