2 using UnityEngine.Serialization;
10 [FormerlySerializedAs(
"fillPercentage")] [Range(0, 1)]
11 public float FillPercentage = 0.4f;
13 [FormerlySerializedAs(
"startColor")]
public Color StartColor = Color.green;
14 [FormerlySerializedAs(
"middleColor")]
public Color MiddleColor = Color.yellow;
15 [FormerlySerializedAs(
"endColor")]
public Color EndColor = Color.red;
16 [FormerlySerializedAs(
"textColor")]
public Color TextColor = Color.blue;
17 [FormerlySerializedAs(
"usePercentageText")]
public bool UsePercentageText;
18 [FormerlySerializedAs(
"text")]
public string Text =
"100";
20 private GameObject fillBar;
22 private RectTransform rectTransform;
23 private RectTransform fillBarRect;
24 private Image fillBarImage;
29 fillBar = transform.Find(
"Bar").gameObject;
30 barText = transform.Find(
"Text").gameObject.GetComponent<Text>();
31 barText.color = TextColor;
32 barText.fontStyle = FontStyle.Bold;
33 rectTransform = GetComponent<RectTransform>();
34 fillBarRect = fillBar.GetComponent<RectTransform>();
35 fillBarImage = fillBar.GetComponent<Image>();
41 var w = rectTransform.rect.width;
43 fillBarRect.offsetMin =
new Vector2(0, 0);
44 fillBarRect.offsetMax =
new Vector2((FillPercentage - 1) * w, 0);
46 if (FillPercentage < 0.5)
48 fillBarImage.color = Color.Lerp(StartColor, MiddleColor, FillPercentage * 2);
52 fillBarImage.color = Color.Lerp(MiddleColor, EndColor, (FillPercentage - 0.5f) * 2);
55 if (UsePercentageText)
57 barText.text = ((int)(FillPercentage * 100.0f)) +
"%";