using AibisDream.Utility; using UnityEngine; namespace AibisDream.SaveSystem { /// /// 存档槽位缩略图捕获工具。 /// /// 使用主相机渲染到 RenderTexture 后读像素并编码为 PNG。 /// 捕获失败时安全返回 null,不会阻塞存档流程。 /// /// public static class SlotThumbnailCapture { /// 捕获当前画面的 PNG 字节数组。 public static byte[] CapturePng() { 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; } } }