竖屏实现过程中发现画面翻转有问题,下面是调整:
- 调整image中Rotation的
Y:180
,Z:-90
- 然后在调整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();
}
}
}