0

To be more specific, I want to change the color really fastly (like, 60 times a second) and I want to change it from blue, to red, to green, then back again and repeat that over and over

will0956
  • 41
  • 1
  • 8
  • To achieve a frame rate of 1000 fps you will need to disable vsync, otherwise the frame rate is tied to that of your monitor (which is unlikely to be more than 100 Hz). Turning off vsync is likely to make the image flickery and cause tearing. Furthermore, depending on your graphics hardware updating something at that frame rate may be impossible. Finally human persistence of vision means that a person is likely to see grey rather than individual colors if they are cycled that quickly. – Matt Coubrough Dec 10 '14 at 23:31
  • @MattCoubrough http://www.mmo-champion.com/threads/1572225-Mythbusting-quot-Human-eye-cannot-see-beyond-60-FPS-quot – will0956 Dec 10 '14 at 23:36
  • 1
    Switching those colors at a rate of 1000Hz will cause people to see a flickery mess of colors. Here's an example using javascript: http://jsfiddle.net/mattcoubrough/pc1t6kb5/ (what you see will depend on the ability of your computer and browser to update at 1000Hz which is unlikely) – Matt Coubrough Dec 10 '14 at 23:45
  • Nice example @MattCoubrough it is pretty useless for a real application! – Dinal24 Dec 11 '14 at 01:03

1 Answers1

1

If you really want to do this, (and I see no practical application for it at all as shown in my javascript demo) the following code will rapidly transition the background colors of the scene (once per frame).

Under camera properties, change the Clear Flags to Solid Color. This disables the skybox background and instead just clears the background to a color.

Then, create a new C# behaviour with the following code and attach it to your camera:

public class SkyColorBehaviourScript : MonoBehaviour {

    // used to track the index of the background to display
    public int cycleIndex = 0;
    Color[] skyColors = new Color[3];

    void Start () {
        // init the sky colors array
        skyColors [0] = new Color (255, 0, 0);    // red
        skyColors [1] = new Color (0, 255, 0);    // green 
        skyColors [2] = new Color (0, 0, 255);    // blue
    }

    // Update is called once per frame
    void Update () {
        // cycle the camera background color
        cycleIndex++;
        cycleIndex %= skyColors.Length;
        camera.backgroundColor = skyColors [cycleIndex];
    }
}

Explanation:

The script has an array skyColors containing three colors, red, green and blue.

At every update (once per frame) the variable cycleIndex is incremented.

Then, by calling cycleIndex %= skyColors.Length, whenever cycleIndex is equal to the length of the colors array it resets to zero. (This way if you add more colors to the array it will cycle through them too).

Finally, we change the camera's background color to the color in the array indexed by cycleIndex.

The default frame rate will probably be locked to the refresh rate of your monitor at around 60-100Hz but if you disable vsync you can probably set the target frame rate higher. Note however, that the updates will only run as fast as your graphics hardware can handle, and with vsync off you will experience that ugly "tearing" effect.

Alternative approach via Skybox Tinting

If for some reason you want to change a preset skybox's tint rather than changing the clear color of the active camera, you can use this version of the Update method:

    // Update is called once per frame
    void Update () {
        // cycle the camera background color
        cycleIndex++;
        cycleIndex %= skyColors.Length;
        RenderSettings.skybox.SetColor("_Tint", skyColors [cycleIndex]);
    }

Note that this assumes you've applied the skybox to all cameras via RenderSettings as opposed to a per camera Skybox. With this version the active camera's clear flags need to be set to skybox, and you're just changing the tint of the skybox, so some of the skybox texture may still be visible (ie. it won't be a pure red, blue and green background)

Caution: Both techniques are likely to induce an epileptic fit.

Matt Coubrough
  • 3,739
  • 2
  • 26
  • 40
  • Now, I get an error "Assets/ChangeSBColor.cs(21,41): error CS1061: Type `UnityEngine.Color[]' does not contain a definition for `length' and no extension method `length' of type `UnityEngine.Color[]' could be found (are you missing a using directive or an assembly reference?)" – will0956 Dec 12 '14 at 00:18
  • Also, If needed, here is what I'm importing `using UnityEngine;` and `using System.Collections;` – will0956 Dec 12 '14 at 00:26
  • Dont get the error, lets me into it, but doesn't change the skybox color – will0956 Dec 16 '14 at 23:38
  • I have clear flags set to solid color, and it still won't work. – will0956 Dec 17 '14 at 00:10
  • 1
    I have added an explanation of how to change the tint of the RenderSettings skybox as an alternative approach. Both techniques work just fine for me and it's a direct cut and paste of my code so I really don't know how to help you further. Check that the code in your behaviour is attached to your camera, and is executing. Are you getting any errors? With the 1st approach does the camera background change color when looking at it in the property inspector? – Matt Coubrough Dec 17 '14 at 00:12
  • Never mind, I figured out why it wasn't working... I never attached it to the camera – will0956 Dec 17 '14 at 00:14