Unity3d,调用摄像头显示
2016-02-18 10:17
555 查看
先上代码
在场景里面添加一个plane
调整plane的位置,并把脚本拖上去,运行就可以了。
如果是要在GUITexture上显示,则代码如下:
如果在本机调试的时候出现以下错误提示
这是没有使用摄像头的权限,build一次安卓应用再试就好了,或者使用以下代码,先判断权限
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上显示,则代码如下:
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"); } } }
相关文章推荐
- unity3d深入学习笔记5:内存释放
- unity3d深入学习笔记6:发布Android apk
- Unity在移动平台中的文件操作路径详解
- Unity中的Path对应各平台中的Path
- unity shader 固定管线实例(五) 简单的玻璃效果
- Unity Invoke("方法名",延迟时间)
- unity shader 固定管线实例(四) 渲染不一样的背面
- unity shader 固定管线实例(三) 光照 自发光混合 纹理混合
- [Unity3D]C# 调用C++ DLL
- unity, standard shader消耗两个draw call
- Unity知识二:显示和隐藏游戏对象
- 使用MonoBehaviour创建xml文件
- Unity知识一:简单的点击事件
- unity3D中使用Socket进行数据通信
- unity3D中使用Socket进行数据通信(二)
- 初识Unity
- Unity全景项目经验Q&A
- Unity中预览lua脚本
- Unity3d 纠正上一篇协程讲解的错误
- Unity_UGUI判断鼠标或者手指是否点击在UI上(三)