Files
aibis-dream/Assets/Scripts/SaveSystem/SlotThumbnailCapture.cs
T

54 lines
1.6 KiB
C#

using AibisDream.Utility;
using UnityEngine;
namespace AibisDream.SaveSystem
{
/// <summary>
/// 存档槽位缩略图捕获工具。
/// <para>
/// 使用主相机渲染到 RenderTexture 后读像素并编码为 PNG。
/// 捕获失败时安全返回 null,不会阻塞存档流程。
/// </para>
/// </summary>
public static class SlotThumbnailCapture
{
/// <summary>捕获当前画面的 PNG 字节数组;禁用时返回 null,不阻塞存档流程。</summary>
public static byte[] CapturePng()
{
if (!ConstRef.SaveThumbnailEnabled)
{
return null;
}
var cam = Camera.main;
if (cam == null)
{
Debug.LogWarning("[SlotThumbnailCapture] 未找到 Camera.main,跳过截图。");
return null;
}
var width = ConstRef.SaveThumbnailWidth;
var height = ConstRef.SaveThumbnailHeight;
var rt = RenderTexture.GetTemporary(width, height, 24, RenderTextureFormat.Default);
var prevTarget = cam.targetTexture;
cam.targetTexture = rt;
cam.Render();
cam.targetTexture = prevTarget;
RenderTexture.active = rt;
var tex = new Texture2D(width, height, TextureFormat.RGB24, false);
tex.ReadPixels(new Rect(0, 0, width, height), 0, 0);
tex.Apply();
RenderTexture.active = null;
var bytes = tex.EncodeToPNG();
Object.Destroy(tex);
RenderTexture.ReleaseTemporary(rt);
return bytes;
}
}
}