- Home /
Change color gradually?
Okay so I have the following script:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class ChangeColor : MonoBehaviour
{
Material target;
public List<Color> colors;
public bool useDefaultColorSet = false;
ColorSet colorSet;
void Awake()
{
if (useDefaultColorSet)
colorSet = GameObject.FindObjectOfType<ColorSet>();
target = gameObject.GetComponent<Renderer>().material;
target.color = getRandomColor();
}
Color getRandomColor()
{
if (useDefaultColorSet)
return colorSet.colors[Random.Range(0, colorSet.colors.Count)];
return colors[Random.Range(0, colors.Count)];
}
}
And I would like for there to be a gradient area between the two colors, because right now it's fairly harsh. It's applying these different colors to a procedurally generated object in segments, so I was wondering if there's a way to use a gradient shift between them? In looking this up I saw something about color lerp but I don't really know if it could be used here, it seems like it shifts color over time mostly.
No, Color.Lerp is just a way of interpolating colours. It has nothing intrinsically to do with time.
So if you have 2 colours and want a colour halfway between them you do
Color halfway = Color.Lerp( colour1, colour2, 0.5f);
And you can use this to get colours any fraction of the way between them.
Sure, you can use this to change a single colour over time, from colour2 to colour2. But you can also use it just to generate a set of colours that vary between colour1 and colour2.
Answer by The-Little-Guy · Oct 26, 2015 at 06:12 PM
You might be interested in using a Gradient. With this example, I create a gradient property and a duration property that you can edit it the inspector. You can use them to set the colors of the gradient with however many colors you would like, and the duration as well.
In this example I change the background color of the Main Camera.
using UnityEngine;
public class ColorChange : MonoBehaviour {
[SerializeField]
Gradient gradient;
[SerializeField]
float duration;
float t = 0f;
void Update() {
float value = Mathf.Lerp(0f, 1f, t);
t += Time.deltaTime / duration;
Color color = gradient.Evaluate(value);
Camera.main.backgroundColor = color;
}
}
If we add Start() we can generate a gradient on the fly like this:
void Start(){
var colorkeys = new GradientColorKey[2];
var alphakeys = new GradientAlphaKey[2];
colorkeys[0].color = new Color(Random.Range(0f, 1f), Random.Range(0f, 1f), Random.Range(0f, 1f));
colorkeys[0].time = 0f;
colorkeys[1].color = new Color(Random.Range(0f, 1f), Random.Range(0f, 1f), Random.Range(0f, 1f));
colorkeys[1].time = 1f;
alphakeys[0].alpha = 1f;
alphakeys[0].time = 0f;
alphakeys[1].alpha = 1f;
alphakeys[1].time = 1f;
gradient.SetKeys(colorkeys, alphakeys);
}
And the last way that I can think of is to use 2 colors and lerp between them.
using UnityEngine;
public class ColorChange : MonoBehaviour {
[SerializeField]
float duration;
float t = 0f;
Color color1, color2;
void Start() {
color1 = new Color(Random.Range(0f, 1f), Random.Range(0f, 1f), Random.Range(0f, 1f));
color2 = new Color(Random.Range(0f, 1f), Random.Range(0f, 1f), Random.Range(0f, 1f));
}
void Update() {
Color color = Color.Lerp(color1, color2, t);
t += Time.deltaTime / duration;
Camera.main.backgroundColor = color;
}
}
Answer by sysmaya · Jan 30, 2017 at 10:48 AM
Fade IN - Fade OUT Change smoothed betwen 2 Colors.
using UnityEngine;
using System.Collections;
using System;
public class BlinkColor : MonoBehaviour {
public Color colorIni = Color.white;
public Color colorFin = Color.red;
public float duration = 3f;
Color lerpedColor = Color.white;
private float t=0;
private bool flag;
Renderer _renderer;
// Use this for initialization
void Start () {
_renderer=GetComponent<Renderer>();
}
void Update() {
lerpedColor = Color.Lerp(colorIni, colorFin, t);
_renderer.material.color = lerpedColor;
if (flag == true) {
t -= Time.deltaTime/duration;
if (t < 0.01f)
flag = false;
} else {
t += Time.deltaTime/duration;
if (t > 0.99f)
flag = true;
}
}
}
Answer by chillandcodegames · Aug 27, 2021 at 10:10 AM
Use this if you are using an Image
using UnityEngine;
using System.Collections;
using System;
using UnityEngine.UI;
public class BlinkColor : MonoBehaviour
{
public Color colorIni = Color.white;
public Color colorFin = Color.red;
public float duration = 3f;
Color lerpedColor = Color.white;
private float t = 0;
private bool flag;
Image _renderer;
// Use this for initialization
void Start()
{
_renderer = GetComponent<Image>();
}
void Update()
{
lerpedColor = Color.Lerp(colorIni, colorFin, t);
_renderer.color = lerpedColor;
if (flag == true)
{
t -= Time.deltaTime / duration;
if (t < 0.01f)
flag = false;
}
else
{
t += Time.deltaTime / duration;
if (t > 0.99f)
flag = true;
}
}
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Implementation of directional buildings? 1 Answer
Illuminating a 3D object's edges OnMouseOver (script in c#)? 1 Answer