基于Texture2D 实现Unity 截屏功能

作者 : admin 本文共600个字,预计阅读时间需要2分钟 发布时间: 2024-06-9 共5人阅读

实现

截屏

Texture2D texture = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);
texture.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
texture.Apply();

存储 

byte[] array = ImageConversion.EncodeToPNG(texture);
if (!Directory.Exists(filePath))
{
    Directory.CreateDirectory(filePath);
}
File.WriteAllBytes(filePath + "/" + fileName, array);

问题 

ReadPixels was called to read pixels from system frame buffer, while not inside drawing frame.

直接使用截屏会出错,可使用协程或在OnPostRender 中处理来解决,具体参见链接文章。

截屏 ReadPixels was called to read pixels from system frame buffer, while not inside drawing frame. – 简书 (jianshu.com)基于Texture2D 实现Unity 截屏功能插图http://www.jianshu.com/p/460803bbd5a9

本站无任何商业行为
个人在线分享 » 基于Texture2D 实现Unity 截屏功能
E-->