Unity3d调用摄像头显示
Unity3d,调用摄像头显示 先上代码
[csharp] view plain copy
using UnityEngine;
using System.Collections;
public class WebCamManager : MonoBehaviour {
// Use this for initialization
void Start () {
WebCamTexture webcamTexture = new WebCamTexture ();
//如果有后置摄像头,调用后置摄像头
for (int i = 0; i < WebCamTexture.devices.Length; i++) {
if (!WebCamTexture.devices [i].isFrontFacing) {
webcamTexture.deviceName = WebCamTexture.devices [i].name;
break;
}
}
Renderer renderer = GetComponent<Renderer>();
renderer.material.mainTexture = webcamTexture;
webcamTexture.Play();
}
}
在场景里面添加一个plane
调整plane的位置,并把脚本拖上去,运行就可以了。
如果是要在GUITexture上显示,则代码如下:
[csharp] view plain copy
using UnityEngine;
using System.Collections;
public class WebCamManager : MonoBehaviour {
// Use this for initialization
void Start () {
WebCamTexture webcamTexture = new WebCamTexture ();
//如果有后置摄像头,调用后置摄像头
for (int i = 0; i < WebCamTexture.devices.Length; i++) {
if (!WebCamTexture.devices [i].isFrontFacing) {
webcamTexture.deviceName = WebCamTexture.devices [i].name;
break;
}
}
GUITexture guiTexture = GetComponent<GUITexture> ();
guiTexture.texture = webcamTexture;
webcamTexture.Play ();
}
}
如果在本机调试的时候出现以下错误提示
[plain] view plain copy
Cannot use web cam, since the user has not authorized this!
这是没有使用摄像头的权限,build一次安卓应用再试就好了,或者使用以下代码,先判断权限
[csharp] view plain copy
using UnityEngine;
using System.Collections;
public class WebcamManager : MonoBehaviour {
// Use this for initialization
void Start () {
StartCoroutine ("CallWebCam");
}
IEnumerator CallWebCam(){
yield return Application.RequestUserAuthorization (UserAuthorization.WebCam);
if (Application.HasUserAuthorization (UserAuthorization.WebCam)) {
WebCamTexture webcamTexture = new WebCamTexture ();
//如果有后置摄像头,调用后置摄像头
for (int i = 0; i < WebCamTexture.devices.Length; i++) {
if (!WebCamTexture.devices [i].isFrontFacing) {
webcamTexture.deviceName = WebCamTexture.devices [i].name;
break;
}
}
GUITexture guiTexture = GetComponent<GUITexture> ();
guiTexture.texture = webcamTexture;
webcamTexture.Play ();
} else {
Debug.Log ("has not authorization");
}
}
}
相关文章
- 硅谷互联网公司的开发流程
开发流程包括这么几个阶段: OKR 的设立; 主项目及其子项目的确立; 每个子项目的生命周期; 主项目的生命周期; 收尾、维护、复盘。 第一点,OKR 的设立 所有项目的起始,都应该从 Ro
- RESTful-表述性状态转移风格
REST英文全拼:Representational State Transfer 面向资源编程 资源指的就是一类数据 产品表->就是产品资源 最重要的是如何表示一个资源 地址即
- 稳定性思考
产品功能线 0-1: 当系统从无到有的时候,首要考虑的是研发效率,功能快速迭代,满足快速增长的业务需求 1-10 系统已经搭建起来,此时考虑的是系统的稳定性。 可用性:1.隔离:区分出核心和非核心功能
- Supervisor守护队列发邮件
安装 CentOS: yum -y install supervisor Debien/Ubuntu适用:apt-get install supervisor 配置 修改主配置文件:vim /et
- 安装libsodium,让服务器支持chacha20等加密方式
用chacha20加密方式需要安装libsodium 注意:libsodium从1.0.15开始就废弃了aes-128-ctr yum install wget m2crypto git libsod
随机推荐
- 硅谷互联网公司的开发流程
开发流程包括这么几个阶段: OKR 的设立; 主项目及其子项目的确立; 每个子项目的生命周期; 主项目的生命周期; 收尾、维护、复盘。 第一点,OKR 的设立 所有项目的起始,都应该从 Ro
- RESTful-表述性状态转移风格
REST英文全拼:Representational State Transfer 面向资源编程 资源指的就是一类数据 产品表->就是产品资源 最重要的是如何表示一个资源 地址即
- 稳定性思考
产品功能线 0-1: 当系统从无到有的时候,首要考虑的是研发效率,功能快速迭代,满足快速增长的业务需求 1-10 系统已经搭建起来,此时考虑的是系统的稳定性。 可用性:1.隔离:区分出核心和非核心功能
- Supervisor守护队列发邮件
安装 CentOS: yum -y install supervisor Debien/Ubuntu适用:apt-get install supervisor 配置 修改主配置文件:vim /et
- 安装libsodium,让服务器支持chacha20等加密方式
用chacha20加密方式需要安装libsodium 注意:libsodium从1.0.15开始就废弃了aes-128-ctr yum install wget m2crypto git libsod