Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Nolirneen · Apr 13, 2011 at 06:42 AM · colorlightslider

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! =)

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Nolirneen · Apr 14, 2011 at 05:21 AM 0
Share

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? =)

avatar image DaveA · Apr 14, 2011 at 07:35 AM 0
Share

You can feed any fraction into lerp, whether it's time or distance or whatever.

avatar image Nolirneen · Apr 15, 2011 at 06:14 AM 0
Share

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.

avatar image DaveA · Apr 15, 2011 at 05:15 PM 0
Share

Added code, above

avatar image Nolirneen · Apr 18, 2011 at 07:01 AM 0
Share

Alright it works! I wanted the light to change between 4 colors, but I'll try to figure it out. Thanks again! =)

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges