游戏开发论坛

 找回密码
 立即注册
搜索
查看: 4135|回复: 0

Unity3D 原生WebCamera实现摄像头显示

[复制链接]

15

主题

16

帖子

102

积分

注册会员

Rank: 2

积分
102
发表于 2017-2-15 11:54:29 | 显示全部楼层 |阅读模式
今天为大家分享一下,如何通过WebCamera 调用外部的摄像头。欢迎加我的学习交流群:575561285
1.首先我们需要简单认识一下,unity有关摄像头需要用到的内置类;
   WebCamDevice
      官方文档:https://docs.unity3d.com/ScriptReference/WebCamDevice.html
   WebCamTexture
      官方文档:https://docs.unity3d.com/ScriptReference/WebCamTexture.html

  
2.
新建一个unity3d 项目,在场景中新建Resources文件夹》Material文件夹,在文件夹中新建一个材质CameraPlane.mat;并且材质球的Shader:Unlit/Texture.
  
3.
在场景中新建一个Camera,并且把对象重新命名为WebCamera,在WebCamera下面添加一个子对象Plane[PlaneMeshRender],注意点是:(1).plane的Rotation (X:90 Y:180 Z:0)如果不修改 ,显示的画面,会相反显示;(2).需要MeshRender,把第一步操作的材质球附加上。
4.到这一步,就是比较重点了,在WebCamera上附加一个WebCameraManager.cs 组件类,主要是处理调用外部摄像头,并且显示摄像的内容。

   
  WebCameraManager.cs 代码如下:
  1. using System.Collections;[/align]using System.Collections.Generic;
  2. using UnityEngine;
  3.    
  4. public class WebCameraManager : MonoBehaviour {
  5.    
  6.         public string DeviceName;
  7.         public Vector2 CameraSize;
  8.         public float CameraFPS;
  9.    
  10.         //接收返回的图片数据
  11.         WebCamTexture _webCamera;
  12.         public GameObject Plane;//作为显示摄像头的面板
  13.    
  14.    
  15.         void OnGUI()
  16.         {
  17.                 if(GUI.Button(new Rect(100,100,100,100),"Initialize Camera"))
  18.                 {
  19.                         StartCoroutine ("InitCameraCor");
  20.                 }
  21.    
  22.                 //添加一个按钮来控制摄像机的开和关
  23.                 if(GUI.Button(new Rect(100,250,100,100),"ON/OFF"))
  24.                 {
  25.                         if (_webCamera != null && Plane != null) {
  26.    
  27.                                 if (_webCamera.isPlaying)
  28.                                         StopCamera ();
  29.                                 else
  30.                                         PlayCamera ();
  31.                         }
  32.                 }
  33.                 if(GUI.Button(new Rect(100,450,100,100),"Quit")){
  34.                            
  35.                         Application.Quit();
  36.                 }
  37.    
  38.         }
  39.    
  40.         public void PlayCamera()
  41.         {
  42.                 Plane.GetComponent<MeshRenderer> ().enabled = true;
  43.                 _webCamera.Play();
  44.         }
  45.    
  46.    
  47.         public void StopCamera()
  48.         {
  49.                 Plane.GetComponent<MeshRenderer> ().enabled =false;
  50.                 _webCamera.Stop();
  51.         }
  52.    
  53.         /// <summary>
  54.         /// 初始化摄像头
  55.         /// </summary>
  56.         public IEnumerator InitCameraCor()
  57.         {
  58.                 yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);
  59.                 if (Application.HasUserAuthorization(UserAuthorization.WebCam))
  60.                 {
  61.                         WebCamDevice[] devices = WebCamTexture.devices;
  62.                         DeviceName= devices[0].name;
  63.                         _webCamera=new WebCamTexture(DeviceName,(int)CameraSize.x,(int)CameraSize.y,(int)CameraFPS);
  64.    
  65.                         Plane.GetComponent<Renderer> ().material.mainTexture=_webCamera;
  66.                         Plane.transform.localScale = new Vector3 (1,1,1);
  67.    
  68.                         _webCamera.Play();
  69.                 }
  70.         }
  71. }
复制代码

  
5.
最后直接运行看效果哈!


图片:5_1.png


您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2025-2-25 00:36

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表