using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class WebCamera : MonoBehaviour
{
WebCamTexture webCam;
public RawImage tt;
// 背景
public Image backGround;
/// <summary>
/// 打开摄像机
/// </summary>
///
private void Start()
{
OpenCameraBackground();
}
/// <summary>
/// 初始化相机
/// </summary>
public void OpenCameraBackground()
{
StartCoroutine("startCam");
}
public IEnumerator startCam()
{
int maxl = Screen.width;
if (Screen.height > Screen.width)
{
maxl = Screen.height;
}
yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);
if (Application.HasUserAuthorization(UserAuthorization.WebCam))
{
if (webCam != null)
{
webCam.Stop();
}
//打开渲染图
tt.gameObject.SetActive(true);
//关掉背景
WebCamDevice[] devices = WebCamTexture.devices;
string devicename = devices[0].name;
webCam = new WebCamTexture(devicename, maxl, maxl, 12);
webCam.wrapMode = TextureWrapMode.Repeat;
tt.texture = webCam;
webCam.Play();
}
}
/// <summary>
/// 启动相机
/// </summary>
public void PlayCamera()
{
if (webCam)
{
if (!webCam.isPlaying)
webCam.Play();
}
}
/// <summary>
/// 关闭摄像机
/// </summary>
public void CloseCameraBackground()
{
StartCoroutine(stopCam());
}
public IEnumerator stopCam()
{
int maxl = Screen.width;
if (Screen.height > Screen.width)
{
maxl = Screen.height;
}
yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);
if (Application.HasUserAuthorization(UserAuthorization.WebCam))
{
if (webCam != null)
{
if (webCam.isPlaying)
webCam.Pause();
}
}
}
public void Update()
{
if (Input.GetKeyDown("o"))
{
PlayCamera();
}
if (Input.GetKeyDown("s"))
{
CloseCameraBackground();
}
//Debug.Log(webCam.isPlaying);
}
}
Unity 调用外置摄像头
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- Capture 截图工具类 QR_Code 上传服务器生成二维码类 GetCamera 获取外置摄像头
- 实现步骤如下: 1,要想调用摄像头首先要打开摄像头驱动,如果用户允许则可以使用。 2,定义WebCamTextur...