为什么我在使用后处理 v2 时在 TryGetSettings 上出现错误?我该如何解决?

Why am I getting error on TryGetSettings when using postprocessing v2 ? And how can I fix it?

在我相机的编辑器中,我添加了两个组件:Post 处理层和 Post 处理体积 在第一个组件层上,我将层更改为我添加的层 PostProcessing 在第二个组件上,我将 Is Global 设置为 true 并添加了一个效果 Depth Of Field

现在我想通过脚本更改和设置景深焦距的值。 所以我创建了一个新脚本:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering.PostProcessing;

public class PostprocessingEffects : MonoBehaviour
{
    private PostProcessVolume postProcessVolume;
    private DepthOfField depthOfField;

    void Start()
    {
        postProcessVolume = GetComponent<PostProcessVolume>();
        postProcessVolume.profile.TryGetSettings(out PostProcessEffectSettings depthOfFieldSettings);

        depthOfField.focalLength.value = 1f;
    }
}

我在线上遇到错误:

postProcessVolume.profile.TryGetSettings(out depthOfField);

在 TryGetSettings 上,错误是:

类型 'PostprocessingEffects' 不能用作泛型类型或方法 'PostProcessProfile.TryGetSettings(out T)' 中的类型参数 'T'。没有从 'PostprocessingEffects' 到 'UnityEngine.Rendering.PostProcessing.PostProcessEffectSettings'.

的隐式引用转换

也试过:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering.PostProcessing;

public class PostprocessingEffects : MonoBehaviour
{
    private PostProcessVolume postProcessVolume;
    private DepthOfField depthOfField;

    void Start()
    {
        postProcessVolume = GetComponent<PostProcessVolume>();
        postProcessVolume.profile.TryGetSettings(out DepthOfField depthOfField);

        depthOfField.focalLength.value = 1f;
    }
}

您使用的类型似乎不正确。

TryGetSettings 不能采用 PostprocessingEffects 的类型 我相信你想要 DepthOfField 尝试:

postProcessVolume.profile.TryGetSettings(out DepthOfField depthOfField);

如果您已经定义了变量:

private DepthOfField depthOfField;
postProcessVolume.profile.TryGetSettings(out depthOfField);

还要确保在检查器中将景深效果附加到后处理体积