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
您可能感兴趣的文章:
- RabbitMQ单机集群搭建出现Error: unable to perform an operation on node 'rabbit1@ClusterNode1'
- DotNetty在window和linux下的性能对比
- .net core 图片合并,图片水印,等比例缩小,SixLabors.ImageSharp
- WPF简单的分页控件实现
- ASP.NET中实现中文简/繁体自动转换的类
- 前后端分离,https站点无法通过Ajax访问http资源(Mixed Content,The page at 'https://xxx.com' was loaded over HTTPS)
- layui,返回的数据不符合规范,正确的成功状态码 (code) 应为:0
如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!
网友评论