Space-Smash-Out/Assets/Scripts/UI/SegmentIndicator.cs

51 lines
914 B
C#

using System;
using System.Collections;
using System.Collections.Generic;
using Unity.Mathematics;
using UnityEngine;
using UnityEngine.UI;
[ExecuteInEditMode]
public class SegmentIndicator : MonoBehaviour
{
public List<Image> SegmentImages;
public Color color;
private float _currentFill = 1;
void Start()
{
foreach (Image i in SegmentImages)
{
i.color = color;
}
}
public void SetFill(float value)
{
_currentFill = value;
float totalResultFill = value * SegmentImages.Count;
int targetSegment = (int)Math.Truncate(totalResultFill);
float targetSegmentFill = totalResultFill - targetSegment;
for (int i = 0; i < SegmentImages.Count; i++)
{
if (i < targetSegment)
{
SegmentImages[i].fillAmount = 1;
continue;
}
if (i > targetSegment)
{
SegmentImages[i].fillAmount = 0;
continue;
}
SegmentImages[i].fillAmount = targetSegmentFill;
}
}
}