Unity3D 原生WebCamera实现摄像头显示
来源:互联网 发布:美丽说网络兼职客服 编辑:程序博客网 时间:2024/05/20 18:00
Unity3D 原生WebCamera实现摄像头显示
今天小编为大家分享一下,如何通过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 代码如下:
[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.最后直接运行看效果哈!
阅读全文
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
- 省赛 Binary to Prime
- UR5机械臂仿真环境搭建
- 以小米时钟为demo学习自定义view过程总结
- 选择器
- 对机器学习的理解
- Unity3D 原生WebCamera实现摄像头显示
- 数据结构心得
- echarts内通过zrender添加文字,图形等
- 微型技术博客——connect函数
- 线程池
- C++ 多项式计算器
- 高分子材料成型(九)
- 关于如何获取int型的最大值
- 杭电1016 Prime Ring Problem