【发布时间】: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