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

  1. using UnityEngine;  
  2. using System.Collections;  
  3. using System.Threading;  
  4.   
  5. public class BtnControl : MonoBehaviour  
  6. {     
  7.     public WebCamTexture webTex;  
  8.     public string deviceName;  
  9.       
  10.     void Start()  
  11.     {  
  12.           
  13.     }  
  14.     void Update()  
  15.     {  
  16.           
  17.     }  
  18.       
  19.     //绘制按钮  
  20.     void OnGUI()  
  21.     {  
  22.         //开始按钮  
  23.         if(GUI.Button(new Rect(0,10,100,30),"click"))  
  24.         {  
  25.             //调用启动那个协程,开启摄像头  
  26.             StartCoroutine(CallCamera());  
  27.         }  
  28.         //重启开始    
  29.         if (GUI.Button(new Rect(0,80,100,30), "restart"))    
  30.         {    
  31.             webTex.Play();    
  32.         }  
  33.         //绘制摄像头的显示区域以及大小  
  34.         if(webTex!=null)  
  35.             GUI.DrawTexture(new Rect(110,0,200,200), webTex);  
  36.     }  
  37. ///  
  38. ///调用摄像头  
  39. ///  
  40.     IEnumerator CallCamera()    
  41.     {  
  42.         yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);    
  43.         if(Application.HasUserAuthorization(UserAuthorization.WebCam))    
  44.         {    
  45.             WebCamDevice[] devices = WebCamTexture.devices;        
  46.             deviceName = devices[0].name;    
  47.             //设置摄像机摄像的区域    
  48.             webTex=new WebCamTexture(deviceName,10,10,10);       
  49.             webTex.Play();//开始摄像    
  50.         }    
  51.     }   
  52. }  

运行结果:

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值