- Home /
Manage gamma with a slider in a 2D game
Hello everyone;
I 'm working on 2D game in Unity and I' m wondering how we can manage brightness with a slider which is in the option menu of my game. I wanted to know what are the steps to solve this problem.
Regards
t-yacine
Answer by Vega4Life · Nov 01, 2018 at 02:47 PM
You could try changing RenderSettings.ambientlight. Have a slider from 0 to 1. Then create a new color with that value and apply it. i.e RenderSettings.ambientlight = new Color(sliderValue, sliderValue, sliderValue, 1.0f);
This what I did but it doesn't seems to work (I didn't create a new color). First, i wrote this code : using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Brightness : $$anonymous$$onoBehaviour { float rgbValue = 0.5f;
public void onGUI()
{
rgbValue = GUI.HorizontalSlider(new Rect(Screen.width / 2 - 50, 90, 100, 90), rgbValue, 0f, 1.0f);
RenderSettings.ambientLight = new Color(rgbValue, rgbValue, rgbValue, 1);
}
} I add this component to the directionnal light. However, when I want to apply this funtion to the slider (the slider is in the option menu) in the "on value changed ", I can't find the function i'v coded. So I can't try if my code is correct. Could give a solution to solve my problem.
Regards.
t-yacine
At a quick glance you have onGUI(), it's actually OnGUI().
I would just add a GameObject -> UI -> Slider (makes it easier than using OnGUI() Once you have that, whatever script you are using, just create a function of:
public void OnSliderChange(UnityEngine.UI.Slider slider)
{
RenderSettings.ambientLight = new Color(slider.value, slider.value, slider.value, 1.0f);
}
Then link the script you are using, and the function call to the On Value Changed. Also, link the Slider component to send across.
Hello,
I've tried your solution but it doesn't work, I can attach my script to the slider. I've added my script to my canvas first, then i've tried with the directional light of my menu and finally with the panel, but it doesn't work. N.B : there is no directional light in the game himself but only in the menu.
Regards
t-yacine
Answer by T-yacine · Nov 01, 2018 at 04:16 PM
This what I did but it doesn't seems to work. First, i wrote this code : using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Brightness : MonoBehaviour { float rgbValue = 0.5f;
public void onGUI() { rgbValue = GUI.HorizontalSlider(new Rect(Screen.width / 2 - 50, 90, 100, 90), rgbValue, 0f, 1.0f); RenderSettings.ambientLight = new Color(rgbValue, rgbValue, rgbValue, 1); } } I add this component to the directionnal light. However, when I want to apply this funtion to the slider (the slider is in the option menu) in the "on value changed ", I can't find the function i'v coded. So I can't try if my code is correct. Could give a solution to solve my problem.
Regards.
t-yacine