Unity打开摄像头占满全屏
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调用摄像头,大家可以参考参考
Do you want to spend the rest of your life selling sugared water or do you want a chance to change the world?
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· 展开说说关于C#中ORM框架的用法!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?