在Unity中如何通过脚本访问音量后处理效果

3
所以,我正在使用HDRP制作3D游戏,为了调整游戏的亮度,我使用组件颜色调整(请查看附加的图像),在那里我改变颜色滤镜强度(点击“HDR”选项后会显示强度)。所以我的问题是:我如何在C#脚本中访问这些信息?这是可能的吗?如果可能,如果您能向我展示,我会非常感激。

提前致谢。

检查器的图像


你尝试过使用GetComponent<ColorAdjustments>().ColorFilter吗? - Eric
这个不起作用。只是给了我错误,比如"'ColorAdjustments' 找不到"。 - sw3yko
3个回答

4
根据Unity论坛上 这个答案,您可以通过以下方式访问音量效果:
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Experimental.Rendering.HDPipeline;
public class AffectDepthOfField : MonoBehaviour
{
    public bool spherecast = true;
    public Transform mainCamera;
    RaycastHit hit;

    DepthOfField dofComponent;

    void Start()
    {
        Volume volume = gameObject.GetComponent<Volume>();
        DepthOfField tmp;
        if (volume.profile.TryGet<DepthOfField>(out tmp))
        {
            dofComponent = tmp;
        }
    }
    void Update()
    {
        if (spherecast)
        {
            if (Physics.SphereCast(mainCamera.position, 0.1f, mainCamera.forward, out hit, 10f))
            {
                dofComponent.nearFocusStart = new MinFloatParameter(1f, 0f, true);
                dofComponent.nearFocusEnd = new MinFloatParameter(1f, 0f, true);
                dofComponent.farFocusStart = new MinFloatParameter(1f, 0f, true);
                dofComponent.farFocusEnd = new MinFloatParameter(1f, 0f, true);
            }
        }
    }
}

1
我在这方面取得了一些进展,例如像这样引用颜色调整:ColorAdjustments _colorAdjustments = gameObject.GetComponent<ColorAdjustments>(); 然后通过编写 _colorAdjustments.colorFilter 来更改颜色滤镜的强度,但是我卡住了。有任何想法如何从这里更改“HDR颜色”的强度吗? - sw3yko

1

如果你只想启用/禁用它,可以将其放在单独的子游戏对象中,并在需要时启用-禁用它。(如果这不是你的意图,请忽略此提示)。


0

您可以将您的Volume Profile拖放到gameVolume中,然后像这样访问音量属性:

[SerializeField] private VolumeProfile gameVolume;
private ColorAdjustments colorAdjustments;

private void Awake()
{
    ColorAdjustments cA;
    if (gameVolume.TryGet<ColorAdjustments>(out cA))
    {
        colorAdjustments = cA;
        colorAdjustments.hueShift.overrideState = true;
        colorAdjustments.hueShift.value = 20f;
    }
}

如果你像这样改变值,即使在退出播放模式后,这些改变也会保持不变。 Unity文档了解更多信息

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接