You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
67 lines
1.9 KiB
67 lines
1.9 KiB
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class SeperatedBar : MonoBehaviour
|
|
{
|
|
[SerializeField] RectTransform spliterRoot;
|
|
[SerializeField] Image spliterPrefab;
|
|
[SerializeField] Color defaultSpliterColor;
|
|
[SerializeField] Color currentSpliterColor;
|
|
|
|
[SerializeField] ExtendSlider progressBar;
|
|
|
|
public int MaxCount { get; private set; }
|
|
public int CurrentCount { get; private set; }
|
|
|
|
public void Setup(int maxCount)
|
|
{
|
|
Debug.Assert(maxCount > 0, "maxCount is less than 1");
|
|
Debug.Assert(spliterRoot != null, "spliterRoot is null");
|
|
|
|
foreach (RectTransform root in spliterRoot)
|
|
{
|
|
Destroy(root.gameObject);
|
|
}
|
|
|
|
MaxCount = maxCount;
|
|
|
|
if(spliterPrefab != null)
|
|
{
|
|
float splitInterval = spliterRoot.rect.width;
|
|
if(maxCount > 1)
|
|
splitInterval /= (maxCount - 1);
|
|
|
|
for (int i = 0; i < maxCount; i++)
|
|
{
|
|
var spliter = Instantiate(spliterPrefab, spliterRoot);
|
|
spliter.color = defaultSpliterColor;
|
|
|
|
var rt = spliter.transform as RectTransform;
|
|
rt.anchorMin = new Vector2(0, 0.5f);
|
|
rt.anchorMax = new Vector2(0, 0.5f);
|
|
rt.anchoredPosition = new Vector2(i * splitInterval, 0);
|
|
}
|
|
}
|
|
|
|
progressBar?.SetRateImmediate(0f);
|
|
}
|
|
|
|
public void SetCurrentCount(int count)
|
|
{
|
|
Debug.Assert(count >= 0 && count <= MaxCount, "count is out of range");
|
|
|
|
CurrentCount = count;
|
|
|
|
for (int i = 0; i < spliterRoot.childCount; i++)
|
|
{
|
|
var spliter = spliterRoot.GetChild(i).GetComponent<Image>();
|
|
spliter.color = (i + 1) == count ? currentSpliterColor : defaultSpliterColor;
|
|
}
|
|
|
|
float rate = 1f;
|
|
if(MaxCount > 1)
|
|
rate = (count - 1f) / (MaxCount - 1f);
|
|
|
|
progressBar?.SetRateImmediate(rate);
|
|
}
|
|
}
|