[Unity] Unity3D 原生WebCamera实现摄像头显示

查看:902 |回复:12 | 2017-2-15 11:41:15

您需要 登录 才可以下载或查看,没有账号?注册

x
   今天为大家分享一下,如何通过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.
2.png

  3.在场景中新建一个Camera,并且把对象重新命名为WebCamera,在WebCamera下面添加一个子对象Plane[PlaneMeshRender],注意点是:(1).plane的Rotation (X:90 Y:180 Z:0)如果不修改 ,显示的画面,会相反显示;(2).需要MeshRender,把第一步操作的材质球附加上。
       3_0.png
  
  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.最后直接运行看效果哈!

评分

参与人数 1元素币 +3 活跃度 +15 展开 理由
小小橘子 + 3 + 15 这个手我给满分

查看全部评分

2017-2-15 11:41:15  
 赞 赞 0

使用道具 登录

12个回答,把该问题分享到群,邀请大神一起回答。
2#
不知道iOS和安卓可以不?
回复 收起回复
2017-2-15 12:50:17   回复
 赞 赞 0

使用道具 登录

3#
我为了元素币,也试试,感谢
回复 收起回复
2018-10-20 10:04:42   回复
 赞 赞 0

使用道具 登录

4#
       
我为了元素币,也试试,感谢
回复 收起回复
2018-10-20 10:25:40   回复
 赞 赞 0

使用道具 登录

5#
   围观看看看看
回复 收起回复
2018-10-20 11:41:24   回复
 赞 赞 0

使用道具 登录

6#
给力,很给力
回复 收起回复
2018-10-20 15:32:21   回复
 赞 赞 0

使用道具 登录

7#
好东西~多谢分享~!
回复 收起回复
2018-10-21 14:25:41   回复
 赞 赞 0

使用道具 登录

8#
感谢楼主分享
回复 收起回复
2018-10-21 14:38:54   回复
 赞 赞 0

使用道具 登录

9#
感谢你的分享
回复 收起回复
2018-10-22 14:12:01   回复
 赞 赞 0

使用道具 登录

10#

感谢分享!!!
回复 收起回复
2018-10-22 18:21:26   回复
 赞 赞 0

使用道具 登录

11#
请问,在安卓上申请成功了,但是IOS上申请失败,获取不了已经授权的相机权限,这个问题怎么解决呀
回复 收起回复
2021-7-17 15:08:09   回复
 赞 赞 0

使用道具 登录

12#
aa
回复 收起回复
2021-8-12 09:35:50   回复
 赞 赞 0

使用道具 登录

13#
给力............
回复 收起回复
2021-8-27 17:33:39   回复
 赞 赞 0

使用道具 登录

CG 游戏行业专业问题

Unity3D技术手机游戏引擎手游引擎
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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