Search code examples
c#unity-game-engineeffectspost-processing

How to fade chromatic aberration using code in Unity


I am trying to fade a chromatic aberration effect in and out triggered by a keypress, but not sure where to start.

I am using the built-in render pipeline.

There seems to be little documentation on controlling post processing via code. Please suggest any resources.


Solution

  • I am guessing you are using the post processing package. Here is a great resource to get some info on how to use it: https://docs.unity3d.com/Packages/[email protected]/manual/Manipulating-the-Stack.html

    The simplest way to manipulate an post-processing (pp) effect with this system is to override the settings of the pp profile.

    Just reference the profile like every other asset in your scene, grab the ChromaticAberration setting and override the settings. Like this:

    public PostProcessProfile profile;
    private ChromaticAberration ca;
    // Start is called before the first frame update
    void Start()
    {
        ca = profile.GetSetting<ChromaticAberration>();
    }
    
    // Update is called once per frame
    void Update()
    {
        ca.intensity.Override(Mathf.Sin(Time.frameCount * 0.1f));
    }
    

    To do the fading I advise you use a tweening library like they do in the example i linked. DoTween is great. If you don't want to do that you can always create your own coroutines and animate the behaviour.