Search code examples
c#user-interfaceunity-game-enginesliderpost-processing

Setting up a UI slider to control post processing effects?


As the title says, I'm trying to set up a UI slider so the player can adjust some of the post processing settings (specifically the exposure and temperature) while the game is running.

To bring you up to speed:

I figure my best shot is to try to adapt what I learned from this tutorial on creating an audio volume slider: https://www.youtube.com/watch?v=YOaYQrN1oYQ&t=122s

This is the code I've cobbled together so far:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BrightnessSlider : MonoBehaviour {

    public void SetBrightness (float brightness)
    {
        Debug.Log(brightness);
    }
}

Specific issues I'm having at specific points in the tutorial:

  • 2:07 For the function of the slider, the tutorial sets up a dynamic float that matches the custom method (SetVolume) they specify. When I try setting up my own function with a custom method (SetBrightness), I can't find it. I'm also not sure if I need to set a different object instead of the canvas for this step.
  • 3:47 In the tutorial they expose a parameter for the volume so that it can be manipulated through script, but I don't know what the equivalent of that would be for post processing.

For the record, I was able to follow this tutorial to create my own audio slider and got it working with no problems.

One last thing: I opened up the script for the post processing profile and found the variable type I think I'd need or would at least be somewhat relevant: ColorGradingModel, but I honestly have no idea what to do with this information.

Update July 09, 2018



I've since been looking over @Nol's code and had someone else look at it and help me out with it. At the moment, the slider's functionality(not sure if that's the correct terminology but that's what I've been sticking with) is set up through the On Value Changed field in the inspector , but it's not actually driving/changing the brightness values. I had someone else (who's far more qualified than I am) look at it with me. It seems like it should work the way they've set it up, but something is getting lost in translation between the method and the slider.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.PostProcessing;
using UnityEngine.UI;

public class BrightnessSlider : MonoBehaviour   
    {
    public Slider slider;
    public PostProcessingProfile Default;
    private ColorGradingModel cgm;

    private void Start()
    {
        //I haven't been able to get this to not return some sort of error, 
        //and I'm not even sure of its usefulness. 
        //I've been keeping it commented out for the most part.
        Default.profile.TryGetSettings(out cgm);
    }

    public void SetBrightness(float brightness)
    {
        ColorGradingModel.Settings settings = cgm.settings;
        settings.basic.postExposure = brightness;
        cgm.settings = settings;
        Debug.Log("Brightness is: " + brightness);  //For testing purposes
    }

}

Solution

  • It seems like you have the core of what you need to know for changing your settings down. That's good.

    You'll need a few simple changes:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.Rendering.PostProcessing; //How you'll access PPV (Post Processing Volume) models and settings
    
    public class BrightnessSlider : MonoBehaviour {
    
        PostProcessingVolume ppv; //You can make this public to set in inspector
        ColorGradingModel cgm; //can use ppv.profile.TryGetSettings(out cgm) in Start()
    
        public void SetBrightness (float brightness)
        {
            cg.[setting you want to change].value = brightness;
        }
    }