How to access Volume post-processing effects by script in Unity

Viewed 15886

So, i'm currently working on a 3D game using HDRP and to adjust the brightness of the game I use the component Color Adjustment (check out the attached image) and there I change the Intensity (the intensity is displayed once clicking the "HDR" option) of the Color Filter. So my question is: How do I access this information in a C# script? Is it possible? If it is possible i'd be really happy if you, the person reading this could show me.

Thanks in advance.

Image of the inspector

3 Answers

As per this answer on the Unity forums, you can access Volume effects like so:

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

you could put it in a separate child game object and enable-disable that on will if all you want to do is enable/disable it. (if thats not your intention, just ignore this).

You can drag and drop your Volume Profile into gameVolume and then access the volume properties like this:

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

If you change the values like this, the change will persist even after you exit play mode. Unity Documentation for more info

Related