using UnityEngine;
using UnityEngine.Rendering.Universal;
using UnityEngine.SceneManagement;
namespace NLD
{
///
/// 在特定相机和特定场景上启用depth texture
/// 用于支持需要深度信息的shader(如雾效soft particles)
///
[RequireComponent(typeof(UnityEngine.Camera))]
public class EnableDepthTextureForCamera : MonoBehaviour
{
[Header("场景限制")]
[Tooltip("指定在哪些场景中启用depth texture,留空则在所有场景生效")]
[SerializeField] private string[] targetSceneNames = new string[0];
[Header("调试信息")]
[Tooltip("是否输出调试日志")]
[SerializeField] private bool showDebugLog = true;
private UnityEngine.Camera _camera;
private UniversalAdditionalCameraData _cameraData;
private bool _originalRequireDepth;
private bool _isDepthEnabled = false;
private void OnEnable()
{
// 检查是否在目标场景中
if (!IsInTargetScene())
{
if (showDebugLog)
{
Debug.Log($"[EnableDepthTexture] 相机 '{gameObject.name}' 不在目标场景中,跳过启用");
}
return;
}
InitializeCamera();
EnableDepthTexture();
CheckRenderTextureConfiguration();
}
private void OnDisable()
{
RestoreOriginalSettings();
}
private void InitializeCamera()
{
_camera = GetComponent();
_cameraData = _camera.GetUniversalAdditionalCameraData();
if (_cameraData == null)
{
Debug.LogError($"[EnableDepthTexture] 相机 '{gameObject.name}' 没有UniversalAdditionalCameraData组件!");
}
}
private void EnableDepthTexture()
{
if (_cameraData == null) return;
_originalRequireDepth = _cameraData.requiresDepthTexture;
_cameraData.requiresDepthTexture = true;
_isDepthEnabled = true;
if (showDebugLog)
{
Debug.Log($"[EnableDepthTexture] 已在相机 '{gameObject.name}' 上启用depth texture");
}
}
private void RestoreOriginalSettings()
{
if (_isDepthEnabled && _cameraData != null)
{
_cameraData.requiresDepthTexture = _originalRequireDepth;
_isDepthEnabled = false;
if (showDebugLog)
{
Debug.Log($"[EnableDepthTexture] 已恢复相机 '{gameObject.name}' 的原始depth texture设置");
}
}
}
private bool IsInTargetScene()
{
if (targetSceneNames == null || targetSceneNames.Length == 0)
{
return true;
}
string currentSceneName = SceneManager.GetActiveScene().name;
foreach (string sceneName in targetSceneNames)
{
if (string.IsNullOrEmpty(sceneName))
{
continue;
}
if (currentSceneName.Equals(sceneName, System.StringComparison.OrdinalIgnoreCase))
{
return true;
}
}
return false;
}
private void CheckRenderTextureConfiguration()
{
if (_camera == null || _camera.targetTexture == null)
{
return;
}
RenderTexture rt = _camera.targetTexture;
if (rt.depth == 0)
{
Debug.LogWarning(
$"[EnableDepthTexture] 警告:相机 '{gameObject.name}' 渲染到RenderTexture '{rt.name}'," +
$"但该RT没有depth buffer (depthBufferBits = 0)!\n"
);
}
else if (showDebugLog)
{
Debug.Log(
$"[EnableDepthTexture] RenderTexture '{rt.name}' 配置正确 " +
$"(depthBufferBits = {rt.depth})"
);
}
}
public void SetDepthTextureEnabled(bool enabled)
{
if (_cameraData != null && IsInTargetScene())
{
_cameraData.requiresDepthTexture = enabled;
_isDepthEnabled = enabled;
if (showDebugLog)
{
Debug.Log($"[EnableDepthTexture] 运行时设置depth texture为: {enabled}");
}
}
}
public bool IsDepthTextureEnabled()
{
return _isDepthEnabled && _cameraData != null && _cameraData.requiresDepthTexture;
}
}
}