unity 利用WebCamTexture 开启相机,解决移动端画面反转问题和首次点击同意权限后相机打不开问题。
程序员文章站
2022-03-03 19:49:55
1:解决相机反转问题,和安装后点击同意相机权限后,相机没有打开。using System.Collections;using System.Collections.Generic;using UnityEngine;using UnityEngine.UI;using static UnityEngine.UI.AspectRatioFitter;//挂载到RawImage上即可[RequireComponent(typeof(AspectRatioFitter))][RequireC...
1:解决相机反转问题,和安装后点击同意相机权限后,相机没有打开。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using static UnityEngine.UI.AspectRatioFitter;
//挂载到RawImage上即可
[RequireComponent(typeof(AspectRatioFitter))]
[RequireComponent(typeof(RawImage))]
public class Webcam_Test : MonoBehaviour
{
private WebCamTexture cameraTexture;
//加上AspectRatioFitter组件调整画面尺寸
RawImage background;
AspectRatioFitter fit;
bool isFirst = true;
IEnumerator Start()
{
InitData();
yield return StartCoroutine(InitCameraCor());
}
// Update is called once per frame
void Update()
{
//调整rawimage
AdjustmentRawimage();
//处理用户首次同意权限后相机没有打开的问题
if (cameraTexture == null)
{
var isOk = Application.HasUserAuthorization(UserAuthorization.WebCam);
if (Application.platform == RuntimePlatform.Android)
{
//等待用户同意
if (isOk)
{
if (isFirst)
{
isFirst = false;
WebCamDevice[] devices = WebCamTexture.devices;
var DeviceName = devices[0].name;
//判断平台
if (Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer)
{
for (int i = 0; i < devices.Length; i++)
{
//开启后置摄像头
if (!devices[i].isFrontFacing)
{
DeviceName = devices[i].name;
break;
}
}
}
cameraTexture = new WebCamTexture(DeviceName, Screen.width, Screen.height, 15);
cameraTexture.Play(); // Start the camera
background.texture = cameraTexture; // Set the texture
}
}
}
}
}
void InitData()
{
background = this.transform.GetComponent<RawImage>();
fit = this.transform.GetComponent<AspectRatioFitter>();
fit.aspectMode = AspectMode.EnvelopeParent;
}
/// <summary>
/// 初始化摄像头
/// </summary>
public IEnumerator InitCameraCor()
{
yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);
if (Application.HasUserAuthorization(UserAuthorization.WebCam))
{
WebCamDevice[] devices = WebCamTexture.devices;
var DeviceName = devices[0].name;
//判断平台
if (Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer)
{
for (int i = 0; i < devices.Length; i++)
{
//开启后置摄像头
if (!devices[i].isFrontFacing)
{
DeviceName = devices[i].name;
break;
}
}
}
cameraTexture = new WebCamTexture(DeviceName, Screen.width, Screen.height, 15);
cameraTexture.Play(); // Start the camera
background.texture = cameraTexture; // Set the texture
}
}
void AdjustmentRawimage()
{
if (cameraTexture == null) return;
float ratio = (float)cameraTexture.width / (float)cameraTexture.height;
fit.aspectRatio = ratio; // Set the aspect ratio
float scaleY = cameraTexture.videoVerticallyMirrored ? -1f : 1f; // Find if the camera is mirrored or not
background.rectTransform.localScale = new Vector3(1f, scaleY, 1f); // Swap the mirrored camera
int orient = -cameraTexture.videoRotationAngle;
background.rectTransform.localEulerAngles = new Vector3(0, 0, orient);
}
//这下面的是我跳转到AR场景当中的,需要把上个场景中的webcamTexture关闭。
//没有这一需求的小伙伴门可以忽略
//private void OnDestroy()
//{
// cameraTexture.Stop();
// cameraTexture = null;
//}
}
本文地址:https://blog.csdn.net/wwrm111/article/details/109257583
上一篇: SSM框架学习之MyBatis
下一篇: Python列表数字元素加权和