Unity 屏幕截图方法

Unity 屏幕截图

  • 前言
  • 代码
    • 第一种方式
    • 第二种方式
    • 第三种方式
    • 第四种方法
  • 工具代码

前言

遇到一个需求是使用Unity截取当前屏幕。
代码转自:astrumL/ScreenShot

代码

第一种方式

普通截图,没什么可说的,UI和物体都可以保存

    /// /// UnityEngine自带截屏Api,只能截全屏/// /// 文件名public void ScreenShotFile(string fileName){UnityEngine.ScreenCapture.CaptureScreenshot(fileName);//截图并保存截图文件Debug.Log(string.Format("截取了一张图片: {0}", fileName));#if UNITY_EDITORUnityEditor.AssetDatabase.Refresh();//刷新Unity的资产目录
#endif}

第二种方式

协程截图,会把纹理转成Texture2D造成卡顿,UI和物体都可以保存

/// /// UnityEngine自带截屏Api,只能截全屏/// /// 文件名/// 截图完成回调/// 协程public IEnumerator ScreenShotTex(string fileName, CallBack callBack = null){yield return new WaitForEndOfFrame();//等到帧结束,不然会报错Texture2D tex = UnityEngine.ScreenCapture.CaptureScreenshotAsTexture();//截图返回Texture2D对象byte[] bytes = tex.EncodeToPNG();//将纹理数据,转化成一个png图片System.IO.File.WriteAllBytes(fileName, bytes);//写入数据Debug.Log(string.Format("截取了一张图片: {0}", fileName));callBack?.Invoke();
#if UNITY_EDITORUnityEditor.AssetDatabase.Refresh();//刷新Unity的资产目录
#endif}

第三种方式

根据像素截图,可以指定截图区域,协程方式,UI和物体都可以保存

    /// /// 截取游戏屏幕内的像素/// /// 截取区域:屏幕左下角为0点/// 文件名/// 截图完成回调/// public IEnumerator ScreenCapture(Rect rect, string fileName, CallBack callBack = null){yield return new WaitForEndOfFrame();//等到帧结束,不然会报错Texture2D tex = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.ARGB32, false);//新建一个Texture2D对象tex.ReadPixels(rect, 0, 0);//读取像素,屏幕左下角为0点tex.Apply();//保存像素信息byte[] bytes = tex.EncodeToPNG();//将纹理数据,转化成一个png图片System.IO.File.WriteAllBytes(fileName, bytes);//写入数据Debug.Log(string.Format("截取了一张图片: {0}", fileName));callBack?.Invoke();
#if UNITY_EDITORUnityEditor.AssetDatabase.Refresh();//刷新Unity的资产目录
#endif}

第四种方法

根据相机截图,可选相机,可选截图范围,读取像素,非协程可能会卡顿,UI和物体都可以保存

/// /// 对相机拍摄区域进行截图,如果需要多个相机,可类比添加,可截取多个相机的叠加画面/// /// 待截图的相机/// 截取的图片宽度/// 截取的图片高度/// 文件名/// 返回Texture2D对象public Texture2D CameraCapture(Camera camera, Rect rect, string fileName){RenderTexture render = new RenderTexture((int)rect.width, (int)rect.height, -1);//创建一个RenderTexture对象 camera.gameObject.SetActive(true);//启用截图相机camera.targetTexture = render;//设置截图相机的targetTexture为rendercamera.Render();//手动开启截图相机的渲染RenderTexture.active = render;//激活RenderTextureTexture2D tex = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.ARGB32, false);//新建一个Texture2D对象tex.ReadPixels(rect, 0, 0);//读取像素tex.Apply();//保存像素信息camera.targetTexture = null;//重置截图相机的targetTextureRenderTexture.active = null;//关闭RenderTexture的激活状态Object.Destroy(render);//删除RenderTexture对象byte[] bytes = tex.EncodeToPNG();//将纹理数据,转化成一个png图片System.IO.File.WriteAllBytes(fileName, bytes);//写入数据Debug.Log(string.Format("截取了一张图片: {0}", fileName));#if UNITY_EDITORUnityEditor.AssetDatabase.Refresh();//刷新Unity的资产目录
#endifreturn tex;//返回Texture2D对象,方便游戏内展示和使用}

工具代码

using UnityEngine;
using System.Collections;public delegate void CallBack();//利用委托回调可以先关闭UI,截取到没有UI的画面
/// 
/// 截图工具类
/// 
public class ScreenTool
{private static ScreenTool _instance;public static ScreenTool Instance{get{if (_instance == null)_instance = new ScreenTool();return _instance;}}/// /// UnityEngine自带截屏Api,只能截全屏/// /// 文件名public void ScreenShotFile(string fileName){UnityEngine.ScreenCapture.CaptureScreenshot(fileName);//截图并保存截图文件Debug.Log(string.Format("截取了一张图片: {0}", fileName));#if UNITY_EDITORUnityEditor.AssetDatabase.Refresh();//刷新Unity的资产目录
#endif}/// /// UnityEngine自带截屏Api,只能截全屏/// /// 文件名/// 截图完成回调/// 协程public IEnumerator ScreenShotTex(string fileName, CallBack callBack = null){yield return new WaitForEndOfFrame();//等到帧结束,不然会报错Texture2D tex = UnityEngine.ScreenCapture.CaptureScreenshotAsTexture();//截图返回Texture2D对象byte[] bytes = tex.EncodeToPNG();//将纹理数据,转化成一个png图片System.IO.File.WriteAllBytes(fileName, bytes);//写入数据Debug.Log(string.Format("截取了一张图片: {0}", fileName));callBack?.Invoke();
#if UNITY_EDITORUnityEditor.AssetDatabase.Refresh();//刷新Unity的资产目录
#endif}/// /// 截取游戏屏幕内的像素/// /// 截取区域:屏幕左下角为0点/// 文件名/// 截图完成回调/// public IEnumerator ScreenCapture(Rect rect, string fileName, CallBack callBack = null){yield return new WaitForEndOfFrame();//等到帧结束,不然会报错Texture2D tex = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.ARGB32, false);//新建一个Texture2D对象tex.ReadPixels(rect, 0, 0);//读取像素,屏幕左下角为0点tex.Apply();//保存像素信息byte[] bytes = tex.EncodeToPNG();//将纹理数据,转化成一个png图片System.IO.File.WriteAllBytes(fileName, bytes);//写入数据Debug.Log(string.Format("截取了一张图片: {0}", fileName));callBack?.Invoke();
#if UNITY_EDITORUnityEditor.AssetDatabase.Refresh();//刷新Unity的资产目录
#endif}/// /// 对相机拍摄区域进行截图,如果需要多个相机,可类比添加,可截取多个相机的叠加画面/// /// 待截图的相机/// 截取的图片宽度/// 截取的图片高度/// 文件名/// 返回Texture2D对象public Texture2D CameraCapture(Camera camera, Rect rect, string fileName){RenderTexture render = new RenderTexture((int)rect.width, (int)rect.height, -1);//创建一个RenderTexture对象 camera.gameObject.SetActive(true);//启用截图相机camera.targetTexture = render;//设置截图相机的targetTexture为rendercamera.Render();//手动开启截图相机的渲染RenderTexture.active = render;//激活RenderTextureTexture2D tex = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.ARGB32, false);//新建一个Texture2D对象tex.ReadPixels(rect, 0, 0);//读取像素tex.Apply();//保存像素信息camera.targetTexture = null;//重置截图相机的targetTextureRenderTexture.active = null;//关闭RenderTexture的激活状态Object.Destroy(render);//删除RenderTexture对象byte[] bytes = tex.EncodeToPNG();//将纹理数据,转化成一个png图片System.IO.File.WriteAllBytes(fileName, bytes);//写入数据Debug.Log(string.Format("截取了一张图片: {0}", fileName));#if UNITY_EDITORUnityEditor.AssetDatabase.Refresh();//刷新Unity的资产目录
#endifreturn tex;//返回Texture2D对象,方便游戏内展示和使用}
}


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部