62 lines
2.0 KiB
C#
62 lines
2.0 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using TMPro;
|
|
using UnityEngine.UI;
|
|
using static UnityEngine.UI.ContentSizeFitter;
|
|
using Cysharp.Threading.Tasks;
|
|
|
|
public class AggregateStation : MonoBehaviour
|
|
{
|
|
|
|
//public SpriteRenderer instruct;
|
|
public TMP_Text countInfo;
|
|
public TMP_Text dataInfo;
|
|
public SpriteRenderer instructTextBackground;
|
|
public float marginHorizontal;
|
|
public float marginVertical;
|
|
public bool isHaveValue;
|
|
|
|
public void SetInfo(string num, string data)
|
|
{
|
|
string cityName = transform.parent.name.Substring(0, 2);
|
|
dataInfo.text = $"{cityName}({num})";
|
|
countInfo.text = $"{data}<size=20>MW</size>";
|
|
gameObject.SetActive(num != "0");
|
|
isHaveValue = num != "0" ? true : false;
|
|
//Vector2 size = GetPreferredSize(dataInfo) + GetPreferredSize(countInfo);
|
|
//float x = size.x - marginHorizontal;
|
|
//float y = size.y - marginVertical;
|
|
//instructTextBackground.size = new Vector2(x, y);
|
|
|
|
|
|
}
|
|
private void OnEnable()
|
|
{
|
|
Vector2 size = GetPreferredSize(dataInfo) + GetPreferredSize(countInfo);
|
|
float x = size.x - marginHorizontal;
|
|
float y = size.y - marginVertical;
|
|
instructTextBackground.size = new Vector2(x, y);
|
|
}
|
|
public Vector2 GetPreferredSize(TMP_Text temp)
|
|
{
|
|
LayoutRebuilder.ForceRebuildLayoutImmediate(temp.GetComponent<RectTransform>());
|
|
return new Vector2((HandleSelfFittingAlongAxis(0, temp)), (HandleSelfFittingAlongAxis(1, temp)));
|
|
}
|
|
|
|
public float HandleSelfFittingAlongAxis(int axis, TMP_Text temp)
|
|
{
|
|
ContentSizeFitter c = temp.GetComponent<ContentSizeFitter>();
|
|
RectTransform r = temp.GetComponent<RectTransform>();
|
|
FitMode fitting = (axis == 0 ? c.horizontalFit : c.verticalFit);
|
|
if (fitting == FitMode.MinSize)
|
|
{
|
|
return LayoutUtility.GetMinSize(r, axis);
|
|
}
|
|
else
|
|
{
|
|
return LayoutUtility.GetPreferredSize(r, axis);
|
|
}
|
|
}
|
|
}
|