Unity 基础 之 WebCamTexture 获取设备摄像头(包括PC和移动端),简单渲染到 UGUI 或 游戏物体上

  • Post author:
  • Post category:其他


Unity 基础 之 WebCamTexture 获取设备摄像头(包括PC和移动端),简单渲染到 UGUI 或 游戏物体上


目录


Unity 基础 之 WebCamTexture 获取设备摄像头(包括PC和移动端),简单渲染到 UGUI 或 游戏物体上


一、简单介绍


二、实现原理


三、注意事项


四、效果预览


五、实现步骤


六、关键代码


一、简单介绍

Unity中的一些基础知识点。

本节介绍,使用 WebCamTexture  获取设别的摄像头,并且进行渲染,同时解决有可能第一次授权却没有显示画面的情况。

二、实现原理

1、WebCamTexture  获取 WebCamDevice 设备

2、new WebCamTexture(WebCamDevice , width, height, fps) 得到目标摄像头的数据画面

3、渲染到 UGUI 或者 游戏物体上

三、注意事项

1、应用要申请 Camera 权限

2、为了避免第一次授权可能没有得到摄像头的画面的结果,添加安全设置多次未获得摄像头就,可能真没有设备,结束后获取摄像头

四、效果预览

五、实现步骤

1、打开 Unity,新建工程

2、布局场景,添加UGUI 和 游戏物体元素,用来摄像头画面渲染

3、在工程中添加脚本,并编辑脚本,实现获得摄像头画面渲染到画布或游戏物体上的功能

4、把脚本添加到场景中,并对应赋值

5、运行场景,效果如上

六、关键代码

1、OpenCameraOnUGUIOrGameObject

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

public class OpenCameraOnUGUIOrGameObject : MonoBehaviour
{
    public RawImage rawImage;//相机渲染的UI
    public GameObject quad;//相机渲染的GameObject
    private WebCamTexture webCamTexture;

    void Start()
    {
        ToOpenCamera();
    }

    /// <summary>
    /// 打开摄像机
    /// </summary>
    public void ToOpenCamera()
    {
        StartCoroutine("OpenCamera");
    }
    public IEnumerator OpenCamera()
    {
        
        int maxl = Screen.width;
        if (Screen.height > Screen.width)
        {
            maxl = Screen.height;
        }

        // 申请摄像头权限
        yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);
        if (Application.HasUserAuthorization(UserAuthorization.WebCam))
        {
            if (webCamTexture != null)
            {
                webCamTexture.Stop();
            }

            //打开渲染图
            if (rawImage != null)
            {
                rawImage.gameObject.SetActive(true);
            }
            if (quad != null)
            {
                quad.gameObject.SetActive(true);
            }

            // 监控第一次授权,是否获得到设备(因为很可能第一次授权了,但是获得不到设备,这里这样避免)
            // 多次 都没有获得设备,可能就是真没有摄像头,结束获取 camera
            int i = 0;
            while (WebCamTexture.devices.Length <= 0 && 1 < 300)
            {
                yield return new WaitForEndOfFrame();
                i++;
            }
            WebCamDevice[] devices = WebCamTexture.devices;//获取可用设备
            if (WebCamTexture.devices.Length <= 0)
            {
                Debug.LogError("没有摄像头设备,请检查");
            }
            else
            {
                string devicename = devices[0].name;
                webCamTexture = new WebCamTexture(devicename, maxl, maxl == Screen.height ? Screen.width : Screen.height, 30)
                {
                    wrapMode = TextureWrapMode.Repeat
                };

                // 渲染到 UI 或者 游戏物体上
                if (rawImage != null)
                {
                    rawImage.texture = webCamTexture;
                }

                if (quad != null)
                {
                    quad.GetComponent<Renderer>().material.mainTexture = webCamTexture;
                }


                webCamTexture.Play();
            }

        }
        else {
            Debug.LogError("未获得读取摄像头权限");
        }
    }

    private void OnApplicationPause(bool pause)
    {
        // 应用暂停的时候暂停camera,继续的时候继续使用
        if (webCamTexture !=null)
        {
            if (pause)
            {
                webCamTexture.Pause();
            }
            else
            {
                webCamTexture.Play();
            }
        }
        
    }
    
    
    private void OnDestroy()
    {
        if (webCamTexture != null)
        {
            webCamTexture.Stop();
        }
    }
}



版权声明:本文为u014361280原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。