Updating URP and HDRP Post Processing Values Using C#

Unity's Universal RP and HDRP does not support PPV2 effects, and requires a different procedure for editing its own post processing values using code. Create an empty GameObject, call it "Post Processing" and add a volume component, a requirement for the new rendering pipelines. Note that to change post processing values, the overrides need to be already added to the volume. Create a new C# script and attach it to your post processing GameObject.

Include the following libraries in your C# script: 

 

using UnityEngine.Rendering;

using UnityEngine.Rendering.Universal;

In this example, we'll have another GameObject set the intensity of chromatic aberration and have it decrease over time to a default value. 

 

Referencing the Profile  

First, we have to get the volume and the correct profile type. For getting the profile type we'll use the TryGet function, which, unlike GetComponent, will not allocate memory in editor if Unity does not find the component. It will pass the profile type through the out modifier and apply it to the ChromaticAberration variable. 



Volume postVolume;

ChromaticAberration ca;

 

void Start(){

    postVolume = gameObject.GetComponent<Volume>();

    postVolume.profile.TryGet<ChromaticAberration>(out ca);

    newChrom = defaultChrom

}

 

Updating the Profile Values

Now that the profile is successfully referenced, the profile settings are exposed and fully editable. Let's say you wanted to change the chromatic aberration intensity to 1f, you'd write it like so:

ca.intensity.Override(1.0f); 

Now we can do some simple code to reduce the intensity if it is over the default value. 

 

void Update(){

    if (newChrom > defaultChrom) {

        newChrom -= 0.01f;

    }

    ca.intensity.Override(newChrom);

}


Referencing the Script from Another 

Create a new public function and have it require a float to be passed through. This value will be automatically applied to the profile because of ca.intensity.Override(newChrom) in the Update function.

 

public void UpdateChromaticAberration(float newValue) {

    newChrom = newValue;

}

From another script, reference this function like: 

 

public Script_Post_Update postUpdate;

postUpdate.UpdateChromaticAberration(1.0f); 

 

Full script

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using UnityEngine.Rendering;

using UnityEngine.Rendering.Universal;

 

public class Script_Post_Update MonoBehaviour {

 

    Volume postVolume;

    ChromaticAberration ca;

 

    float defaultChrom 0.034f;

    float newChrom;

 

    void Start(){

        postVolume = gameObject.GetComponent<Volume>();

        postVolume.profile.TryGet<ChromaticAberration>(out ca);

 

        newChrom = defaultChrom;

    }

 

    void Update(){

        if (newChrom > defaultChrom) {

            newChrom -= 0.01f;

        }

        ca.intensity.Override(newChrom);

    }

 

    public void UpdateChromaticAberration(float newValue) {

        newChrom = newValue;

    }

}