博客
关于我
u3d 巧用 CaptureScreenshot捕捉游戏画面(截图,截屏)
阅读量:268 次
发布时间:2019-03-01

本文共 2264 字,大约阅读时间需要 7 分钟。

孙广东   2015.4.27

游戏中测试人员在测试的时候,我们很希望能他们捕捉到当时的问题瞬间,而不是简单的用语言描述。

通过unity内置的截图功能,也有几种方法:

   1,通过Application.CaptureScreenshot来截图,这种方式最简单,一行代码搞定,缺点也很明显,比如不能选择区域,不能选择图片格式,不能屏蔽某些对象等等;

   2,通过Texture2D.ReadPixels来读取屏幕区域像素,然后通过EncodeToJPG/EncodeToPNG编码,最后创建文件保存,步骤繁琐,但可控性更高;(注意这个 的协程  必须在 OnGUI 中调用才可以

   3,通过一个RenderTexture渲染相机内容,然后读取RenderTexture的像素,然后用2.2的方式实现截图,可控性更高,可以增加各种效果,可以实现屏蔽等功能;

账号Unity提供了这个游戏截屏的功能, 现在我们就来实现一下这个东东吧。
Application.CaptureScreenshot
static void CaptureScreenshot(string filename, int superSize = 0);
 
文件默认被保存在这个路径:persistent data path
那么图片我们存储在哪里呢? 在移动设备上的路径,我们有一下几种:
Application.dataPath:
此属性用于返回程序的数据文件所在文件夹的路径。例如在Editor中就是Assets了。
Application.streamingAssetsPath:
此属性用于返回流数据的缓存目录,返回路径为相对路径,适合设置一些外部数据文件的路径。
Application.persistentDataPath:
此属性用于返回一个持久化数据存储目录的路径,可以在此路径下存储一些持久化的数据文件。这个路径是可读可写的
Application.temporaryCachePath:
此属性用于返回一个临时数据的缓存目录。
android平台
Application.dataPath:
/data/app/xxx.xxx.xxx.apk
Application.streamingAssetsPath:
jar:file:///data/app/xxx.xxx.xxx.apk/!/assets
Application.persistentDataPath:
/data/data/xxx.xxx.xxx/files
Application.temporaryCachePath:
/data/data/xxx.xxx.xxx/cache
IOS平台
Application.dataPath:
Application/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/xxx.app/Data
Application.streamingAssetsPath:
Application/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/xxx.app/Data/Raw
Application.persistentDataPath:
Application/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/Documents
Application.temporaryCachePath:
Application/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/Library/Caches
我们就是在游戏界面中绘制一个Button(位置要选择好不要碍事), 这个脚本最好是 永远不被销毁的,这样就可以一直可用了。
单击按钮就及时的捕捉到问题的画面就OK了。
(我以前在这篇文章中也写过类似的代码了:http://blog.csdn.net/u010019717/article/details/43113305)

using UnityEngine;using System.Collections;using System;/// /// 用于对游戏的画面进行捕捉,就是截屏/// 测试可以使用,对问题捕捉下来/// public class ScreenShoter : MonoBehaviour{    public string filePath  = Application.dataPath;    void Awake()    {        DontDestroyOnLoad(transform.gameObject);    }    void OnGUI()    {        if (GUI.Button(new Rect((Screen.width - 60) * 0.5f, 0, 60, 30), "截屏"))        {            Application.CaptureScreenshot(string.Format("{0}\\ss_{1}x{2}_{3}.jpg",filePath, Screen.width, Screen.height, System.DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalSeconds));        }    }}

你可能感兴趣的文章
mabatis 中出现< 以及> 代表什么意思?
查看>>
Mac book pro打开docker出现The data couldn’t be read because it is missing
查看>>
MAC M1大数据0-1成神篇-25 hadoop高可用搭建
查看>>
mac mysql 进程_Mac平台下启动MySQL到完全终止MySQL----终端八步走
查看>>
Mac OS 12.0.1 如何安装柯美287打印机驱动,刷卡打印
查看>>
MangoDB4.0版本的安装与配置
查看>>
Manjaro 24.1 “Xahea” 发布!具有 KDE Plasma 6.1.5、GNOME 46 和最新的内核增强功能
查看>>
mapping文件目录生成修改
查看>>
MapReduce程序依赖的jar包
查看>>
mariadb multi-source replication(mariadb多主复制)
查看>>
MariaDB的简单使用
查看>>
MaterialForm对tab页进行隐藏
查看>>
Member var and Static var.
查看>>
memcached高速缓存学习笔记001---memcached介绍和安装以及基本使用
查看>>
memcached高速缓存学习笔记003---利用JAVA程序操作memcached crud操作
查看>>
Memcached:Node.js 高性能缓存解决方案
查看>>
memcache、redis原理对比
查看>>
memset初始化高维数组为-1/0
查看>>
Metasploit CGI网关接口渗透测试实战
查看>>
Metasploit Web服务器渗透测试实战
查看>>