方法一:
在unity的API中,unity给咱们提供了一个现成的API : Application.CaptureScreenshot(imagename),可是在咱们使用这个API截图后的截图存放在哪儿呢?不少新朋友可能不是很清楚,固然不一样的平台它的存放路径是有差异的。若是你想要你的游戏中显示你截图的缩略图,那么这种方法不是一个好方法,由于你要用 WWW去加载你刚才的截图,这会消耗你一部分的时间。
下面是各个平台的截图存放路径:缓存
Application.CaptureScreenshot(screencapture.png) if(Application.platform==RuntimePlatform.Android || Application.platform==RuntimePlatform.IPhonePlayer) imagePath=Application.persistentDataPath; else if(Application.platform==RuntimePlatform.WindowsPlayer) imagePath=Application.dataPath; else if(Application.platform==RuntimePlatform.WindowsEditor) { imagePath=Application.dataPath; imagePath= imagePath.Replace("/Assets",null); } imagePath = imagePath + "/screencapture.png";
方法二:
经过读取屏幕缓存而后转化为Png图片进行截图,并可直接使用png图片做为缩略图。(截图存储路径你能够本身设置)
code
IEnumerator GetCapture() { yield return new WaitForEndOfFrame(); int width = Screen.width; int height = Screen.height; Texture2D tex = new Texture2D(width,height,TextureFormat.RGB24,false); tex.ReadPixels(new Rect(0,0,width,height),0,0,true); byte[] imagebytes = tex.EncodeToPNG();//转化为png图 tex.Compress(false);//对屏幕缓存进行压缩 image.mainTexture = tex;//对屏幕缓存进行显示(缩略图) File.WriteAllBytes(Application.dataPath + "/screencapture.png",imagebytes);//存储png图 }