如何使用统一的 RenderTexture 获得更好的颜色?

How to have better colors with RenderTexture in unity?

我正在使用脚本在我的游戏中做一些截图。但是,我对颜色格式和东西知之甚少。拍出来的照片颜色比实物少,尤其是透明物体(一些灰色透明的东西被渲染成黑色,不那么透明)。

示例:

这是代码:

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

public class ScreenshotManager : MonoBehaviour
{
    public Camera cam;

    IEnumerator Rend() 
    {
        yield return new WaitForEndOfFrame();
        RenderTexture renderTexture = cam.targetTexture;

        Texture2D renderResult = new Texture2D(renderTexture.width, renderTexture.height, TextureFormat.ARGB32, false);
        Rect rect = new Rect(0, 0, renderTexture.width, renderTexture.height);
        renderResult.ReadPixels(rect, 0, 0);

        byte[] byteArray = renderResult.EncodeToPNG();

        int screenshotIndex = 1;
        while(System.IO.File.Exists(Application.dataPath + "/Screenshot_" + screenshotIndex +".png"))
        {
            screenshotIndex++;
        }
        
        System.IO.File.WriteAllBytes(Application.dataPath + "/Screenshot_" + screenshotIndex +".png", byteArray);

        Debug.Log("Screen saved");

        RenderTexture.ReleaseTemporary(renderTexture);
        cam.targetTexture = null;
    }

    public void TakeScreenshot()
    {
        cam.targetTexture = RenderTexture.GetTemporary(Screen.width, Screen.height, 16);
        StartCoroutine(Rend());
    }
}

我是不是漏掉了什么?

感谢您的帮助。

您可以只使用此功能进行屏幕截图

ScreenCapture.CaptureScreenshot(fileName)

或者在您的情况下,问题可能出在 RenderTextureFormat 中。 如果您的相机使用 HDR

,请尝试使用 RenderTextureFormat.ARGB64
RenderTexture.GetTemporary(Screen.width, Screen.height, RenderTextureFormat.ARGB64);