-
Unity3D中使用JPGEncoder.cs进行截图压缩
资源介绍
我在网上找到了JPGEncoder.cs纯代码压缩类
添加函数
IEnumerator CaptureScreenshotJPG(Rect rect)
{
yield return new WaitForEndOfFrame ();
// 先创建一个的空纹理,大小可根据实现需要来设置
TextureFormat aTextureFormat;
;
aTextureFormat = TextureFormat.RGB24;
Texture2D screenShot = new Texture2D ((int)(rect.width), (int)(rect.height), aTextureFormat, false);
// 读取屏幕像素信息并存储为纹理数据,
screenShot.ReadPixels (rect, 0, 0);
screenShot.Apply ();
JPGEncoder encoder = new JPGEncoder (screenShot, 20);//质量1~100
encoder.doEncoding ();
while (!encoder.isDone)
yield return null;
string filename = Application.persistentDataPath + "/" + "screenName.png";
System.IO.File.WriteAllBytes (filename, encoder.GetBytes ());
Debug.Log (string.Format ("截屏了一张图片: {0}", filename));
}