本文主要是介绍基于Texture2D 实现Unity 截屏功能,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
实现
截屏
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)https://www.jianshu.com/p/460803bbd5a9
这篇关于基于Texture2D 实现Unity 截屏功能的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!