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);