Files
ChunilENG/Assets/WorkSpace/Personal/JYM/UI_StackBar.cs

58 lines
1.7 KiB
C#
Raw Normal View History

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;
using WI;
public class UI_StackBar : UIBase
{
public Image targetImage;
public List<float> segmentRatios = new List<float>();
public Image[] stackedImages;
public override void AfterAwake()
{
targetImage = GetComponent<Image>();
var images = transform.GetComponentsInChildren<Image>();
stackedImages = images.Where(t => t != targetImage).ToArray();
}
public void SetStackBar(BarChartData barChartData, Vector3 topPosition, float height)
{
SetActive(true);
rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, height);
transform.position = topPosition;
SetImageRatios(barChartData);
}
private void SetImageRatios(BarChartData barChartData)
{
segmentRatios.Clear();
segmentRatios.Add(barChartData.etc);
segmentRatios.Add(barChartData.holdingPressureTime);
segmentRatios.Add(barChartData.weighingTime);
segmentRatios.Add(barChartData.coolingTime);
var parentHeight = targetImage.rectTransform.rect.height;
var totalHeight = 0f;
foreach (var height in segmentRatios)
{
totalHeight += height;
}
AdjustChildImagesHeight(parentHeight, totalHeight);
}
void AdjustChildImagesHeight(float parentHeight, float totalHeight)
{
for (int i = 0; i < stackedImages.Length; i++)
{
var rectTrnasform = stackedImages[i].rectTransform;
var newHeight = (segmentRatios[i] / totalHeight) * parentHeight;
rectTrnasform.sizeDelta = new Vector2(rectTrnasform.sizeDelta.x, newHeight);
}
}
}