Unity3D 原生WebCamera实现摄像头显示

    今天小编为大家分享一下,如何通过WebCamera 调用外部的摄像头。Unity3D 原生WebCamera实现摄像头显示

  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.

    Unity3D 原生WebCamera实现摄像头显示

2.png (117.54 KB, 下载次数: 1)

下载附件  保存到相册

2017-2-8 17:16 上传



   3.在场景中新建一个Camera,并且把对象重新命名为WebCamera,在WebCamera下面添加一个子对象Plane[PlaneMeshRender],注意点是:(1).plane的Rotation (X:90 Y:180 Z:0)如果不修改 ,显示的画面,会相反显示;(2).需要MeshRender,把第一步操作的材质球附加上。

    Unity3D 原生WebCamera实现摄像头显示

3_0.png (145.37 KB, 下载次数: 1)

下载附件  保存到相册

2017-2-8 17:16 上传




    Unity3D 原生WebCamera实现摄像头显示

3_1.png (195.61 KB, 下载次数: 1)

下载附件  保存到相册

2017-2-8 17:16 上传



   4.到这一步,就是比较重点了,在WebCamera上附加一个WebCameraManager.cs 组件类,主要是处理调用外部摄像头,并且显示摄像的内容。

    Unity3D 原生WebCamera实现摄像头显示

4.png (207.3 KB, 下载次数: 1)

下载附件  保存到相册

2017-2-8 17:17 上传



     WebCameraManager.cs 代码如下:

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

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

相关文章: