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)。。场景里面的配置如下~

7305b707jw1f7s2ir4kymj205k09474c.jpg

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

7305b707jw1f7s2m5bw6hj20aq0mewg7.jpg

7305b707jw1f7s2map2mnj20aw0hp75a.jpg

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

首先,第一步。打开相机~
在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调用摄像头,大家可以参考参考~

转载于:https://www.cnblogs.com/Erma-king/p/5869177.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值