Unity3D调用摄像头显示当前拍摄画面

  1. using UnityEngine;  
  2. using System.Collections;  
  3.   
  4. public class CameraTest : MonoBehaviour  
  5. {  
  6.     public WebCamTexture cameraTexture;  
  7.     public string cameraName="";  
  8.     private bool isPlay = false;  
  9.     // Use this for initialization  
  10.     void Start()  
  11.     {  
  12.         StartCoroutine(Test());  
  13.     }  
  14.   
  15.     // Update is called once per frame  
  16.     void Update()  
  17.     {  
  18.   
  19.     }  
  20.   
  21.     IEnumerator Test()  
  22.     {  
  23.         yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);  
  24.         if (Application.HasUserAuthorization(UserAuthorization.WebCam))  
  25.         {  
  26.             WebCamDevice[] devices = WebCamTexture.devices;  
  27.             cameraName = devices[0].name;  
  28.             cameraTexture = new WebCamTexture(cameraName, 400, 300, 15);  
  29.             cameraTexture.Play();  
  30.             isPlay = true;  
  31.         }  
  32.     }  
  33.   
  34.     void OnGUI()  
  35.     {  
  36.         if (isPlay)  
  37.         {  
  38.             GUI.DrawTexture(new Rect(0, 0, 400, 300), cameraTexture, ScaleMode.ScaleToFit);  
  39.         }  
  40.     }  

原文地址:https://www.cnblogs.com/yfceshi/p/6856723.html