Unity3D 原生WebCamera实现摄像头显示
来源:互联网 发布:全网vip视频电影源码 编辑:程序博客网 时间:2024/05/10 02:52
今天为大家分享一下,如何通过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 代码如下:
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.最后直接运行看效果哈!
0 0
- Unity3D 原生WebCamera实现摄像头显示
- Unity3D 原生WebCamera实现摄像头显示
- Unity3D 原生WebCamera实现摄像头显示
- opencv 网络摄像头(webcamera)
- Unity3d,调用摄像头显示
- Unity3D调用摄像头显示当前拍摄画面
- Unity3D调用摄像头显示当前拍摄画面
- Unity3D调用摄像头显示当前拍摄画面
- Unity3D调用摄像头显示当前拍摄画面
- C# 实现真正的WebCamera【原创】
- unity3d鼠标滚轮实现摄像头拉近后退
- Unity3D-调用IOS原生分享的实现
- unity3d实现NPC显示名称
- 原生H5调用摄像头
- FFmpeg+Qt实现摄像头(rtsp)实时显示
- 基于Unity3D的调用摄像头功能的实现
- Unity3D调用摄像头
- Unity3d 原生录音 Microphone
- linux du
- js读取本地图片进行预览,上传服务器
- Android Fragment总结
- [南阳OJ-No.33]蛇形填数|在n*n方陈里填入1,2,...,n*n,要求填成蛇形。
- 数据结构实验之图论二:基于邻接表的广度优先搜索遍历
- Unity3D 原生WebCamera实现摄像头显示
- Android 手写百分比布局
- Android之MVP设计模式
- PHP自定义函数&数组
- 蓝桥杯 算法训练 字符串的展开
- 上海市学分银行理论研究
- PHP开发APP接口之封装通讯接口
- Unity Debug拓展
- cordova 常用命令、插件