Unity获取摄像头权限

本文介绍了在Unity中如何获取摄像头权限的详细步骤,通过内置功能轻松解决摄像头使用问题。
摘要由CSDN通过智能技术生成

直接上代码,unity帮我们内置了功能。

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

public class Test : MonoBehaviour
{
    // 图片组件
    public RawImage rawImage;

    //图形组件父实体
    public RectTransform imageParent;

    //当前相机索引
    private int index = 0;

    //当前运行的相机
    private WebCamTexture currentWebCam;
    void Start()
    {
        StartCoroutine(Call());

    }

    public IEnumerator Call()
    {
        // 请求权限
        yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);


        if (Application.HasUserAuthorization(UserAuthorization.WebCam) && WebCamTexture.devices.Length > 0)
        {
            // 创建相机贴图
            currentWebCam = new WebCamTexture(WebCamTexture.devices[index].name, Screen.width, Screen.height, 60);
            rawImage.texture = currentWebCam;
            currentWebCam.Play();

            //前置后置摄像头需要旋转一定角度,否则画面是不正确的,必须置于Play()函数后
            rawImage.rectTransform.localEu
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值