Unity三种截图方法

来源:互联网 发布:淘宝自动回复 编辑:程序博客网 时间:2024/05/26 07:29

转自:http://blog.csdn.net/anyuanlzh/article/details/17008909,请点击链接查看原文,尊重楼主版权。

1.Application类下的CaptureScreenshot方法

void CaptureScreen()   {      Application.CaptureScreenshot("Screenshot.png", 0);  }  
这个方法,截取的是某一帧时整个游戏的画面,或者说是全屏截图吧。
a、不能针对某一个相机(camera)的画面,进行截图。
b、对局部画面截图,实现起来不方便,效率也低,不建议在项目中使用。

c.图片是在Application.CaptureScreenshot调用完成后一帧,才要被保存下来的,所以你要在Application.CaptureScreenshot调用完成后的下一帧,才能去使用它保存下来的图片。不然你会发现找不到图片的。


2.Texture2d类下相关方法

/// <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;  }  
使用:

截全屏(有ui)

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));


3.RenderTexture类相关

这第三个方法,最牛了,可以针对某个相机进行截图

这样的话,我就可截下,我的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”中):




总调用脚本:

转自:http://blog.csdn.net/lyh916/article/details/48244471

实现需要来设置            Texture2D screenShot = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGB24, false);            // 读取屏幕像素信息并存储为纹理数据          yield return new WaitForEndOfFrame();          screenShot.ReadPixels(rect, 0, 0, false);          screenShot.Apply();            // 然后将这些纹理数据,成一个png图片文件            byte[] bytes = screenShot.EncodeToPNG();          string filename = Application.dataPath + "/Screenshot2.png";          System.IO.File.WriteAllBytes(filename, bytes);            AssetDatabase.Refresh();          Debug.Log(string.Format("截屏了一张图片: {0}", filename));      }        // 读取RenderTexture像素      Texture2D Capture3(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 + "/Screenshot3.png";          System.IO.File.WriteAllBytes(filename, bytes);          AssetDatabase.Refresh();          Debug.Log(string.Format("截屏了一张照片: {0}", filename));            return screenShot;      }   }  




0 0
原创粉丝点击