Unity截屏并压缩图片方案总结

一、使用Unity内置的截屏功能CaptureScreenshot直接截图保存到本地

ScreenCapture.CaptureScreenshot(Application.streamingAssetsPath + "/ScreenShot.png");

二、使用Unity内置的截屏功能CaptureScreenshotAsTexture得到Texture2D
该方案需要考虑CaptureScreenshotAsTexture只能在一个渲染帧完成后执行,相关的描述如下
To get a reliable output from this method you must make sure it is called once the frame rendering has ended, and not during the rendering process. A simple way of ensuring this is to call it from a coroutine that yields on WaitForEndOfFrame. If you call this method during the rendering process you will get unpredictable and undefined results.

    IEnumerator RecordFrame()
    {
        yield return new WaitForEndOfFrame();
        var texture = ScreenCapture.CaptureScreenshotAsTexture();
        Object.Destroy(texture);
    }

    public void LateUpdate()
    {
        StartCoroutine(RecordFrame());
    }

三、使用Texture2D的ReadPixels获得Texture2D(这个的协程 必须在OnGUI中调用才可以)

    public static Texture2D CaptureScreen(Camera came, Rect r) {
        RenderTexture rt = new RenderTexture((int)r.width, (int)r.height, 0);
        came.targetTexture = rt;
        came.Render();
        RenderTexture.active = rt;
        Texture2D screenShot = new Texture2D((int)r.width, (int)r.height, TextureFormat.RGB24, false);
        screenShot.ReadPixels(r, 0, 0);
        screenShot.Apply();
        came.targetTexture = null;
        RenderTexture.active = null;
        Destroy(rt);
        byte[] bytes = screenShot.EncodeToPNG();
        string filename = Application.streamingAssetsPath + "/aaaaaaaaScreenShot.png";
        File.WriteAllBytes(filename, bytes);
        return screenShot;
    }

其中方案方案一、二在使用中能得到正确的图片,截取的是全屏,但是也会截取到UI。
方案三可以根据设置的Rect截取任意部分屏幕,可以根据参数came当前渲染的部分进行选择性截屏,但是在本人项目中使用时有渲染逻辑问题,即不同类型的物体在截屏后会有渲染先后的问题,其他人也遇到过这个问题,可能是因为没有在OnGUI中调用,暂时未测试,我在项目中使用的是方案二,然后再根据需要的位置进行截取和压缩图片,代码如下:

 public static Texture2D ResetTexture(Texture2D orginalTexture,int resetWidth,int resetHeight) {
        Texture2D orginalTextureReset = new Texture2D(orginalTexture.height, orginalTexture.height, TextureFormat.RGB24, false);
        Color color;
        int cutValue=420;
        for (int i = 0; i < orginalTextureReset.width; i++) {
            for (int j = 0; j < orginalTextureReset.height; j++) {
                color = orginalTexture.GetPixel(i+ cutValue, j);
                orginalTextureReset.SetPixel(i, j, color);
            }
        }
        Texture2D newTexture = new Texture2D(resetWidth, resetHeight, TextureFormat.RGB24, false);
        for (int i = 0; i < newTexture.height; i++)
        {
            for (int j = 0; j < newTexture.width; j++) {
                color = orginalTextureReset.GetPixel((int)(i * 8.4375f), (int)(j * 8.4375f));
                newTexture.SetPixel(i, j, color);
            }
        }
        newTexture.Apply();
        return newTexture;
    }

上面代码的功能是将orginalTexture截取成最大居中正方形(项目为19201080,所有截取后为10801080),并压缩成128*128像素。其中数值420=(1920-1080)/2;8.4375=1080/128。项目中的截图用于一个小图标,经过测试保存成有损的jpg会比png小很多(5k之于13k),因此使用Texture2D.EncodeToJPG保存。
四、将Texture2D转换成string进行网络传输

System.Convert.ToBase64String(texture2D.EncodeToJPG())

五、将string转换成sprite显示在Image上

Texture2D texture = new Texture2D(128, 128);
texture.LoadImage(System.Convert.FromBase64String(textureString);
thumbnailImage.sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), Vector2.zero);
         
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Unity中,保存截图是一个很简单的操作。首先,我们需要在Unity的脚本中编写保存截图的代码。代码示例如下: ```csharp using UnityEngine; public class Screenshot : MonoBehaviour { // 设置截图的文件名 public string fileName = "screenshot.png"; void Update() { // 当用户按下指定的按键(例如空格键)时,执行截图操作 if (Input.GetKeyDown(KeyCode.Space)) { // 获取当前屏幕的宽度和高度 int width = Screen.width; int height = Screen.height; // 创建一个新的2D纹理,并将屏幕内容拷贝到这个纹理中 Texture2D tex = new Texture2D(width, height, TextureFormat.RGB24, false); tex.ReadPixels(new Rect(0, 0, width, height), 0, 0); tex.Apply(); // 将纹理保存为PNG格式的图片 byte[] bytes = tex.EncodeToPNG(); System.IO.File.WriteAllBytes(fileName, bytes); } } } ``` 在这段代码中,我们首先定义了一个变量`fileName`,用来保存截图的文件名。然后在`Update`方法中,我们检测用户是否按下了空格键,如果按下了空格键,就执行截图操作。截图操作的具体步骤是:获取屏幕的宽度和高度,创建一个新的2D纹理,将屏幕内容拷贝到这个纹理中,然后将纹理保存为PNG格式的图片。 当我们把这段代码添加到一个游戏对象上之后,只需要在Unity中运行游戏,按下空格键,就可以保存当前屏幕的截图了。这样我们就可以很方便地在Unity中进行截图保存操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JousonRen

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值