using System.Collections; using System.Collections.Generic; using UnityEngine; public class ColorInterpolation : MonoBehaviour { public Color startColor; public Color endColor; private void OnGUI() { if (GUILayout.Button("Over 5 seconds")) //interpolate from A to B in 5 seconds StartCoroutine(TransitionOverDuration(5)); if (GUILayout.Button("Half spectrum per second")) //interpolate from white to black in 2 seconds StartCoroutine(TransitionOverDuration(0.5f)); } IEnumerator TransitionOverDuration(float duration) { float startTime = Time.time; float endTime = startTime + duration; Color c = startColor; while(Time.time < endTime) { //remap Time.time to 0-->1 range (or p for percentage completion) float p = Mathf.InverseLerp(startTime, endTime, Time.time); c = Color.Lerp(startColor, endColor, p); Debug.Log($"Color! {Time.time.ToString("f2")}"); yield return null; } } IEnumerator TransitionBySpeed(float speed) { Color c = startColor; while (c != endColor) { //cool usage of Vector4! For what is a color if not but 4 floats!? c = Vector4.MoveTowards(c, endColor, speed * Time.deltaTime); Debug.Log($"Color! {Time.time.ToString("f2")}"); yield return null; } } }