- Home /
GUI slider controls sunlight color?
Hello, I'd like to ask how do I change the color of a light (sunlight) via a slider?
Say, I have a specific set of colors that I want the slider to change the light's color to when it reaches a certain value.
Example: At value 0, the color is light blue; at value 50, the color is orange; at value 100, the color is purple.
I came up with this script, but it doesn't seem to work; the color doesn't really change. If someone could point me in the right direction on how to work around this?
var lights : Light[]; var Sunlight : Light; var SunColorSliderValue : float = 0.0; var SunColors : Color[]; var guiSkin : GUISkin; var toggleBoolSUN : boolean = true; private var toggleRect = Rect(15, 20, 135, 45);
function OnGUI ()
{ GUILayout.BeginArea( Rect( 10, 410, 240, 180 ),GUI.skin.window);
toggleBoolSUN = GUI.Toggle(toggleRect, toggleBoolSUN, "Sunlight", guiSkin.button);
lights[0].enabled = toggleBoolSUN;
if(toggleBoolSUN)
{
SunColorSliderValue = GUI.VerticalSlider(Rect(185,65,30,100), SunColorSliderValue, 0, 100);
}
Sunlight.color = SunColorSliderValue;
if(SunColorSliderValue == 0)
{
Sunlight.color = SunColour[0];
}
if(SunColorSliderValue == 50)
{
Sunlight.color = SunColour[1];
}
if(SunColorSliderValue == 100)
{
Sunlight.color = SunColour[2];
}
GUILayout.EndArea(); }
Thanks for the help! =)
Answer by DaveA · Apr 13, 2011 at 07:08 AM
If you want a smooth transition, look into the Color.Lerp function. http://unity3d.com/support/documentation/ScriptReference/Color.Lerp.html
if (SunColorSliderValue < 50)
Sunlight.color = Color.Lerp(SunColour[0], SunColour[1], SunColorSliderValue/50);
else
Sunlight.color = Color.Lerp(SunColour[1], SunColour[2], (SunColorSliderValue-50)/50);
I have some VRML utilities that include a sorta multi-interpolator that allows as many different colors as you want, two lines of code, I should publish them.
Hmm ok, I just checked out the lerp function on the script reference, but according to what I see, it's only possible to change the color smoothly as time passes. I want to change the color smoothly through a slider. Perhaps you could show me how? =)
You can feed any fraction into lerp, whether it's time or distance or whatever.
Could you show me how, please? I'm new to program$$anonymous$$g and I've tried everything I could, and I just keep getting compiling errors.
Alright it works! I wanted the light to change between 4 colors, but I'll try to figure it out. Thanks again! =)
Answer by Uchiha Itachi · May 09, 2014 at 04:41 AM
For the slider control, I am also not quite familiar. I previously studied something on the UI slider control, however, the C# tutorial for slider control make me feel so complicated. Don't know if that kind of thing be helpful to you. The proper answer here offered me much insights. I picked up the courage to continue digging into the GUI slider.
Your answer
Follow this Question
Related Questions
Light, halo and color 1 Answer
Change Slider Color and add a custom Slider icon 1 Answer
light being colored 1 Answer
Changing light colour makes it too bright 3 Answers
Make a Lava Object Shiny 1 Answer