Unity实现摄像头录像功能

Unity实现摄像头录像功能

前言

在之前的很多展馆展示的项目中,甲方有很多要求实现用摄像头录像的功能。使用Unity实现调用USB摄像头画面的功能非常容易实现,但是实现录屏的功能有一些困难,我使用了几种方法都没有实现出想要的效果,后来我在网上找到一款叫做AVProMovieCapture的插件,实现了录屏的良好效果,同时也实现了使用Unity实现摄像头录像的效果,具体实现方法如下所示:

实现步骤

1.在项目中导入AVProMovieCapture插件,如下图所示:
在这里插入图片描述
2.在场景中新建plane物体,设置如下图所示:
在这里插入图片描述3.在场景中拖入ScreenGameObject物体,如下图所示:
在这里插入图片描述
4.在场景中新建WebCapture物体,在该物体上挂载WebCapture.cs脚本,脚本代码如下图所示:

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

namespace RenderHeads.Media.AVProMovieCapture.Demos
{
    
    
    public class WebCapture : MonoBehaviour
    {
    
    
        private class Instance
        {
    
    
            public string name;
            public WebCamTexture texture;
            public CaptureFromTexture capture;
            public CaptureGUI gui;
        }

        [SerializeField]
        private GUISkin _skin;

        //[SerializeField]
        //private GameObject _prefab;

        [SerializeField]
        private int _webcamResolutionWidth = 1920;

        [SerializeField]
        private int _webcamResolutionHeight = 1080;

        [SerializeField]
        private int _webcamFrameRate = 30;

        //State
        private Instance[] _instances;
        private int _selectedWebcamIndex;
        //显示视频的面板
        public MeshRenderer plane;
        //调用录像的脚本物体
        public CaptureGUI captureObject;

        private void Start()
        {
    
    
            //Create instance data per webcam
            int numCams = WebCamTexture.devices.Length;
            _instances = new Instance[numCams];
            for (int i = 0;i < numCams;i++)
            {
    
    
                //GameObject go = (GameObject)GameObject.Instantiate(_prefab);

                Instance instance = new Instance();
                instance.name = WebCamTexture.devices[i].name;
                //instance.capture = go.GetComponent<CaptureFromTexture>();
                instance.capture = captureObject.gameObject.GetComponent<CaptureFromTexture>();
                instance.capture._autoFilenamePrefix = "Demo4Webcam-" + i;
                //instance.gui = go.GetComponent<CaptureGUI>();
                instance.gui = captureObject.gameObject.GetComponent<CaptureGUI>();
                instance.gui._showUI = true;

                _instances[i] = instance;
            }

            if (numCams > 0)
            {
    
    
                Change(0);
            }
            StartCoroutine(OpenCamera());
            //captureObject = GameObject.Find("ScreenGameObject(Clone)").GetComponent<CaptureGUI>();
        }
        /// <summary>
        /// 开启摄像头
        /// </summary>
        /// <returns></returns>
        IEnumerator OpenCamera()
        {
    
    
            yield return new WaitForSeconds(0.5f);
            beginCamera();
            yield return new WaitForSeconds(0.5f);
            captureObject.ToStartCapture();
        }

        private void StartWebcam(Instance instance)
        {
    
    
            instance.texture = new WebCamTexture(instance.name,_webcamResolutionWidth,_webcamResolutionHeight,_webcamFrameRate);
            instance.texture.Play();
            if (instance.texture.isPlaying)
            {
    
    
                instance.capture.SetSourceTexture(instance.texture);
                plane.material.mainTexture = instance.texture;
            }
            else
            {
    
    
                StopWebcam(instance);
            }
        }

        private void StopWebcam(Instance instance)
        {
    
    
            if (instance.texture != null)
            {
    
    
                if (instance.capture != null && instance.capture.IsCapturing())
                {
    
    
                    instance.capture.SetSourceTexture(null);
                    instance.capture.StopCapture();
                }
                instance.texture.Stop();
                Destroy(instance.texture);
                instance.texture = null;
            }
        }

        private void OnDestroy()
        {
    
    
            for (int i = 0;i < _instances.Length;i++)
            {
    
    
                StopWebcam(_instances[i]);
            }
        }

        private void Change(int index)
        {
    
    
            _selectedWebcamIndex = index;
            for (int j = 0;j < _instances.Length;j++)
            {
    
    
                _instances[j].gui._showUI = (j == _selectedWebcamIndex);
            }
        }
        /// <summary>
        /// 开启摄像头
        /// </summary>
        public void beginCamera()
        {
    
    
            for (int i = 0;i<_instances.Length;i++)
            {
    
    
                Instance webcam = _instances[i];
                StartWebcam(webcam);
            }
        }
    }
}

5.运行场景,发现已经调用了摄像头,如下图所示:
在这里插入图片描述
6.虽然调用了摄像头,但是不知道是否已经进行了录像,查找到工程下的movie文件夹,发现已经录入了视频,从而实现了使用usb摄像头录像的功能,如下图所示:
在这里插入图片描述
7.实现录像功能,有的需求还需要获取到这些视频并且展示出来,这个也在我之前的项目实现了,具体怎么实现不再赘述了,在这里将核心代码分享在这里:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;


public class Load : MonoBehaviour
{
    
    
    public List<string> filePaths;
    public static string[][] pic;
    private List<string> LAN;
    private string movieUrl;
    //遍历的视频数量
    public static int movieNumber = 0;

    private void Start()
    {
    
    
        movieUrl = ConfigTest.dic["录像路径"]["url"];
        LAN = new List<string>();
        LAN.Add(movieUrl);
        pic = new string[LAN.Count][];
        Debug.Log(pic.Length);
        LoadIma();
    }

    void LoadIma()
    {
    
    
        for (int i = 0;i < pic.Length;i++)
        {
    
    
            pic[i] = (load(LAN[i],i));
        }
    }

    string[] load(string LAN,int t)
    {
    
    
        filePaths = new List<string>();
        string imgtype = "*.mp4|*.mov|*.avi";
        string[] ImageType = imgtype.Split('|');
        for (int i = 0;i < ImageType.Length;i++)
        {
    
    
            //获取所有视频视频的路径
            string[] dirs = Directory.GetFiles(@"" + LAN,ImageType[i]);
            //Debug.Log(dirs.Length);
            //movieNumber = dirs.Length;
            for (int j = 0;j < dirs.Length;j++)
            {
    
    
                filePaths.Add(dirs[j]);
                movieNumber = j;
                //Debug.Log(movieNumber);
            }
        }
        return fuzhi(t);
    }

    public string[] fuzhi(int t)
    {
    
    
        pic[t] = new string[filePaths.Count];
        for (int i = 0; i < filePaths.Count;i++)
        {
    
    
            pic[t][i] = filePaths[i];
        }
        return pic[t];
    }
}

结尾语

网上开发的各种大神有很多,他们开发出许许多多的插件供我们使用,极大节省了我们的开发时间,在这里向他们表示感谢。我作为一名Unity小菜鸟,希望和大家有问题一起讨论,共同进步,大家有问题可以私聊我。

猜你喜欢

转载自blog.csdn.net/qq_17367039/article/details/128099463