【问题标题】:How to have better colors with RenderTexture in unity?如何在统一的 RenderTexture 中获得更好的颜色?
【发布时间】:2021-06-18 06:55:52
【问题描述】:

我正在使用脚本在我的游戏中做一些截图。但是,我对颜色格式和东西知之甚少。它拍摄的照片比实际颜色少,尤其是在透明物体上(一些灰色透明的东西被渲染为黑色且透明度较低)。

示例:

代码如下:

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());
    }
}

我错过了什么吗?

感谢您的帮助。

【问题讨论】:

    标签: c# image unity3d image-processing screenshot


    【解决方案1】:

    你可以用这个功能截图

    ScreenCapture.CaptureScreenshot(fileName)
    

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

    RenderTexture.GetTemporary(Screen.width, Screen.height, RenderTextureFormat.ARGB64);
    

    【讨论】:

    • 哇,我确实做到了这一切,而有一条简单的线可以做到 xD。非常感谢,它工作得很好!
    猜你喜欢
    • 1970-01-01
    • 2015-03-17
    • 1970-01-01
    • 2012-10-15
    • 2012-10-15
    • 1970-01-01
    • 2010-11-18
    • 2010-12-25
    • 2020-08-30
    相关资源
    最近更新 更多