Unity实现摄像头录像功能
创始人
2024-02-28 12:38:18
0

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;//Stateprivate Instance[] _instances;private int _selectedWebcamIndex;//显示视频的面板public MeshRenderer plane;//调用录像的脚本物体public CaptureGUI captureObject;private void Start(){//Create instance data per webcamint 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();instance.capture = captureObject.gameObject.GetComponent();instance.capture._autoFilenamePrefix = "Demo4Webcam-" + i;//instance.gui = go.GetComponent();instance.gui = captureObject.gameObject.GetComponent();instance.gui._showUI = true;_instances[i] = instance;}if (numCams > 0){Change(0);}StartCoroutine(OpenCamera());//captureObject = GameObject.Find("ScreenGameObject(Clone)").GetComponent();}/// /// 开启摄像头/// /// 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);}}/// /// 开启摄像头/// 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 filePaths;public static string[][] pic;private List LAN;private string movieUrl;//遍历的视频数量public static int movieNumber = 0;private void Start(){movieUrl = ConfigTest.dic["录像路径"]["url"];LAN = new List();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 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小菜鸟,希望和大家有问题一起讨论,共同进步,大家有问题可以私聊我。

相关内容

热门资讯

平凡的世界中金波的爱情 平凡的世界中金波的爱情  《平凡的世界》是中国作家路遥创作的一部全景式地表现中国当代城乡社会生活的百...
文言文断句技巧顺口溜 文言文断句技巧顺口溜  导语:我们在给文言文断句的时候要掌握一定的技巧,那么关于这方面的口诀有哪些?...
齐桓公伐楚盟屈完的原文阅读及... 齐桓公伐楚盟屈完的原文阅读及翻译译文  原文阅读:  春,齐侯以诸侯之师侵蔡。蔡溃,遂伐楚。楚子使与...
《假如给我三天光明》阅读练习... 《假如给我三天光明》阅读练习题及答案  (美)海伦.凯勒  ①我们谁都知道自己难免一死。但是这一天的...
形容冬天寒风的词语有哪些 形容冬天寒风的词语有哪些  导语:冬天,小朋友们脸上拂过的是冷冷的冬风,让小朋友们感到非常的寒冷。下...
桃花源记拼音版 桃花源记拼音版  桃(táo)花(huā)源(yuán)记(j&igra...
《溪居即事》翻译赏析 《溪居即事》翻译赏析  《溪居即事》作者为唐朝文学家崔道融。古诗全文如下:  篱外谁家不系船,春风吹...
形容云雾缭绕的语句 形容云雾缭绕的语句  雾竟有如此神奇的力量,它丰富多彩,变幻莫测,是它给这群山增添了无穷的魅力;近处...
离骚注音全文 离骚注音全文  《离骚》中有些字比较偏僻,很多人可能会认错读错,以下是小编整理的离骚注音全文,欢迎参...
弟子规出则悌全文解释 弟子规出则悌全文解释  《弟子规》是清朝康熙时山西绛州人李毓秀所作。下面是小编收集整理的弟子规出则悌...
《归园田居·其一》原文与赏析 《归园田居·其一》原文与赏析  《归园田居·其一》写景而写心,表现见出诗人对污浊官场的鄙夷和厌恶。下...
前后赤壁赋原文 前后赤壁赋原文  《前后赤壁赋》又称《赤壁二赋帖》,是元代书法家赵孟頫于元大德五年(1301)创作的...
《兰亭集序》书法特点 《兰亭集序》书法特点  《兰亭集序》之所以流传千古,不仅因为其立意深远,而且因为其文笔清新流畅,朴素...
《梅花·墙角数枝梅》翻译赏析 《梅花·墙角数枝梅》翻译赏析  “墙角数枝梅”出自宋代诗人王安石的《梅花》。此诗作于王安石第二次罢相...
欧阳修《醉翁亭记》原文与翻译 欧阳修《醉翁亭记》原文与翻译  《醉翁亭记》是北宋文学家欧阳修创作的一篇散文。以下是由小编为您整理的...
春江晚景古诗原文及翻译 春江晚景古诗原文及翻译  古诗,在我们中国的历史上,是一个特别的文化,从古传承至今,已经有了无数年的...
《登楼望水》赏析 《登楼望水》赏析  《登楼望水》是唐代顾况所做的一首诗。下面小编给大家带来《登楼望水》赏析。希望能够...
《老子·四十一章》文言文阅读... 《老子·四十一章》文言文阅读及答案  阅读下面的材料,完成23—24题。(5分)  上士闻道,勤而行...
不要把现实当童话 不要把现实当童话  是的,在现在这个残酷的现实生活中,我总见到很多人一直把现实当成童话。他们总有一个...
出师表 / 前出师表诸葛亮的... 出师表 / 前出师表诸葛亮的文言文原文赏析及翻译  在日常过程学习中,我们总免不了跟文言文打交道,文...