-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebCamTextureUI.cs
More file actions
89 lines (80 loc) · 2.13 KB
/
WebCamTextureUI.cs
File metadata and controls
89 lines (80 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
/// <summary>
/// 使用摄像头作为UI背景,挂载在UI的RawImage组件下
/// </summary>
public class WebCamTextureUI: MonoBehaviour {
public WebCamTexture webcamTexture;
public RawImage rawImage;
public string[] devicesNames;
// Use this for initialization
void Start () {
if (rawImage == null)
{
rawImage = GetComponent<RawImage>();
}
if (rawImage != null)
{
StartCoroutine(Camtexture());
}
}
/// <summary>
/// 控制视频的播放和停止
/// </summary>
/// <param name="isPlay"></param>
public void StartPlayWebcamTexture(bool isPlay = true)
{
if (isPlay)
{
if (webcamTexture == null)
{
StartCoroutine(Camtexture());
}
else
{
webcamTexture.Play();
}
}
else
{
if (webcamTexture == null)
{
StartCoroutine(Camtexture());
webcamTexture.Stop();
}
else
{
webcamTexture.Stop();
}
}
}
// Update is called once per frame
void Update () {
}
/// <summary>
/// 开始读取摄像头
/// </summary>
/// <returns></returns>
public IEnumerator Camtexture()
{
yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);
if (Application.HasUserAuthorization(UserAuthorization.WebCam))
{
WebCamDevice[] devices = WebCamTexture.devices;
if (devices.Length <= 0)
{
yield return false;
}
SetBackGroundExpend();
webcamTexture = new WebCamTexture(devices[0].name, 1920, 1080,15);
rawImage.texture = webcamTexture;
webcamTexture.Play();
}
}
public void SetBackGroundExpend()
{
rawImage.rectTransform.sizeDelta = new Vector2(Screen.width,Screen.height);
}
}