Files
webRtc/Assets/Script/Recorder/EditorGameViewRecorder.cs

99 lines
3.2 KiB
C#
Raw Permalink Normal View History

2026-05-27 00:05:32 +08:00
#if UNITY_EDITOR
using System;
using System.IO;
using RenderStreaming;
using Stary.Evo;
using UnityEngine;
using UnityEditor.Recorder;
using UnityEditor.Recorder.Input;
public class EditorGameViewRecorder : IVideoRecorder, IController
{
private RecorderController recorderController;
public bool IsRecording => recorderController != null && recorderController.IsRecording();
/// <summary> Save the video to Application.persistentDataPath. </summary>
/// <value> The full pathname of the video save file. </value>
private string VideoSaveExtension => Path.Combine(Application.persistentDataPath, "Recording");
private string VideoSavePath
{
get
{
var timeStamp = DateTime.Now.ToString("yyyy-MM-ddTHH-mm-ss");
var filename =
$"meeting-recording-{this.GetSystem<IGlobalConfigSystem>().GetConnectionId()}-{timeStamp}";
return Path.Combine(Application.persistentDataPath, VideoSaveExtension, filename);
}
}
private string _videoSavePath;
public Action OnStartedRecordingVideo { get; set; }
public Action<string> OnStoppedRecordingVideoAction { get; set; }
public void StartRecording()
{
if (IsRecording)
return;
_videoSavePath = VideoSavePath;
var controllerSettings = ScriptableObject.CreateInstance<RecorderControllerSettings>();
recorderController = new RecorderController(controllerSettings);
var movieSettings = ScriptableObject.CreateInstance<MovieRecorderSettings>();
movieSettings.name = "Game View MP4 Recorder";
movieSettings.Enabled = true;
movieSettings.OutputFormat = MovieRecorderSettings.VideoRecorderOutputFormat.MP4;
movieSettings.OutputFile = _videoSavePath;
movieSettings.CaptureAudio = false;
movieSettings.ImageInputSettings = new GameViewInputSettings
{
OutputWidth = 1920,
OutputHeight = 1080
};
controllerSettings.AddRecorderSettings(movieSettings);
controllerSettings.SetRecordModeToManual();
controllerSettings.FrameRate = 30.0f;
controllerSettings.CapFrameRate = true;
recorderController.PrepareRecording();
var started = recorderController.StartRecording();
if (!started)
{
Debug.LogError("Editor Recorder 启动失败,请确认已进入 Play Mode并安装 com.unity.recorder");
recorderController = null;
return;
}
Debug.Log($"Editor 开始录制: {_videoSavePath}");
OnStartedRecordingVideo?.Invoke();
}
public void StopRecording()
{
if (!IsRecording)
{
Debug.LogError("当前没有正在进行的 Editor 录制");
return;
}
recorderController.StopRecording();
if (!File.Exists(_videoSavePath))
OnStoppedRecordingVideoAction?.Invoke(_videoSavePath + ".mp4");
else
Debug.LogError($"Editor MP4 文件不存在,可能还在写入或输出路径变化: {_videoSavePath}");
recorderController = null;
}
public IArchitecture GetArchitecture()
{
return MainArchitecture.Interface;
}
}
#endif