Skip to content

Instantly share code, notes, and snippets.

@buijldert
Created September 28, 2017 09:19
Show Gist options
  • Save buijldert/cc787849031565d616d8678b0e7bf6a8 to your computer and use it in GitHub Desktop.
Save buijldert/cc787849031565d616d8678b0e7bf6a8 to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ColorLerp : MonoBehaviour
{
[SerializeField]
private Image _imageToLerp;
[SerializeField]
private bool _startsLerping;
private Color lerpedColor = Color.white;
[SerializeField]
public Color32 startColor = new Color32(66, 134, 244, 75);
[SerializeField]
public Color32 endColor = new Color32(10, 57, 132, 75);
private Coroutine _lerpColorCoroutine;
private void Start()
{
if(_startsLerping)
{
StartCoroutine(LerpColor());
}
}
private IEnumerator LerpColor()
{
lerpedColor = Color.Lerp(startColor, endColor, Mathf.PingPong(Time.time, 1));
_imageToLerp.color = lerpedColor;
yield return null;
_lerpColorCoroutine = StartCoroutine(LerpColor());
}
public void ControlLerp(bool isLerping)
{
_imageToLerp.enabled = isLerping;
if (isLerping)
{
_lerpColorCoroutine = StartCoroutine(LerpColor());
}
else
{
StopCoroutine(_lerpColorCoroutine);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment