| | 1 | | using TMPro; |
| | 2 | | using UnityEngine; |
| | 3 | |
|
| | 4 | | [RequireComponent(typeof(RectTransform))] |
| | 5 | | public class LoadingBar : MonoBehaviour |
| | 6 | | { |
| 0 | 7 | | public float currentPercentage { get; set; } |
| | 8 | |
|
| | 9 | | [SerializeField] internal string textPreviousToPercentage; |
| | 10 | | [SerializeField] internal TMP_Text percentageText; |
| | 11 | | [SerializeField] internal RectTransform loadingBar; |
| | 12 | |
|
| | 13 | | private float maxBarWidth; |
| 41 | 14 | | private string percentagePreText = ""; |
| | 15 | |
|
| | 16 | | private void Awake() |
| | 17 | | { |
| 19 | 18 | | maxBarWidth = ((RectTransform)transform).rect.size.x; |
| | 19 | |
|
| 19 | 20 | | if (!string.IsNullOrEmpty(textPreviousToPercentage)) |
| 16 | 21 | | percentagePreText = $"{textPreviousToPercentage} "; |
| 19 | 22 | | } |
| | 23 | |
|
| 12 | 24 | | public void SetActive(bool isActive) { gameObject.SetActive(isActive); } |
| | 25 | |
|
| | 26 | | public void SetPercentage(float newValue) |
| | 27 | | { |
| 0 | 28 | | currentPercentage = newValue; |
| 0 | 29 | | percentageText.text = $"{percentagePreText}{newValue.ToString("0")}%"; |
| 0 | 30 | | loadingBar.sizeDelta = new Vector2(newValue * maxBarWidth / 100f, loadingBar.sizeDelta.y); |
| 0 | 31 | | } |
| | 32 | | } |