基于Unity3D的调用摄像头功能的实现
AR中会用到设备的摄像头,那么又如何去在Unity3D中去调用摄像头呢?
原地址:http://blog.****.net/wuyt2008/article/details/50684236
如下代码:
- 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的位置,并把脚本拖上去,运行就可以了。
如果是要在GUITexture上显示,则代码如下:
- 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 ();
- }
- }
如果在本机调试的时候出现以下错误提示
- Cannot use web cam, since the user has not authorized this!
这是没有使用摄像头的权限,build一次安卓应用再试就好了,或者使用以下代码,先判断权限
- 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");
- }
- }
- }
相关推荐
- 基于Asterisk的VoIP开发指南——(1)实现基本呼叫功能
- html5-websocket实现基于远程方法调用的数据交互
- 基于 QPlay 的智能无线流媒体传输音箱的功能实现
- 基于矢量数据的逆地理编码功能实现
- 使用Holer服务端软件实现基于自己Holer服务端的端口映射功能
- 基于ROS实现多功能履带车的制作总结
- 基于prototype实现的图片左右翻页与lightbox功能
- 一行代码调用实现带字段选取+条件判断+排序+分页功能的增强ORM框架
- 【游戏跨场景寻路】基于egret(白鹭)的游戏地图跨场景寻路功能的实现
- 浅析基于ARM的Linux下的系统调用的实现
- 基于Unity3D的调用摄像头功能的实现
- 必读论文 | 卷积神经网络百篇经典论文推荐