如何使用透明背景捕获Unity3d中的屏幕截图?

时间:2014-08-01 12:44:13

标签: c# unity3d

我试图在Unity3D Pro中捕获游戏对象的屏幕截图,以便它具有透明背景。这个脚本是我建议的,只要材料没有纹理,它就可以连接到主摄像头。然后我在游戏对象上出现半透明,如本例所示。 http://sta.sh/0iwguk5rx61。对此的任何帮助将不胜感激。

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

2 个答案:

答案 0 :(得分:0)

我是哥伦比亚人,我的英语不好,我希望你理解我,

我遇到了同样的问题,只需将TextureFormat ARGB32更改为RGB24即可解决问题:

...
RenderTexture rt = new RenderTexture(resWidth, resHeight, 24);
camera.targetTexture = rt;
Texture2D screenShot = new Texture2D(resWidth, resHeight, TextureFormat.RGB24, false);
camera.Render();
RenderTexture.active = rt;
...

我希望能提供帮助 见你,:D

答案 1 :(得分:0)

看看这是否适合您。

TEXT

您可能必须刷新资产文件夹(Ctrl + R)才能使“屏幕截图”文件夹出现在检查器中。