Unity打开摄像头占满全屏

AR项目需求,Unity打开摄像头作为背景渲染占满全屏~ Unity对设备硬件操作的API并不是太友好~打开一个摄像头,渲染到屏幕上也都得自己写,虽然步骤少,提取摄像头texture,渲染到UGUI上(本文采取的是UGUI的方案),这时候涉及到一个屏幕适配的问题,以及Unity层级问题。。。

下面先贴上代码和场景配置~ 再说一些坑。。

using UnityEngine;
using System.Collections;
using UnityEngine.UI; public class STCamDeviceController : MonoBehaviour
{ WebCamTexture camTexture;
CanvasScaler CanScaler;
Camera ca;
Image img; void Start () { img = GetComponentInChildren<Image>(); CanScaler = GetComponentInChildren<CanvasScaler> ();
CanScaler.referenceResolution = new Vector2 (Screen.width, Screen.height); ca = GetComponentInChildren<Camera> ();
ca.orthographicSize = Screen.width / 100.0f/ 2.0f; img.transform.localScale = new Vector3 (-1, -1, -1); img.rectTransform.anchorMin = new Vector2 (0.5f, 0.5f);
img.rectTransform.anchorMax = new Vector2 (0.5f, 0.5f);
img.rectTransform.pivot = new Vector2(0.5f,0.5f); img.rectTransform.SetSizeWithCurrentAnchors (RectTransform.Axis.Horizontal, Screen.height);
img.rectTransform.SetSizeWithCurrentAnchors (RectTransform.Axis.Vertical, Screen.width); // 设备不同的坐标转换
#if UNITY_IOS || UNITY_IPHONE
img.transform.Rotate (new Vector3 (0, 180, 90));
#elif UNITY_ANDROID
img.transform.Rotate (new Vector3 (0, 0, 90));
#endif StartCoroutine(CallCamera());
} IEnumerator CallCamera()
{
yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);
if (Application.HasUserAuthorization(UserAuthorization.WebCam))
{
if (camTexture != null)
camTexture.Stop(); WebCamDevice[] cameraDevices = WebCamTexture.devices;
string deviceName = cameraDevices[0].name; camTexture = new WebCamTexture(deviceName,Screen.height,Screen.width,60);
img.canvasRenderer.SetTexture(camTexture); camTexture.Play();
}
}
}

此脚本挂在作为打开相机渲染背景的Canvas上~ (UI是另外一个相对独立的Canvas)。。场景里面的配置如下~

再看CameraGBCanvas 和 CameraBG 的配置~ 因为背景image的大小约束都是通过代码动态设置的~

配置如上~ 说说实现思路和一些坑~

首先,第一步。打开相机~

在Start方法里通过 IEnumerator 开启了相机。判断用户是否给了摄像头哦权限,接下来获取设备列表,取第0个就是后置摄像头,取出texture并且渲染到 image上,,这里可以看到取出的texture的 宽等于其高,,高等于其宽,,那是因为取出的textur绕z轴旋转了90度。这里先做了宽高对调~

第二步,渲染成功后背景Image屏幕适配问题。。

a. 首先调整屏幕适配宽高参考值,就为当前屏幕宽高

代码:

		CanScaler = GetComponentInChildren<CanvasScaler> ();
CanScaler.referenceResolution = new Vector2 (Screen.width, Screen.height);

b.摄像头渲染背景的相机已经调整为正交模式了,其中有一个正交大小的值 orthographicSize 。。根据官方文档的说法是当处于垂直转台的时候等于高的一半,也就是代码如下~

		ca = GetComponentInChildren<Camera> ();
ca.orthographicSize = Screen.width / 100.0f/ 2.0f;

c. 接着做image旋转处理

	img.transform.localScale = new Vector3 (-1, -1, -1);

	img.rectTransform.anchorMin = new Vector2 (0.5f, 0.5f);
img.rectTransform.anchorMax = new Vector2 (0.5f, 0.5f);
img.rectTransform.pivot = new Vector2(0.5f,0.5f); img.rectTransform.SetSizeWithCurrentAnchors (RectTransform.Axis.Horizontal, Screen.height);
img.rectTransform.SetSizeWithCurrentAnchors (RectTransform.Axis.Vertical, Screen.width);

d.最后根据设备不同,判断image的rotae,这一点感觉Unity一点儿都不友好,为什么不能自己判断设备自动适配坐标系统叻? Unity API 给我的感觉是发展空间还挺大的,好多地方都需要改进~

	// 设备不同的坐标转换
#if UNITY_IOS || UNITY_IPHONE
img.transform.Rotate (new Vector3 (0, 180, 90));
#elif UNITY_ANDROID
img.transform.Rotate (new Vector3 (0, 0, 90));
#endif

好了上文就是Unity打开摄像头,并且渲染为背景的方法,网上也有一部分博文讲解的是Unity调用摄像头,大家可以参考参考

最新文章

  1. (js) 输入框只能输入中文、英文、数字、@符号和.符号
  2. [最近公共祖先] POJ 3728 The merchant
  3. python基础之正则表达式。
  4. TWaver HTML5 (2D)--基本概念
  5. [Everyday Mathematics]20150126
  6. Window Event 2008
  7. List(列表)
  8. iOS Block 用法 (1)-once again
  9. [跟我学spring学习笔记][DI循环依赖]
  10. JavaSctipr 兼容、技巧、牛角尖
  11. JavaScript中数组Array方法详解
  12. UNIX环境高级编程——进程基本概述
  13. oracle 内存不足处理
  14. git上传到github时犯的错误
  15. div 中图片溢出问题及 CSS3中图片翻转问题
  16. [daily][dpdk] 网卡offload识别包类型;如何模拟环境构造一个vlan包
  17. ClassOne__HomeWork
  18. Arrays.sort和Collections.sort实现原理解析
  19. mybatis generator为实体类生成自定义注释(读取数据库字段的注释添加到实体类,不修改源码)
  20. js中将时间字符串转换为时间戳

热门文章

  1. U盘安装Ubuntu 10.4 Server
  2. linux下打包zip文件
  3. 皮裤原理和运营微信公众号dotNET跨平台
  4. docfx组件介绍--MarkdownLite
  5. 程序员的经济学系列——你不可不知的生存智慧——第一篇:小X是要成为IT精英的男人!
  6. Hadoop学习笔记—4.初识MapReduce
  7. Base 64 编码
  8. 队列送券的实际应用--ConcurrentLinkedQueue并发队列
  9. 《Entity Framework 6 Recipes》中文翻译系列 (28) ------ 第五章 加载实体和导航属性之测试实体是否加载与显式加载关联实体
  10. eCharts动态加载各省份的数据