using System;
using Framework;
using PhxhSDK;
using UnityEngine;
using Constants = Framework.Constants;
///
/// 截图管理器
/// 负责管理游戏内的模糊截图功能(用于UI背景等)
///
public class ScreenshotManager
{
#region Singleton
private static ScreenshotManager _instance;
public static ScreenshotManager Instance
{
get
{
if (_instance == null)
{
_instance = new ScreenshotManager();
}
return _instance;
}
}
private ScreenshotManager() { }
#endregion
private const string SCREEN_SNAPSHOT = "ScreenSnapshot";
private RenderTexture _screenTexture;
///
/// 截取当前屏幕并应用模糊效果
///
/// 是否截取UI相机内容(默认true),false则截取主相机
public void CaptureScreenshot(bool captureUI = true)
{
try
{
//int width = PerformanceManager.instance.ScreenSize.x / 2;
//int height = PerformanceManager.instance.ScreenSize.y / 2;
int width = Screen.width / 2;
int height = Screen.height / 2;
// 创建或更新 RenderTexture
if (null == _screenTexture)
{
_screenTexture = RenderTexturePool.Instance.Create(SCREEN_SNAPSHOT, width, height, RenderTexturePool.DEFAULT_DEPTH);
_screenTexture.filterMode = FilterMode.Trilinear;
}
else if (_screenTexture.width != width || _screenTexture.height != height)
{
RenderTexturePool.Instance.Destroy(_screenTexture, true);
_screenTexture = RenderTexturePool.Instance.Create(SCREEN_SNAPSHOT, width, height, RenderTexturePool.DEFAULT_DEPTH);
_screenTexture.filterMode = FilterMode.Trilinear;
}
CameraManager.Instance.SetMainCameraActive(false);
// 渲染截图
Camera usingCamera = captureUI ? CameraManager.Instance.UICamera : CameraManager.Instance.MainCamera;
if (usingCamera != null)
{
Rect rect = usingCamera.rect;
usingCamera.rect = new Rect(0f, 0f, 1f, 1f);
usingCamera.targetTexture = _screenTexture;
usingCamera.Render();
usingCamera.targetTexture = captureUI ? null : CameraManager.Instance.DefaultRenderTarget;
usingCamera.rect = rect;
}
else
{
DebugUtil.LogError("ScreenshotManager: usingCamera is null");
}
// 高斯模糊处理
ApplyGaussianBlur();
}
catch (Exception e)
{
DebugUtil.LogError($"ScreenshotManager.CaptureScreenshot error: {e}");
}
}
///
/// 应用高斯模糊效果
///
private void ApplyGaussianBlur()
{
var material = AssetManager.Instance.GetPreLoadResult(Constants.BLUR_MAT_PATH);
if (material == null)
{
DebugUtil.LogError("ScreenshotManager: Blur material not found");
return;
}
// 需要用一个缓存的rt,不然移动平台会出现方块
RenderTexture cacheRt = CameraManager.Instance.DefaultRenderTarget;
Graphics.Blit(_screenTexture, cacheRt, material);
Graphics.Blit(cacheRt, _screenTexture);
}
///
/// 重置截图状态(恢复主相机)
///
public void ResetScreenshot()
{
CameraManager.Instance.SetMainCameraActive(true);
}
///
/// 获取截图的 Texture2D
///
public Texture2D GetScreenTexture2D()
{
if (_screenTexture == null)
{
DebugUtil.LogWarning("ScreenshotManager: No screenshot available");
return null;
}
Texture2D texture2D = PhxhSDK.Utils.CreateTexture2D(_screenTexture.name, _screenTexture.width, _screenTexture.height);
RenderTexture.active = _screenTexture;
texture2D.ReadPixels(new Rect(0f, 0f, _screenTexture.width, _screenTexture.height), 0, 0);
texture2D.Apply();
RenderTexture.active = null;
return texture2D;
}
}