Skip to content

Instantly share code, notes, and snippets.

@adamski11
Last active July 3, 2020 11:12
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamski11/22c34da5c4a5e71819c09ca91bab3a96 to your computer and use it in GitHub Desktop.
Save adamski11/22c34da5c4a5e71819c09ca91bab3a96 to your computer and use it in GitHub Desktop.
Add this script to any game object with an image component you're using as a fill bar colour the bar based on the current fill amount and a gradient set by you. Works in the editor for testing as well.
using UnityEngine;
using UnityEngine.UI;
[RequireComponent(typeof(Image))]
[ExecuteInEditMode]
public class FillBarGradientScript : MonoBehaviour
{
public Gradient gradient;
Image fillImage;
void Start()
{
fillImage = this.GetComponent<Image>();
fillImage.fillAmount = 1;
}
void Update()
{
if (fillImage == null)
fillImage = this.GetComponent<Image>();
else
fillImage.color = gradient.Evaluate(fillImage.fillAmount);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment