#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(); /// Save the video to Application.persistentDataPath. /// The full pathname of the video save file. 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().GetConnectionId()}-{timeStamp}"; return Path.Combine(Application.persistentDataPath, VideoSaveExtension, filename); } } private string _videoSavePath; public Action OnStartedRecordingVideo { get; set; } public Action OnStoppedRecordingVideoAction { get; set; } public void StartRecording() { if (IsRecording) return; _videoSavePath = VideoSavePath; var controllerSettings = ScriptableObject.CreateInstance(); recorderController = new RecorderController(controllerSettings); var movieSettings = ScriptableObject.CreateInstance(); 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