Unity3D windows平台视频录制录屏插件 UnityRecorder

例子:从官方例子简单改了

using UnityEditor;
using UnityEditor.Recorder;
using UnityEditor.Recorder.Input;
using System;

namespace UnityEngine.Recorder.Examples
{
public enum RecorderControllerState
{
Video,
Animation,
ImageSequence
}

/// <summary>
/// 录制
/// </summary>
public class RecorderExammlpText : MonoBehaviour
{
RecorderController m_RecorderController;

private RecorderControllerState controllerState = RecorderControllerState.Video;
[Header("下面两个单纯观看数据,不用管")]
public RecorderControllerSettings controllerSettings;
public MovieRecorderSettings videoRecorder;

private string animationOutputFolder;
private string mediaOutputFolder;

private void Start()
{
controllerSettings = ScriptableObject.CreateInstance<RecorderControllerSettings>();
m_RecorderController = new RecorderController(controllerSettings);
animationOutputFolder = Application.dataPath + "/SampleRecordings";
mediaOutputFolder = Application.dataPath + "../SampleRecordings";

}

private void Update()
{
if (Input.GetKeyDown(KeyCode.A))
{
StartRecorder();
}
if (Input.GetKeyDown(KeyCode.D))
{
StopRecorder();
}
}

#region 记录视频-结束视频 位置在项目根目录

/// <summary>
/// 开始记录视频
/// </summary>
/// <param name="state">默认视频</param>
public void StartRecorder(RecorderControllerState state = RecorderControllerState.Video)
{

//var outputFolder = Application.dataPath + "/SampleRecordings";
switch (state)
{
case RecorderControllerState.Video:

// Video
RecorderVideo();

break;
case RecorderControllerState.Animation:

// Animation
RecorderAnimation();
break;
case RecorderControllerState.ImageSequence:

// Image Sequence
RecorderImageSequence();
break;

default:
break;
}
// Setup Recording

controllerSettings.SetRecordModeToManual();
controllerSettings.frameRate = 60.0f;

Options.verboseMode = false;
m_RecorderController.StartRecording();
}

/// <summary>
/// 录制视频
/// </summary>
private void RecorderVideo()
{
videoRecorder = ScriptableObject.CreateInstance<MovieRecorderSettings>();
videoRecorder.name = "My Video Recorder";
videoRecorder.enabled = true;

videoRecorder.outputFormat = VideoRecorderOutputFormat.MP4;
videoRecorder.videoBitRateMode = VideoBitrateMode.Low;

// videoRecorder.SetOutput_720p_HD(); GameViewInputSettings 修改屏幕分辨率
videoRecorder.imageInputSettings = new GameViewInputSettings
{
outputWidth = 1920,
outputHeight = 1080
};

videoRecorder.audioInputSettings.preserveAudio = true;
string str = DateTime.Now.Year.ToString()+"_" + DateTime.Now.Month + "_" + DateTime.Now.Day + "_" + DateTime.Now.Hour + "_" + DateTime.Now.Minute + "_" + DateTime.Now.Second;
videoRecorder.outputFile = mediaOutputFolder + "/Magic_" + str;
controllerSettings.AddRecorderSettings(videoRecorder);
}

/// <summary>
/// 动画
/// </summary>
private void RecorderAnimation()
{
var animationRecorder = ScriptableObject.CreateInstance<AnimationRecorderSettings>();
animationRecorder.name = "My Animation Recorder";
animationRecorder.enabled = true;

var sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);

animationRecorder.animationInputSettings = new AnimationInputSettings
{
gameObject = sphere,
recursive = true,
};

animationRecorder.animationInputSettings.AddComponentToRecord(typeof(Transform));

animationRecorder.outputFile = animationOutputFolder + "/animation_" + DefaultWildcard.GeneratePattern("GameObject") + "_" + DefaultWildcard.Take;

controllerSettings.AddRecorderSettings(animationRecorder);
}

/// <summary>
/// 图像序列
/// </summary>
private void RecorderImageSequence()
{
var imageRecorder = ScriptableObject.CreateInstance<ImageRecorderSettings>();
imageRecorder.name = "My Image Recorder";
imageRecorder.enabled = true;

imageRecorder.outputFormat = ImageRecorderOutputFormat.PNG;
imageRecorder.captureAlpha = true;

imageRecorder.outputFile = mediaOutputFolder + "/image_" + DefaultWildcard.Frame + "_" + DefaultWildcard.Take;

imageRecorder.imageInputSettings = new CameraInputSettings
{
source = ImageSource.MainCamera,
outputWidth = 1920,
outputHeight = 1080,
captureUI = true
};

controllerSettings.AddRecorderSettings(imageRecorder);
}

/// <summary>
/// 停止录制
/// </summary>
public void StopRecorder()
{
Debug.Log("停止录制");
m_RecorderController.StopRecording();
controllerSettings.RemoveRecorder(videoRecorder);
}

#endregion


void OnDisable()
{
StopRecorder(http://www.my516.com);

}
}
}

--------------------- 

posted @ 2019-06-18 11:55  水至清明  阅读(5419)  评论(0编辑  收藏  举报