Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Cycling through the color spectrum

Discussion in 'Scripting' started by theinfomercial, Apr 23, 2011.

Thread Status:
Not open for further replies.
  1. theinfomercial

    theinfomercial

    Joined:
    Sep 9, 2008
    Posts:
    1,000
    Is there a way to animate a color using the color spectrum rather than the RGB float array? The spectrum is that bar in the color inspector that you can change to cycle through the saturation colors. I want to animate an object's color from red, to "opposite end of the spectrum" red, shifting through the other colors as it animates.

    For example, open the color inspector in a color variable (like the color variable you would find in a material), and make the RGB values be 255/255,0,0. Then, move the spectrum bar from bottom to top. This will create an animating rainbow effect that I want.

    How would one go about scripting this?
     
  2. BlackMantis

    BlackMantis

    Joined:
    Feb 7, 2010
    Posts:
    1,475
    You can probably do this with a shader.
    How come you don't want to just slide the slider just a little and copy the numbers to an array then call them back at a set speed.
    Probably only take a couple of lines of code once the array was setup.
     
  3. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
  4. theinfomercial

    theinfomercial

    Joined:
    Sep 9, 2008
    Posts:
    1,000
    I guess what I'm actually looking for is a way to animate the tint color of a material to go through the colors of the rainbow. The particle animator has this ability, how does one animate the colors through scripting the same way the particle animator does?
     
  5. WesleyE

    WesleyE

    Joined:
    Mar 2, 2011
    Posts:
    3
    Convert the color to HSB and change the angle of the hue. That way you can only change the tint (Or hue) of the color while maintaining the saturation and brightness.

    I've made a script that can do this here:
    http://wmcity.nl/scripts.php?actie=bekijk&id=2035

    It's for PHP but you can easily adapt it for C# or Javascript yourself.
     
  6. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    Or, you could use the script that's already in C#. It would be so cool if someone had linked to it above! :eek:
     
    screamingcolor likes this.
  7. theinfomercial

    theinfomercial

    Joined:
    Sep 9, 2008
    Posts:
    1,000
  8. gavinoz

    gavinoz

    Joined:
    Oct 6, 2018
    Posts:
    1
    The website attached to that link is no longer viewable by people. Do you happen to have a copy of the RGB cycle code?
     
  9. amirmehmood

    amirmehmood

    Joined:
    Mar 31, 2021
    Posts:
    9
    Here is a script to make go through the color spectrum.

    Code (CSharp):
    1. public class ColorChange : MonoBehaviour
    2. {
    3.  
    4.     [SerializeField] Renderer randerObject;
    5.     [SerializeField] float seconds;
    6.     float timer = 0.0f;
    7.     bool blueToGreen = true;
    8.     bool greenToRed = false;
    9.     bool redToBlue = false;
    10.  
    11.     // Update is called once per frame
    12.     void Update()
    13.     {
    14.        
    15.         timer += Time.deltaTime/seconds;
    16.  
    17.         if (blueToGreen == true && greenToRed == false && redToBlue == false)
    18.         {
    19.             randerObject.material.color = Color.Lerp(Color.blue, Color.green, timer);
    20.             if(timer >= 1.0f)
    21.             {
    22.                 timer = 0.0f;
    23.                 blueToGreen = false;
    24.                 greenToRed = true;
    25.             }
    26.         }
    27.  
    28.         if (greenToRed == true && blueToGreen == false && redToBlue == false)
    29.         {
    30.             randerObject.material.color = Color.Lerp(Color.green, Color.red, timer);
    31.             if (timer >= 1.0f)
    32.             {
    33.                 timer = 0.0f;
    34.                 greenToRed = false;
    35.                 redToBlue = true;
    36.             }
    37.         }
    38.  
    39.         if (redToBlue == true && greenToRed == false && blueToGreen == false)
    40.         {
    41.             randerObject.material.color = Color.Lerp(Color.red, Color.blue, timer);
    42.             if (timer >= 1.0f)
    43.             {
    44.                 timer = 0.0f;
    45.                 redToBlue = false;
    46.                 blueToGreen = true;
    47.             }
    48.         }
    49.     }
    50. }
    51.  
     
    hessex likes this.
  10. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,321
    Please don't necro really old threads, this is from 2011. Let's close this thread, it's run its course.
     
Thread Status:
Not open for further replies.