如何在 Unity3d 中使用透明背景捕获屏幕截图? [英] How do I capture a screenshot in Unity3d with a transparent background?

查看:100
本文介绍了如何在 Unity3d 中使用透明背景捕获屏幕截图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Unity3D Pro 中捕获游戏对象的屏幕截图,以便它具有透明背景.这个脚本是向我推荐的,当连接到主​​相机时,只要材质没有纹理,它就可以工作.然后我在游戏对象上出现一个半透明,如本例所示.http://sta.sh/0iwguk5rx61.对此的任何帮助将不胜感激.

I'm trying to capture a screenshot of a gameobject in Unity3D Pro so that it has a transparent background. This script was suggested to me and it works, when connected to the main Camera, as long as the material doesn't have a texture. Then I get a semi transparency appearing on the gameobject as shown in this example. http://sta.sh/0iwguk5rx61. Any help with this would be greatly appreciated.

public int resWidth = 2550; 
public int resHeight = 3300;

private bool takeHiResShot = false;

    public static string ScreenShotName(int width, int height) {
    return string.Format("{0}/screen_{1}x{2}_{3}.png", 
                         Application.dataPath, 
                         width, height, 
                         System.DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss"));
}

public void TakeHiResShot() {
    takeHiResShot = true;
}

void LateUpdate() {
    takeHiResShot |= Input.GetKeyDown("k");
    if (takeHiResShot) 
    {
        RenderTexture rt = new RenderTexture(resWidth, resHeight, 24);
        camera.targetTexture = rt;
        Texture2D screenShot = new Texture2D(resWidth, resHeight, TextureFormat.ARGB32, false);
        camera.Render();
        RenderTexture.active = rt;
        screenShot.ReadPixels(new Rect(0, 0, resWidth, resHeight), 0, 0);
        camera.targetTexture = null;
        RenderTexture.active = null; 
        Destroy(rt);
        byte[] bytes = screenShot.EncodeToPNG();
        string filename = ScreenShotName(resWidth, resHeight);

        System.IO.File.WriteAllBytes(filename, bytes);
        Debug.Log(string.Format("Took screenshot to: {0}", filename));
        Application.OpenURL(filename);
        takeHiResShot = false;
    }
}

sdg

推荐答案

看看这是否适合您.

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;

public class Screenshot : MonoBehaviour
{

    private void Start()
    {
        string filename = string.Format("Assets/Screenshots/capture_{0}.png", DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss-fff"));
        if (!Directory.Exists("Assets/Screenshots"))
        {
            Directory.CreateDirectory("Assets/Screenshots");
        }
        TakeTransparentScreenshot(Camera.main, Screen.width, Screen.height, filename);
    }

    public static void TakeTransparentScreenshot(Camera cam, int width, int height, string savePath)
    {
        // Depending on your render pipeline, this may not work.
        var bak_cam_targetTexture = cam.targetTexture;
        var bak_cam_clearFlags = cam.clearFlags;
        var bak_RenderTexture_active = RenderTexture.active;

        var tex_transparent = new Texture2D(width, height, TextureFormat.ARGB32, false);
        // Must use 24-bit depth buffer to be able to fill background.
        var render_texture = RenderTexture.GetTemporary(width, height, 24, RenderTextureFormat.ARGB32);
        var grab_area = new Rect(0, 0, width, height);

        RenderTexture.active = render_texture;
        cam.targetTexture = render_texture;
        cam.clearFlags = CameraClearFlags.SolidColor;

        // Simple: use a clear background
        cam.backgroundColor = Color.clear;
        cam.Render();
        tex_transparent.ReadPixels(grab_area, 0, 0);
        tex_transparent.Apply();

        // Encode the resulting output texture to a byte array then write to the file
        byte[] pngShot = ImageConversion.EncodeToPNG(tex_transparent);
        File.WriteAllBytes(savePath, pngShot);

        cam.clearFlags = bak_cam_clearFlags;
        cam.targetTexture = bak_cam_targetTexture;
        RenderTexture.active = bak_RenderTexture_active;
        RenderTexture.ReleaseTemporary(render_texture);
        Texture2D.Destroy(tex_transparent);
    }

}

您可能需要刷新资产文件夹 (Ctrl+R) 才能使 Screenshots 文件夹出现在检查器中.

You might have to refresh your assets folder (Ctrl+R) to make Screenshots folder appear in the inspector.

这篇关于如何在 Unity3d 中使用透明背景捕获屏幕截图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆