Unity3d之截图方法
http://blog.csdn.net/highning0007/article/details/37991787
下面是我总结的、在u3d中的,三种截屏方法:
1、使用Application类下的CaptureScreenshot方法。
- void CaptureScreen()
- {
- Application.CaptureScreenshot("Screenshot.png", 0);
- }
这个方法,截取的是某一帧时整个游戏的画面,或者说是全屏截图吧。
a、不能针对某一个相机(camera)的画面,进行截图。
b、对局部画面截图,实现起来不方便,效率也低,不建议在项目中使用:
虽然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));

这里使用了几个Texture2d类的方法,使用上也有一些要注意的地方,自己看吧。
当然,这个方法也不要到实现针对某个相机的截图的功能。不过关键接口已经出现了,它就是Texture2d.ReadPixels(),这段就不说了,接着往下看吧!
3、这第三个方法,最牛了,可以针对某个相机进行截图。
这样的话,我就可截下,我的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/anyuanlzh/article/details/17008909
Unity3d之截图方法的更多相关文章
- Unity3d中Update()方法的替身
在网上看到一些资料说Unity3d的Update方法是如何如何不好,影响性能.作为一个菜鸟,之前我还觉得挺好用的,完全没用什么影响性能的问题存在.现在发现确实有很大的问题,我习惯把一大堆检测判断放在U ...
- python web自动化测试中失败截图方法汇总
在使用web自动化测试中,用例失败则自动截图的网上也有,但实际能落地的却没看到,现总结在在实际应用中失败截图的几种方法: 一.使用unittest框架截图方法: 1.在tearDown中写入截图的 ...
- selenium-java,UI自动化截图方法
截图方法: import java.io.File; import java.io.IOException; import org.apache.commons.io.FileUtils; impor ...
- C#截图操作(几种截图方法)
公共函数获取屏幕截图private Bitmap GetScreenCapture(){ Rectangle tScreenRect = new Rectangle(0, 0, Screen.Prim ...
- Java&Selenium截图方法封装
Java&Selenium截图方法封装 package util; import org.apache.commons.io.FileUtils; import org.openqa.sele ...
- Python+Appium自动化测试(7)-截图方法
一,selenium模块的两种截图方法 get_screenshot_as_file(filename) 参数filename为截图文件保存的绝对路径,如: driver.get_screenshot ...
- (转)Unity3d的3种截图方法
下面是我总结的.在u3d中的,三种截屏方法: 1.使用Application类下的CaptureScreenshot方法. void CaptureScreen() { Application.Cap ...
- unity3d中dllimport方法的使用,以接入腾讯平台为例!!!
说到有关dllimport方法可能还有很多人比较陌生,其实我自己也说不太清楚,大概说说什么时候要用它. 事实上功能类似于调用android的第三包,我们想要使用苹果上特定的api或者第三方平台的一些东 ...
- Python + Appium 获取当前屏幕的截图方法的封装
使用方法:get_screenshot_as_file(filename),来自于selenium\webdriver\remote\webdiver.py def take_screenShot(s ...
随机推荐
- 判断Javascript变量是否为空 undefined 或者null(附样例)
1.变量申明未赋值 var type; //type 变量未赋值 1. type==undefined //true 2. type===undefined //true 3. typeof(type ...
- 用命令打开本地tomcat服务器
1.点击开始菜单,搜索cmd,默认第一个结果是cmd.exe, 鼠标右键用管理员权限打开(win7及以上版本系统) 启动命令是net start tomcat8 (我电脑是tomcat8,如果是tom ...
- spring mvc 校验@NULL
一需要的包 1 validation-api-1.0.0.GA.jar:JDK的接口: 2 hibernate-validator-4.2.0.Final.jar 是对上述接口的实现: 二 若在pom ...
- vue系列之核心思想
1.数据驱动 只要改变数据,Vuejs会通过Directives指令对DOM进行封装,当数据发生变化,会通知相应的DOM进行变化 Vuejs会对DOM进行监听,通过DOMListeners监听视图的变 ...
- java多线程快速入门(六)
多线程应用实例(批量发送短信) 1.创建实体类 package com.cppdy; public class UserEntity { private int id; private String ...
- hive遇到FAILED: Execution Error, return code 2 from org.apache.hadoop.hive.ql.exec.mr.MapRedTask错误
hive遇到FAILED: Execution Error, return code 2 from org.apache.hadoop.hive.ql.exec.mr.MapRedTask错误 起因 ...
- 基于 OSGi 的面向服务的组件编程,helloworld
基于 OSGi 的面向服务的组件编程 OSGi(Open Services Gateway Initiative,开放服务网关协议)提供了一个面向服务组件的编程模型,基于 OSGi 编程,具有模块化, ...
- kotlin 插件更新到 1.2.41 程序出错 Please use kotlin-stdlib-jdk7 instead
buildscript { ext.kotlin_version = '1.2.41' repositories { google() jcenter() } dependencies { class ...
- [转] h5上传视频或文件编写
Html5 finally solves an age old problem of being able to upload files while also showing the upload ...
- 用Delphi从内存流中判断图片格式
https://blog.csdn.net/my98800/article/details/53536774 废话不多说了,利用内存流来判断文件的格式,其实判断文件的前几个字节就可以简单的判断这个文件 ...
