前一段时间在ios/Android做微博和微信的分享图片功能,一开始觉得很快就能做好,最后没想到卡在了屏幕截图这块!就是在截图的时候,会卡1-2秒
一开始用的方法就是Unity3d本身的截屏函数Application.CaptureScreenshot
这个是截取你整个屏幕并存在当前目录下,用这个截屏明显感觉屏幕会卡一下,
之后又试了好多方法,比如想直接用x-code/Eclipse中的截屏功能并且保存等等都或多或少与我预期想的效果不一样,最后在一个朋友的提点下选用了RenderTexture(这个方法其实就是类似与制作小地图功能,但就是当时没想到
)
void ShotThumb(int _multiple, string _name , int _width = 130 , int _height = 200)
{
RenderTexture rt = new RenderTexture(_width * _multiple, _height * _multiple, 24);
Color32[] thumbPixels;
int width = rt.width ;
int height = rt.height;
Texture2D thumb2d = new Texture2D(width, height, TextureFormat.RGB24, false);
showCamera.targetTexture = rt;
showCamera.Render();
RenderTexture.active = rt;
thumb2d.ReadPixels(new Rect(0, 0, width, height), 0, 0);
thumbPixels = thumb2d.GetPixels32();
thumb2d.SetPixels32(thumbPixels);
thumb2d.Apply();
byte[] b = thumb2d.EncodeToPNG();
string path = Path.Combine(Application.dataPath + Path.AltDirectorySeparatorChar, _name + "_thumb.jpg");
Debug.Log(path);
File.WriteAllBytes(path, b);
}
这样之前的问题就解决了 ,截屏的时候不会再出现卡的现象
一开始用的方法就是Unity3d本身的截屏函数Application.CaptureScreenshot
这个是截取你整个屏幕并存在当前目录下,用这个截屏明显感觉屏幕会卡一下,
之后又试了好多方法,比如想直接用x-code/Eclipse中的截屏功能并且保存等等都或多或少与我预期想的效果不一样,最后在一个朋友的提点下选用了RenderTexture(这个方法其实就是类似与制作小地图功能,但就是当时没想到
void ShotThumb(int _multiple, string _name , int _width = 130 , int _height = 200)
{
RenderTexture rt = new RenderTexture(_width * _multiple, _height * _multiple, 24);
Color32[] thumbPixels;
int width = rt.width ;
int height = rt.height;
Texture2D thumb2d = new Texture2D(width, height, TextureFormat.RGB24, false);
showCamera.targetTexture = rt;
showCamera.Render();
RenderTexture.active = rt;
thumb2d.ReadPixels(new Rect(0, 0, width, height), 0, 0);
thumbPixels = thumb2d.GetPixels32();
thumb2d.SetPixels32(thumbPixels);
thumb2d.Apply();
byte[] b = thumb2d.EncodeToPNG();
string path = Path.Combine(Application.dataPath + Path.AltDirectorySeparatorChar, _name + "_thumb.jpg");
Debug.Log(path);
File.WriteAllBytes(path, b);
}
这样之前的问题就解决了 ,截屏的时候不会再出现卡的现象




