Unity调用摄像头画面翻转问题

2019-11-21  本文已影响0人  下雨之後

如何在Unity实现AR中的现实背景效果

竖屏实现过程中发现画面翻转有问题,下面是调整:

  1. 调整image中Rotation的Y:180Z:-90
  2. 然后在调整image的宽高

调用摄像头代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class CameraBGController : MonoBehaviour
{
    WebCamTexture camTexture;
    CanvasScaler canvasScaler;
    Image image;

    void Start()
    {
        image = GetComponentInChildren<Image>();
        canvasScaler = GetComponent<CanvasScaler>();
        canvasScaler.referenceResolution = new Vector2(Screen.width, Screen.height);
        StartCoroutine(CallCamera());
        // 调整image宽高,因为rotation的Z轴翻转了-90,所有宽为Height,高为width
        transform.GetChild(0).gameObject.GetComponent<RectTransform>().sizeDelta = new Vector2(Screen.height, Screen.width);
    }

    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.width, Screen.height, 60);
            image.canvasRenderer.SetTexture(camTexture);
            camTexture.Play();

        }
    }
}
上一篇 下一篇

猜你喜欢

热点阅读