Unity 常用脚本:ScreenCapture

ScreenCapture

截图功能。

public static void CaptureScreenshot(string filename);

public static void CaptureScreenshot(string filename, [DefaultValue("1")] int superSize); 

摘要:以PNG文件的形式捕获路径文件名的屏幕截图。 

参数:

  • filename:将截屏文件保存到的位置。
  • superSize:提高分辨率的因素。
ScreenCapture.CaptureScreenshot(Application.dataPath + "/aaa.png");

public static Texture2D CaptureScreenshotAsTexture(int superSize = 1); 

摘要:将游戏视图的屏幕截图捕获到Texture2D对象中。 

参数: 

  • superSize:提高分辨率的因素。
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用Unity自带的ScreenCapture和Microphone类来实现录制屏幕和麦克风的功能。以下是一个简单的示例代码: ``` using UnityEngine; using System.Collections; using System.IO; public class Recorder : MonoBehaviour { public int frameRate = 30; public int width = 1920; public int height = 1080; public string outputPath = "output.mp4"; private bool isRecording = false; private float startTime; private string tempPath; void Start () { tempPath = Path.Combine(Application.temporaryCachePath, "temp.mp4"); } void Update () { if (Input.GetKeyDown(KeyCode.R)) { if (!isRecording) { StartRecording(); } else { StopRecording(); } } } void StartRecording () { isRecording = true; startTime = Time.time; Screen.SetResolution(width, height, false); Application.targetFrameRate = frameRate; Microphone.Start(null, true, 10, AudioSettings.outputSampleRate); } void StopRecording () { isRecording = false; Screen.SetResolution(Screen.currentResolution.width, Screen.currentResolution.height, true); Application.targetFrameRate = -1; Microphone.End(null); StartCoroutine(EncodeVideo()); } IEnumerator EncodeVideo () { yield return new WaitForEndOfFrame(); var audioClip = Microphone.GetClip(null, false); var videoEncoder = new VideoEncoder(tempPath, width, height, frameRate); var audioEncoder = new AudioEncoder(tempPath, audioClip); var videoThread = new System.Threading.Thread(videoEncoder.Encode); var audioThread = new System.Threading.Thread(audioEncoder.Encode); videoThread.Start(); audioThread.Start(); videoThread.Join(); audioThread.Join(); File.Move(tempPath, outputPath); Debug.Log("Video saved to " + outputPath); } } public class VideoEncoder { private string outputPath; private int width; private int height; private int frameRate; public VideoEncoder (string outputPath, int width, int height, int frameRate) { this.outputPath = outputPath; this.width = width; this.height = height; this.frameRate = frameRate; } public void Encode () { var args = string.Format("-y -f rawvideo -pix_fmt rgba -s {0}x{1} -r {2} -i - -c:v libx264 -preset ultrafast -crf 0 {3}", width, height, frameRate, outputPath); var process = new System.Diagnostics.Process(); process.StartInfo.FileName = "ffmpeg"; process.StartInfo.Arguments = args; process.StartInfo.UseShellExecute = false; process.StartInfo.RedirectStandardInput = true; process.StartInfo.RedirectStandardOutput = true; process.StartInfo.RedirectStandardError = true; process.Start(); var stdin = process.StandardInput.BaseStream; var stdout = process.StandardOutput.BaseStream; var stderr = process.StandardError.BaseStream; var buffer = new byte[width * height * 4]; while (true) { var time = Time.time - startTime; if (time < 0) { continue; } var frame = Mathf.FloorToInt(time * frameRate); if (frame >= 0 && frame < maxFrames) { var rect = new Rect(0, 0, width, height); var tex = new Texture2D(width, height, TextureFormat.RGBA32, false); tex.ReadPixels(rect, 0, 0); tex.Apply(); var pixels = tex.GetRawTextureData(); stdin.Write(pixels, 0, pixels.Length); Destroy(tex); } else if (frame >= maxFrames) { break; } } stdin.Close(); process.WaitForExit(); Debug.Log(process.StandardError.ReadToEnd()); } } public class AudioEncoder { private string outputPath; private AudioClip audioClip; public AudioEncoder (string outputPath, AudioClip audioClip) { this.outputPath = outputPath; this.audioClip = audioClip; } public void Encode () { var args = string.Format("-y -f s16le -ar {0} -ac 1 -i - -c:a aac -b:a 256k {1}", audioClip.frequency, outputPath); var process = new System.Diagnostics.Process(); process.StartInfo.FileName = "ffmpeg"; process.StartInfo.Arguments = args; process.StartInfo.UseShellExecute = false; process.StartInfo.RedirectStandardInput = true; process.StartInfo.RedirectStandardOutput = true; process.StartInfo.RedirectStandardError = true; process.Start(); var stdin = process.StandardInput.BaseStream; var stdout = process.StandardOutput.BaseStream; var stderr = process.StandardError.BaseStream; var buffer = new float[audioClip.samples * audioClip.channels]; audioClip.GetData(buffer, 0); var bytes = new byte[buffer.Length * 2]; for (int i = 0; i < buffer.Length; i++) { var sample = Mathf.Clamp01(buffer[i]); var value = (short)(sample * 32767); bytes[i * 2] = (byte)(value & 0xff); bytes[i * 2 + 1] = (byte)(value >> 8); } stdin.Write(bytes, 0, bytes.Length); stdin.Close(); process.WaitForExit(); Debug.Log(process.StandardError.ReadToEnd()); } } ``` 这个脚本使用了ffmpeg来编码视频和音频,需要在系统中安装ffmpeg并将其添加到环境变量中。录制视频时,可以按下R键开始/停止录制,录制完成后会保存为一个MP4文件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值