好的,以下是实现上述功能的代码示例: ```csharp using UnityEngine; using System.IO; using System.Collections; public class ScreenRecorder : MonoBehaviour { private bool isRecording = false; private string savePath = ""; // 视频保存路径 private string tempPath = ""; // 临视频保存路径 private MovieTexture movieTexture = null; void Start () { // 生成视频保存路径 savePath = Application.persistentDataPath + "/" + System.DateTime.Now.ToString("yyyyMMddHHmmss") + ".mp4"; tempPath = Application.temporaryCachePath + "/tempVideo.mp4"; } void Update () { // 键盘输入 R 开始/停止录制 if (Input.GetKeyDown(KeyCode.R)) { if (isRecording) { StopRecording(); } else { StartRecording(); } } } // 开始录制视频 void StartRecording() { isRecording = true; // 隐藏所有UI Canvas[] uiCanvas = FindObjectsOfType<Canvas>(); foreach (Canvas canvas in uiCanvas) { canvas.enabled = false; } // 开始录制 Handheld.PlayFullScreenMovie(tempPath, Color.black, FullScreenMovieControlMode.CancelOnInput, FullScreenMovieScalingMode.AspectFit); } // 停止录制视频并且保存 void StopRecording() { isRecording = false; // 显示所有UI Canvas[] uiCanvas = FindObjectsOfType<Canvas>(); foreach (Canvas canvas in uiCanvas) { canvas.enabled = true; } // 拷贝视频文件 File.Copy(tempPath, savePath, true); // 删除临文件 File.Delete(tempPath); // 回放视频 StartCoroutine(PlayVideo()); } // 回放视频 IEnumerator PlayVideo() { movieTexture = new MovieTexture(); movieTexture.Stop(); WWW videoFile = new WWW("file://" + savePath); while (!videoFile.isDone) { yield return null; } movieTexture = videoFile.movie; AudioClip audioClip = movieTexture.audioClip; AudioSource audioSource = gameObject.GetComponent<AudioSource>(); if (audioClip != null && audioSource != null) { audioSource.clip = audioClip; audioSource.playOnAwake = false; audioSource.loop = false; audioSource.Play(); } Renderer renderer = gameObject.GetComponent<Renderer>(); if (renderer != null) { Material material = renderer.material; if (material != null) { material.mainTexture = movieTexture; movieTexture.loop = true; if (!movieTexture.isPlaying) { audioSource.Play(); movieTexture.Play(); } } } } } ``` 这段代码利用 Unity 提供的接口实现了在 App 启动录制 App 的画面,并在 App 关闭后将录制的视频保存到本地。其中,通过输入 R 来开始或停止录制视频;利用 PlayFullScreenMovie 函数录制屏幕,并将视频暂保存到临文件中;利用 File 类的 Copy 函数将临文件复制到指定的保存路径,并删除临文件;最后通过 MovieTexture 实现视频的回放。