用 CaptureScreenshot捕捉游戏画面(截图,截屏)

通过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)


[csharp]  view plain  copy
 print ?
  1. using UnityEngine;  
  2. using System.Collections;  
  3. using System;  
  4.   
  5. /// <summary>  
  6. /// 用于对游戏的画面进行捕捉,就是截屏  
  7. /// 测试可以使用,对问题捕捉下来  
  8. /// </summary>  
  9.   
  10. public class ScreenShoter : MonoBehaviour  
  11. {  
  12.     public string filePath  = Application.dataPath;  
  13.   
  14.     void Awake()  
  15.     {  
  16.         DontDestroyOnLoad(transform.gameObject);  
  17.     }  
  18.     void OnGUI()  
  19.     {  
  20.         if (GUI.Button(new Rect((Screen.width - 60) * 0.5f, 0, 60, 30), "截屏"))  
  21.         {  
  22.             Application.CaptureScreenshot(string.Format("{0}\\ss_{1}x{2}_{3}.jpg",  
  23. filePath, Screen.width, Screen.height, System.DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalSeconds));  
  24.         }  
  25.     }  
  26. }  
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值