本文共 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: 
    此属性用于返回一个临时数据的缓存目录。 
                              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(位置要选择好不要碍事), 这个脚本最好是 永远不被销毁的,这样就可以一直可用了。 
        (我以前在这篇文章中也写过类似的代码了: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));        }    }}