目录
Unity 工具 之 插件实现录频、拍照jpg、生成Gif动图功能(适用移动端)
一、简单介绍
二、插件导入使用
三、几个 Demo 演示效果
四、关键代码
五、下载地址
Unity 工具类,自己整理的一些游戏开发可能用到的模块,单独独立使用,方便游戏开发。
本节介绍,使用Unity插件,进行屏幕录制,生成图片,gif 动图,以及mp4文件等;能录制场景,也能录制摄像头,多功能录制,也适用于移动端。
1、打开Unity,新建工程
2、导入 Natcorder 插件
3、几个场景使用 demo 如下
1、Giffy.scene(拍摄场景画面动图,可带摄像头画面)
2、JPG.scene(拍摄场景画面)
3、ReplayCam.scene(拍摄录制指定场景 camera 的画面)
(保存为MP4文件,可以到指定路径查看文件(移动端的话,demo中,会自动预览视频))
4、WebCam .scene(拍摄录制摄像头的画面)
(保存为MP4文件,可以到指定路径查看文件(移动端的话,demo中,会自动预览视频))
1、Giffy.cs
/*
* NatCorder
* Copyright (c) 2020 Yusuf Olokoba
*/
namespace NatSuite.Examples {
using UnityEngine;
using Recorders;
using Recorders.Clocks;
using Recorders.Inputs;
public class Giffy : MonoBehaviour {
[Header("GIF Settings")]
public int imageWidth = 640;
public int imageHeight = 480;
public float frameDuration = 0.1f; // seconds
private GIFRecorder recorder;
private CameraInput cameraInput;
public void StartRecording () {
// Start recording
recorder = new GIFRecorder(imageWidth, imageHeight, frameDuration);
cameraInput = new CameraInput(recorder, new RealtimeClock(), Camera.main);
// Get a real GIF look by skipping frames
cameraInput.frameSkip = 4;
}
public async void StopRecording () {
// Stop the recording
cameraInput.Dispose();
var path = await recorder.FinishWriting();
Debug.Log($"Saved animated GIF image to: {path}");
var prefix = Application.platform == RuntimePlatform.IPhonePlayer ? "file://" : "";
Application.OpenURL($"{prefix}{path}");
}
}
}
2、JPG.cs
using NatSuite.Recorders;
using NatSuite.Recorders.Clocks;
using NatSuite.Recorders.Inputs;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class JPG : MonoBehaviour
{
[Header("GIF Settings")]
public int imageWidth = 640;
public int imageHeight = 480;
private JPGRecorder recorder;
private CameraInput cameraInput;
public Camera cam;
private bool isPhoto = false;
public void StartRecording()
{
if (isPhoto == false) {
isPhoto = true;
// Start recording
recorder = new JPGRecorder(imageWidth, imageHeight);
cameraInput = new CameraInput(recorder, new RealtimeClock(), cam);
cameraInput.frameSkip = 40;
Debug.Log(" StartRecording ");
}
}
public async void StopRecording()
{
isPhoto = false;
// Stop the recording
cameraInput.Dispose();
var path = await recorder.FinishWriting();
Debug.Log($"Saved animated jpg image to: {path}");
var prefix = Application.platform == RuntimePlatform.IPhonePlayer ? "file://" : "";
Application.OpenURL($"{prefix}{path}");
}
}
3、ReplayCam.cs
using UnityEngine;
using System.Collections;
using Recorders;
using Recorders.Clocks;
using Recorders.Inputs;
public class ReplayCam : MonoBehaviour {
[Header("Recording")]
public int videoWidth = 1280;
public int videoHeight = 720;
public bool recordMicrophone;
private IMediaRecorder recorder;
private CameraInput cameraInput;
private AudioInput audioInput;
private AudioSource microphoneSource;
public AudioListener audioListener;
public Camera cam;
private IEnumerator Start () {
// Start microphone
microphoneSource = gameObject.AddComponent();
microphoneSource.mute =
microphoneSource.loop = true;
microphoneSource.bypassEffects =
microphoneSource.bypassListenerEffects = false;
if(recordMicrophone)
microphoneSource.clip = Microphone.Start(null, true, 10, AudioSettings.outputSampleRate);
yield return new WaitUntil(() => Microphone.GetPosition(null) > 0);
microphoneSource.Play();
}
private void OnDestroy () {
// Stop microphone
if (recordMicrophone)
{
microphoneSource.Stop();
Microphone.End(null);
}
}
public void StartRecording () {
// Start recording
var frameRate = 30;
//var sampleRate = recordMicrophone ? AudioSettings.outputSampleRate : 0;
//var channelCount = recordMicrophone ? (int)AudioSettings.speakerMode : 0;
var sampleRate = AudioSettings.outputSampleRate;
var channelCount = (int)AudioSettings.speakerMode ;
var clock = new RealtimeClock();
recorder = new MP4Recorder(videoWidth, videoHeight, frameRate, sampleRate, channelCount);
// Create recording inputs
cameraInput = new CameraInput(recorder, clock, cam);
//audioInput = recordMicrophone ? new AudioInput(recorder, clock, microphoneSource, true) : null;
audioInput = new AudioInput(recorder, clock, audioListener);
// Unmute microphone
microphoneSource.mute = audioInput == null;
}
public async void StopRecording () {
// Mute microphone
microphoneSource.mute = true;
// Stop recording
audioInput?.Dispose();
cameraInput.Dispose();
var path = await recorder.FinishWriting();
// Playback recording
Debug.Log($"Saved recording to: {path}");
var prefix = Application.platform == RuntimePlatform.IPhonePlayer ? "file://" : "";
// 移动端,录频完后,播放录制的视频
Handheld.PlayFullScreenMovie($"{prefix}{path}");
}
}
4、WebCam.cs
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using Recorders;
using Recorders.Clocks;
using Recorders.Inputs;
public class WebCam : MonoBehaviour {
public RawImage rawImage;
public AspectRatioFitter aspectFitter;
private WebCamTexture webCamTexture;
private MP4Recorder recorder;
private WebCamTextureInput webCamTextureInput;
private IEnumerator Start () {
// Request camera permission
yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);
if (!Application.HasUserAuthorization(UserAuthorization.WebCam))
yield break;
// Start the WebCamTexture
webCamTexture = new WebCamTexture(1280, 720, 30);
webCamTexture.Play();
// Display webcam
yield return new WaitUntil(() => webCamTexture.width != 16 && webCamTexture.height != 16); // Workaround for weird bug on macOS
rawImage.texture = webCamTexture;
aspectFitter.aspectRatio = (float)webCamTexture.width / webCamTexture.height;
}
public void StartRecording () {
// Start recording
var clock = new RealtimeClock();
recorder = new MP4Recorder(webCamTexture.width, webCamTexture.height, 30);
webCamTextureInput = new WebCamTextureInput(recorder, clock, webCamTexture);
}
public async void StopRecording () {
// Stop recording
webCamTextureInput.Dispose();
var path = await recorder.FinishWriting();
// Playback recording
Debug.Log($"Saved recording to: {path}");
var prefix = Application.platform == RuntimePlatform.IPhonePlayer ? "file://" : "";
//Handheld.PlayFullScreenMovie($"{prefix}{path}");
}
}
建议购买正版插件
Unity Asset Store:https://assetstore.unity.com/packages/tools/integration/natcorder-video-recording-api-102645
CSDN 下载地址:https://download.csdn.net/download/u014361280/12488838