U3D实现WebCamera显示


Unity3D 原生WebCamera实现摄像头显示





原创

2017年02月15日 12:03:22

        <ul class="article_tags clearfix csdn-tracking-statistics tracking-click" data-mod="popu_377">
            <li class="tit">标签:</li>


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

     

     

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

     

      

     

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

      

       

        WebCameraManager.cs 代码如下:

    [csharp] view plain copy
    print ?
    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. }  
    using System.Collections;[/align]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.最后直接运行看效果哈!



                    </div>
    </article>
    
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
软件介绍:可随意截图,不管是手机上显示的还是手机摄像头里看到的,更可录像(将你在手机上玩的游戏录下来分享给好友-可谓是众乐乐啊)本软件支持USB_蓝牙_WIFI 塞班相关信息http://bbs.dospy.com/thread-5086709-1-193-4.html 安装说明:先安装Pc端_mobiola_webcam_3_0_11.exe文件后安装Webcam_v3.0.11汉化.exe会提示覆盖,点确定。 再安装手机端_webcam_s60_v_3_0.sis(此文件需你自签名!) 而后把手机和电脑用USB数据线(或蓝牙_WIFI)连接,首先打开手机端Mobiola Web Camera for S60软件,然后打开电脑端Mobiola Web Camera for S60软件,这时电脑右下方的Mobiola Web Camera for S60小图标会有一条红杠,在软件右方选择USB,然后选择端口。(端口的查看-我的电脑-右键-管理-设备管理器-端口里查找你的手机端口是哪一个)如果还是分别不出那可在电脑端Mobiola Web Camera for S60软件里一个一个试,选择好后红扛会消失,表示连接成功 在手机Mobiola Web Camera for S60介面上有两个介面,一个是cam 1和Screen。请在cam 1介面下点-选择-connect-USB 点确定。这时就会自动连接。连接成功后电脑端Mobiola Web Camera for S60软件会显示出你的手机摄像头拍摄出的情景,也可通过它截取你手机上显示的所有内容(图片格式),录像也不在话下!一个字-强... 注明:开这软件要先开手机端,再开电脑端。如果中间你关了视频,那么要把电脑端的关掉,先把手机端打开再开电脑端。支持QQ、MSN等所有聊天视频等... ____如果觉得好用 也请多关照本店(淘宝店铺:http://shop68388423.taobao.com/)软件陆续更新中____

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值