【问题标题】:Why am I getting error on TryGetSettings when using postprocessing v2 ? And how can I fix it?为什么在使用后处理 v2 时出现 TryGetSettings 错误?我该如何解决?
【发布时间】:2020-08-11 16:20:56
【问题描述】:

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

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

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;
    }
}

【问题讨论】:

    标签: c# unity3d post-processing


    【解决方案1】:

    看来您使用的类型不正确。

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

    postProcessVolume.profile.TryGetSettings(out DepthOfField depthOfField);
    

    如果您已经定义了变量:

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

    还要确保在检查器中将景深效果附加到 Postprocess Volume

    【讨论】:

    • 这正在更改为 PostProcessEffectSettings depthOfFieldSettings 但有点奇怪,因为我从本教程中获取了示例代码:youtube.com/watch?v=jg2I2odw51M 并且在第 6:53 分钟,他使用了与我的代码中相同的代码,他没有收到任何错误。
    • 您可能正在使用不同版本的统一,其中 API 看起来有点不同或类名被重命名。无论如何,如果它解决了问题,请接受答案:)
    • 现在我在 depthOfField 上得到空值:depthOfField.focalLength.value = 1f;在视频教程中,他使用了bloom效果但没有变空。
    • 如果为空,则表示后处理卷没有附加此类效果。如果你浏览检查器,找到 Postprocess Volume,添加 DepthOfField 效果,顺便说一下,我想类名可能不同,在视频中他使用了 Bloom,你需要 DepthOfField。不是 PostProcessEffect 或设置。我会更新答案
    • 我编辑了我的问题,depthOfField 的类型现在是 DepthOfField 并且在编辑器的相机中我添加了两个组件,一个是后处理层,另一个是后处理量,然后添加了 depthoffield 效果我可以将编辑器中的焦距值从 1 更改为 300,它会将视图更改为模糊,但现在我想使用脚本更改它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-21
    • 2019-12-22
    • 1970-01-01
    • 2022-01-14
    • 2020-08-20
    • 2016-06-19
    • 1970-01-01
    相关资源
    最近更新 更多