Files
Ultimate-Fishing-Simulator-…/Assets/Scripts/Assembly-CSharp/Michsky/UI/Heat/ProgressBar.cs
2026-03-04 09:37:33 +08:00

176 lines
3.2 KiB
C#

using System;
using TMPro;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;
namespace Michsky.UI.Heat
{
[ExecuteInEditMode]
public class ProgressBar : MonoBehaviour
{
[Serializable]
public class OnValueChanged : UnityEvent<float>
{
}
public enum BarDirection
{
Left = 0,
Right = 1,
Top = 2,
Bottom = 3,
Custom = 4
}
public Sprite icon;
public float currentValue;
public float minValue;
public float maxValue = 100f;
public float minValueLimit;
public float maxValueLimit = 100f;
public Image barImage;
[SerializeField]
private Image iconObject;
[SerializeField]
private Image altIconObject;
[SerializeField]
private TextMeshProUGUI textObject;
[SerializeField]
private TextMeshProUGUI altTextObject;
public bool addPrefix;
public bool addSuffix;
[Range(0f, 5f)]
public int decimals;
public string prefix = "";
public string suffix = "%";
public BarDirection barDirection;
public OnValueChanged onValueChanged;
[HideInInspector]
public Slider eventSource;
private void Start()
{
Initialize();
UpdateUI();
}
public void UpdateUI()
{
if (barImage != null)
{
barImage.fillAmount = currentValue / maxValue;
}
if (iconObject != null)
{
iconObject.sprite = icon;
}
if (altIconObject != null)
{
altIconObject.sprite = icon;
}
if (textObject != null)
{
UpdateText(textObject);
}
if (altTextObject != null)
{
UpdateText(altTextObject);
}
if (eventSource != null)
{
eventSource.value = currentValue;
}
}
private void UpdateText(TextMeshProUGUI txt)
{
if (addSuffix)
{
txt.text = currentValue.ToString("F" + decimals) + suffix;
}
else
{
txt.text = currentValue.ToString("F" + decimals);
}
if (addPrefix)
{
txt.text = prefix + txt.text;
}
}
public void Initialize()
{
if (Application.isPlaying && onValueChanged.GetPersistentEventCount() != 0)
{
if (eventSource == null)
{
eventSource = base.gameObject.AddComponent(typeof(Slider)) as Slider;
}
eventSource.transition = Selectable.Transition.None;
eventSource.minValue = minValue;
eventSource.maxValue = maxValue;
eventSource.onValueChanged.AddListener(onValueChanged.Invoke);
}
SetBarDirection();
}
private void SetBarDirection()
{
if (barImage != null)
{
barImage.type = Image.Type.Filled;
if (barDirection == BarDirection.Left)
{
barImage.fillMethod = Image.FillMethod.Horizontal;
barImage.fillOrigin = 0;
}
else if (barDirection == BarDirection.Right)
{
barImage.fillMethod = Image.FillMethod.Horizontal;
barImage.fillOrigin = 1;
}
else if (barDirection == BarDirection.Top)
{
barImage.fillMethod = Image.FillMethod.Vertical;
barImage.fillOrigin = 1;
}
else if (barDirection == BarDirection.Bottom)
{
barImage.fillMethod = Image.FillMethod.Vertical;
barImage.fillOrigin = 0;
}
}
}
public void ClearEvents()
{
eventSource.onValueChanged.RemoveAllListeners();
}
public void SetValue(float newValue)
{
currentValue = newValue;
UpdateUI();
}
}
}