У меня есть игра, фон которой меняет цвет с течением времени, но это плоский цвет.
Есть ли способ применить эффект линейного градиента, не подвергая код слишком много рефакторинга?
Это то, что у меня есть (способ установки цвета):
public const float TRANSITION_TIME = 30;
public float transitionTimeElapsed = TRANSITION_TIME;
...
public Color[] backgroundColors;
public int colorIndex;
public int lastColorIndex;
...
public void PrepareBackgroundColors()
{
backgroundColors = new Color[4];
backgroundColors[0] = Color.black;
backgroundColors[1] = new Color(1, 0.85f, 0.62f); //Naranja
backgroundColors[2] = new Color(0.6f, 0.8f, 1); //Azul
backgroundColors[3] = new Color(1, 0.72f, 0.29f); //Naranja
colorIndex = Random.Range(0, 3);
if (colorIndex == 0)
lastColorIndex = 3;
else
lastColorIndex = colorIndex - 1;
}
...
private void UpdateBackground()
{
if (transitionTimeElapsed <= Time.deltaTime)
{
// start a new transition
transitionTimeElapsed = TRANSITION_TIME;
lastColorIndex = colorIndex;
colorIndex++;
if (colorIndex > backgroundColors.Length - 1)
{
colorIndex = 0;
}
Debug.Log(colorIndex + " - " + lastColorIndex);
}
else
{
// transition in progress
// calculate interpolated color
Camera.main.backgroundColor = Color.Lerp(backgroundColors[colorIndex], backgroundColors[lastColorIndex],
transitionTimeElapsed / TRANSITION_TIME);
// update the timer
transitionTimeElapsed -= Time.deltaTime;
}
}
Это игра Unity2D