Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Saturation change slider

Discussion in 'Editor & General Support' started by LifeSymbiont, Dec 21, 2021.

  1. LifeSymbiont

    LifeSymbiont

    Joined:
    Apr 19, 2015
    Posts:
    36
    Hello, I am currently trying to code a Saturation slider that also saves and sets the value automatically.

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.Rendering;
    6. using UnityEngine.Rendering.HighDefinition;
    7. using UnityEngine.UI;
    8.  
    9. public class SaturationChange : MonoBehaviour
    10. {
    11.     public Slider SaturationSlider;
    12.     public Volume volume;
    13.     public ColorAdjustments CA;
    14.    private VolumeParameter<float> SaTuRaTiOn = new VolumeParameter<float>();
    15.  
    16.     void Start()
    17.     {  
    18.         volume.profile.TryGet<ColorAdjustments>(out CA);
    19.         SaTuRaTiOn = SaturationSlider.value;
    20.  
    21.         PlayerPrefs.GetFloat("SaTuRaTiOn", SaTuRaTiOnValue);
    22.         SaturationSlider = SaTuRaTiOn;
    23.     }
    24.     public void ChangeSaturation(float SaTuRaTiOnValue)
    25.     {
    26.         CA.saturation.SetValue(SaTuRaTiOn);
    27.         PlayerPrefs.SetFloat("SaTuRaTiOn", SaTuRaTiOnValue);
    28.     }
    29. }
    I honestly have no idea what to do here. Can anyone please help me get this working?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,970
    I have no idea what you're having trouble with because you haven't said.

    How to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    For the Slider itself, go do slider tutorials on youtube until you understand connections to it perfectly. It's simple but there's a lot of moving parts to get right, so don't do it while trying to make something useful.

    For PlayerPrefs, rather than splattering your hostage-ransom-note-cased strings (eg, "SaTuRaTiOn") all over the code, this helps keep things cleaner:

    Here's an example of simple persistent loading/saving values using PlayerPrefs:

    https://gist.github.com/kurtdekker/01da815d2dfd336a925ae38019c3a163

    Useful for a relatively small number of simple values.
     
  3. LifeSymbiont

    LifeSymbiont

    Joined:
    Apr 19, 2015
    Posts:
    36
    I changed the code a little, I don't get any errors, now I can't move the slider at all and the saturation still won't change.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Rendering;
    5. using UnityEngine.Rendering.HighDefinition;
    6. using UnityEngine.UI;
    7.  
    8. public class SaturationChange : MonoBehaviour
    9. {
    10.     public Slider SaturationSlider;
    11.     public Volume volume;
    12.     public ColorAdjustments CA;
    13.     public float SaturationValue;
    14.     void Start()
    15.     {  
    16.         volume.profile.TryGet<ColorAdjustments>(out CA);
    17.         SaturationSlider.value = PlayerPrefs.GetFloat("SaTuRaTiOn", SaturationValue);
    18.     }
    19.     public void ChangeSaturation()
    20.     {
    21.         SaturationSlider.value = CA.saturation.value = SaturationValue;
    22.         PlayerPrefs.SetFloat("SaTuRaTiOn", SaturationValue);
    23.     }
    24. }
    25.  
    Please help me make it slide again and change. The min and max of the slider is set to -10 min and 10 max.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,970
    Is this the code the slider is calling?

    Because if so, then this line:

    is wiping out the value with whatever happens to be sitting in the SaturationValue variable. Do you ever change that value? The code above doesn't, so that alone would account for the value not changing.

    Again I say, work through a tutorial and understand the flow of data from the slider to whatever your uses are. This stuff is ultra simple basic core UI to code interactions, you gotta know it cold, and you can if you put the effort in. If you don't, you're only wasting your own time thrashing around with it.

    Here's one:

     
  5. LifeSymbiont

    LifeSymbiont

    Joined:
    Apr 19, 2015
    Posts:
    36
    Hello, thank you for your feedback, I managed to make it completely work.
    Thank you again.
     
    Kurt-Dekker likes this.