Unity3d屏幕截图方法
多数项目中都会有游戏屏幕截图这个功能存在,那么在开发过程中,这个屏幕截图功能是怎么实现的呢,下面就给大家介绍下三种实现屏幕截图的方法。
方法一:在Unity的API中,unity给我们提供了一个现成的API : Application.CaptureScreenshot(imagename)。但是这个API虽然简单,在PC、mac运用没有多大的影响,但是如果是在移动平台上使用的话就显得相当的吃力,因为它会消耗我们很大的CUP,所以你在移动端使用它截屏的时候会发现很长一段的卡顿现象。但是不得不说使用起来非常的方便,但是在我们使用这个API截图后的截图存放在哪儿呢?很多新朋友可能不是很清楚,当然不同的平台它的存放路径是有差别的。下面是各个平台的截图存放路径:
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";
方法二:使用Texture2d类下的相关方法,通过读取屏幕缓存然后转化为Png图片进行截图。(当然截图存储路径你可以自己设置)
/// <summary> /// Captures the screenshot2. /// </summary> /// <returns>The screenshot2.</returns> /// <param name="rect">Rect.截图的区域,左下角为o点</param> Texture2D CaptureScreenshot2(Rect rect) { // 先创建一个的空纹理,大小可根据实现需要来设置 Texture2D screenShot = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGB24,false); // 读取屏幕像素信息并存储为纹理数据, screenShot.ReadPixels(rect, 0, 0); screenShot.Apply(); // 然后将这些纹理数据,成一个png图片文件 byte[] bytes = screenShot.EncodeToPNG(); string filename = Application.dataPath + "/Screenshot.png"; System.IO.File.WriteAllBytes(filename, bytes); Debug.Log(string.Format("截屏了一张图片: {0}", filename)); // 最后,我返回这个Texture2d对象,这样我们直接,所这个截图图示在游戏中,当然这个根据自己的需求的。 return screenShot; }
CaptureScreenshot2( new Rect( Screen.width*0f, Screen.height*0f, Screen.width*1f, Screen.height*1f));
截中间4分之(如下图):
CaptureScreenshot2( new Rect( Screen.width*0.25f, Screen.height*0.25f, Screen.width*0.5f, Screen.height*0.5f));
这里使用了几个Texture2d类的方法,使用上也有一些要注意的地方,自己看吧。
当然,这个方法也不要到实现针对某个相机的截图的功能。不过关键接口已经出现了,它就是Texture2d.ReadPixels(),这段就不说了,接着往下看吧!方法三:可以针对某个相机进行截图。
这样的话,我就可截下,我的Avatar在游戏中场景中所看的画面了,UI界面(用一个专门的camera显示)什么的是不应该有的。要做到这一点,我们应该将分出一个camera来专门显示ui界面,用另一个camera相机来场景显示场景画面。然后,我们只对场景相机进行截屏就是了。所以这关键点就是:如何实现对某个相机进行截屏了。这里用到一个新的类是RenderTexture。
代码如下:
/// <summary> /// 对相机截图。 /// </summary> /// <returns>The screenshot2.</returns> /// <param name="camera">Camera.要被截屏的相机</param> /// <param name="rect">Rect.截屏的区域</param> Texture2D CaptureCamera(Camera camera, Rect rect) { // 创建一个RenderTexture对象 RenderTexture rt = new RenderTexture((int)rect.width, (int)rect.height, 0); // 临时设置相关相机的targetTexture为rt, 并手动渲染相关相机 camera.targetTexture = rt; camera.Render(); //ps: --- 如果这样加上第二个相机,可以实现只截图某几个指定的相机一起看到的图像。 //ps: camera2.targetTexture = rt; //ps: camera2.Render(); //ps: ------------------------------------------------------------------- // 激活这个rt, 并从中中读取像素。 RenderTexture.active = rt; Texture2D screenShot = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGB24,false); screenShot.ReadPixels(rect, 0, 0);// 注:这个时候,它是从RenderTexture.active中读取像素 screenShot.Apply(); // 重置相关参数,以使用camera继续在屏幕上显示 camera.targetTexture = null; //ps: camera2.targetTexture = null; RenderTexture.active = null; // JC: added to avoid errors GameObject.Destroy(rt); // 最后将这些纹理数据,成一个png图片文件 byte[] bytes = screenShot.EncodeToPNG(); string filename = Application.dataPath + "/Screenshot.png"; System.IO.File.WriteAllBytes(filename, bytes); Debug.Log(string.Format("截屏了一张照片: {0}", filename)); return screenShot; }
多的我就不说了,相关知识自己去找资料吧,因为我也不懂!
直接上图了。
无ui的全屏图:
只有ui的全屏图:
有ui有场景的全屏图(只指定这两个相机哦,相关提示在代码的“//ps”中):