Unity實(shí)現(xiàn)攝像頭錄像功能
前言
在之前的很多展館展示的項(xiàng)目中,甲方有很多要求實(shí)現(xiàn)用攝像頭錄像的功能。使用Unity實(shí)現(xiàn)調(diào)用USB攝像頭畫面的功能非常容易實(shí)現(xiàn),但是實(shí)現(xiàn)錄屏的功能有一些困難,我使用了幾種方法都沒(méi)有實(shí)現(xiàn)出想要的效果,后來(lái)我在網(wǎng)上找到一款叫做AVProMovieCapture的插件,實(shí)現(xiàn)了錄屏的良好效果,同時(shí)也實(shí)現(xiàn)了使用Unity實(shí)現(xiàn)攝像頭錄像的效果,具體實(shí)現(xiàn)方法如下所示:
實(shí)現(xiàn)步驟
1.在項(xiàng)目中導(dǎo)入AVProMovieCapture插件,如下圖所示:
2.在場(chǎng)景中新建plane物體,設(shè)置如下圖所示:3.在場(chǎng)景中拖入ScreenGameObject物體,如下圖所示:
4.在場(chǎng)景中新建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;
//調(diào)用錄像的腳本物體
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>
/// 開(kāi)啟攝像頭
/// </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>
/// 開(kāi)啟攝像頭
/// </summary>
public void beginCamera()
{
for (int i = 0;i<_instances.Length;i++)
{
Instance webcam = _instances[i];
StartWebcam(webcam);
}
}
}
}
5.運(yùn)行場(chǎng)景,發(fā)現(xiàn)已經(jīng)調(diào)用了攝像頭,如下圖所示:
6.雖然調(diào)用了攝像頭,但是不知道是否已經(jīng)進(jìn)行了錄像,查找到工程下的movie文件夾,發(fā)現(xiàn)已經(jīng)錄入了視頻,從而實(shí)現(xiàn)了使用usb攝像頭錄像的功能,如下圖所示:
7.實(shí)現(xiàn)錄像功能,有的需求還需要獲取到這些視頻并且展示出來(lái),這個(gè)也在我之前的項(xiàng)目實(shí)現(xiàn)了,具體怎么實(shí)現(xiàn)不再贅述了,在這里將核心代碼分享在這里:文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-404874.html
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;
//遍歷的視頻數(shù)量
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];
}
}
結(jié)尾語(yǔ)
網(wǎng)上開(kāi)發(fā)的各種大神有很多,他們開(kāi)發(fā)出許許多多的插件供我們使用,極大節(jié)省了我們的開(kāi)發(fā)時(shí)間,在這里向他們表示感謝。我作為一名Unity小菜鳥,希望和大家有問(wèn)題一起討論,共同進(jìn)步,大家有問(wèn)題可以私聊我。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-404874.html
到了這里,關(guān)于Unity實(shí)現(xiàn)攝像頭錄像功能的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!