当前位置: 首页 > news >正文

Unity 打开摄像头 并显示在UI

需求:

打开相机并显示在UI上

效果:

注意:

电脑可能有多个摄像头,注意名称

代码:

using System;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;
using System.Collections.Generic;
#if UNITY_EDITOR
using UnityEditor;
#endif

namespace Streamlab.XingJiKongLong
{
#if UNITY_EDITOR
    [CustomEditor(typeof(RealCameraDeviceManager))]
    public class RealCameraDeviceManagerEditor : Editor
    {

        private RealCameraDeviceManager script;

        private void OnEnable()
        {
            script = (RealCameraDeviceManager)target;
        }

        public override void OnInspectorGUI()
        {
            base.OnInspectorGUI();
            GUILayout.BeginHorizontal();
            {
                if (GUILayout.Button("初始化", GUILayout.Height(50)))
                {
                    script.InitDevices();
                }
                if (GUILayout.Button("打开", GUILayout.Height(50)))
                {
                    script.Play();
                }

                if (GUILayout.Button("关闭", GUILayout.Height(50)))
                {
                    script.Stop();
                }

                if (GUILayout.Button("暂停", GUILayout.Height(50)))
                {
                    script.Pause();
                }
            }
            GUILayout.EndHorizontal();

        }
    }
#endif
    public class RealCameraDeviceManager : MonoBehaviour
    {
        public bool Inited = false;
        public bool IsPlaying = false;

        private WebCamDevice CurrentWebCamDevice;
        private WebCamTexture CurrentWebCamTexture;

        public RawImage image;

        /// <summary>
        /// 默认摄像头设备名称
        /// </summary>
        public string DefaultWebCamDeviceName = "USB2.0 PC CAMERA";

        public List<string> WebCamDevice = new List<string>();
        public void InitDevices()
        {
            WebCamDevice[] devices = WebCamTexture.devices;

            if (devices.Length == 0)
            {
                throw new Exception("暂未识别到摄像头设备,请检查摄像头有没有");
            }
            else
            {
                WebCamDevice.Clear();
                foreach (var item in devices)
                {
                    Debug.Log($"摄像头设备【{item.name}】");
                    WebCamDevice.Add(item.name);
                }
            }

            if (string.IsNullOrEmpty(DefaultWebCamDeviceName))
            {
                DefaultWebCamDeviceName = devices[0].name;
            }


            CurrentWebCamDevice = devices.Where(x => x.name == DefaultWebCamDeviceName).FirstOrDefault();
            CurrentWebCamTexture = new WebCamTexture(CurrentWebCamDevice.name);

            if (image == null)
            {
                throw new Exception("未指定显示摄像头的UI");
            }
            image.texture = CurrentWebCamTexture;

            Inited = true;
        }

        public void Play()
        {
            if (Inited == false)
            {
                throw new Exception("未初始化摄像头设备");
            }

            CurrentWebCamTexture.Play();
            IsPlaying = true;
        }

        public void Stop()
        {
            if (Inited == false)
            {
                throw new Exception("未初始化摄像头设备");
            }
            CurrentWebCamTexture.Stop();
            IsPlaying = false;
        }

        public void Pause()
        {
            if (Inited == false)
            {
                throw new Exception("未初始化摄像头设备");
            }
            CurrentWebCamTexture.Pause();
            IsPlaying = false;
        }
    }
}

 一键三联

点赞·留言·收藏

如果可以帮到你,好用的话,记得一键三连。

毕竟博主写文章是靠爱发电,所以爱会消失对不对

相关文章:

  • 防御保护实验三---前七章综合练习
  • labelimg的xml文件转labelme的json文件
  • 【DeepSeek-R1】满血版免费网页端使用(不卡顿,支持联网搜索)
  • 前端工程化的具体实现细节
  • 【从字节码的角度看switch-case】
  • spring boot知识点2
  • DeePMD深度势教程6:Abacus STRU文件转换方法
  • java如何读取文件一条一条处理,快速把下划线变量变为驼峰变量
  • 栈回溯方案
  • 使用DeepSeek+本地知识库,尝试从0到1搭建高度定制化工作流(自动化篇)
  • Pytorch实现论文之利用多生成器来预防模式崩溃
  • 解决“区间内查询数字的频率”问题
  • I2C、SPI、UART
  • java项目打包成docker镜像步骤
  • Node.js 的 http 模块
  • 前端监控的具体实现细节
  • 代码补全『三重奏』:EverEdit如何用上下文识别+语法感知+智能片段重构你的编码效率!
  • SAP 归档 生产订单的序列号查询
  • Word Embeddings
  • 51单片机入门_10_数码管动态显示(数字的使用;简单动态显示;指定值的数码管动态显示)
  • 气候多米诺:厄尔尼诺与东南亚跨境害虫或威胁中国粮食安全
  • 持续8年仍难终了的纠纷:败诉方因拒执罪被立案,胜诉方银行账户遭冻结
  • 这个“超强致癌细菌”,宝宝感染率高达40%,预防却很简单
  • 青海规范旅游包车行为:不得引导外省籍旅游包车违规驻地运营
  • 人民币对美元即期汇率盘中创半年新高,离岸市场升破7.2
  • 国际博物馆日中国主会场确定,北京将展“看·见殷商”等展览