holopy3/Assets/Scripts/FadingColors.cs

84 lines
1.9 KiB
C#
Raw Permalink Normal View History

2021-01-28 12:07:52 +00:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class FadingColors : MonoBehaviour
{
[SerializeField] Color color1;
[SerializeField] Color color2;
[SerializeField] Color color3;
[SerializeField] float interval;
private Gradient gradient;
private GradientColorKey[] colorKey;
private GradientAlphaKey[] alphaKey;
private float time;
private bool timePlus;
private MeshRenderer m;
private Button button;
private ColorBlock cb;
private Image image;
private Color col;
void Start()
{
time = 0f;
timePlus = true;
gradient = new Gradient();
// Populate the color keys at the relative time 0 and 1 (0 and 100%)
colorKey = new GradientColorKey[3];
colorKey[0].color = color1;
colorKey[0].time = 0.0f;
colorKey[1].color = color2;
colorKey[1].time = 0.5f;
colorKey[2].color = color3;
colorKey[2].time = 1.0f;
// Populate the alpha keys at relative time 0 and 1 (0 and 100%)
alphaKey = new GradientAlphaKey[3];
alphaKey[0].alpha = 0.0f;
alphaKey[0].time = 0.0f;
alphaKey[1].alpha = 0.5f;
alphaKey[1].time = 0.5f;
alphaKey[2].alpha = 1.0f;
alphaKey[2].time = 1.0f;
gradient.SetKeys(colorKey, alphaKey);
// What's the color at the relative time 0.25 (25 %) ?
Debug.Log(gradient.Evaluate(0.25f));
m = GetComponent<MeshRenderer>();
}
private void Update()
{
if (timePlus)
{
time += Time.deltaTime;
if (time > interval)
{
timePlus = false;
}
}
if (!timePlus)
{
time -= Time.deltaTime;
if (time < 0)
{
timePlus = true;
}
}
m.material.color = gradient.Evaluate(time / interval);
}
}