162 lines
3.6 KiB
C#
162 lines
3.6 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using TMPro;
|
|
|
|
/*
|
|
//Set properties in C# example codes.
|
|
using RainbowArt.CleanFlatUI;
|
|
public class ProgressBarSpecialDemo : MonoBehaviour
|
|
{
|
|
//The ProgressBarSpecial Component.
|
|
public ProgressBarSpecial m_ProgressBar;
|
|
void Start()
|
|
{
|
|
//Set the current value.
|
|
m_ProgressBar.CurrentValue = 50;
|
|
//Set the maximum value.
|
|
m_ProgressBar.MaxValue = 100;
|
|
//Set whether to show the text.
|
|
m_ProgressBar.HasText = true;
|
|
}
|
|
}
|
|
*/
|
|
|
|
namespace RainbowArt.CleanFlatUI
|
|
{
|
|
[ExecuteAlways]
|
|
public class ProgressBarSpecial : MonoBehaviour
|
|
{
|
|
[SerializeField]
|
|
float currentValue = 0f;
|
|
|
|
[SerializeField]
|
|
float maxValue = 100.0f;
|
|
|
|
[SerializeField]
|
|
bool hasText = true;
|
|
|
|
[SerializeField]
|
|
TextMeshProUGUI text;
|
|
|
|
[SerializeField]
|
|
RectTransform foreground;
|
|
|
|
[SerializeField]
|
|
RectTransform foregroundArea;
|
|
|
|
bool bDelayedUpdate = false;
|
|
|
|
public float CurrentValue
|
|
{
|
|
get => currentValue;
|
|
set
|
|
{
|
|
if(currentValue == value)
|
|
{
|
|
return;
|
|
}
|
|
currentValue = value;
|
|
OnValueChanged();
|
|
}
|
|
}
|
|
|
|
public float MaxValue
|
|
{
|
|
get => maxValue;
|
|
set
|
|
{
|
|
if(maxValue == value)
|
|
{
|
|
return;
|
|
}
|
|
maxValue = value;
|
|
OnValueChanged();
|
|
}
|
|
}
|
|
|
|
public bool HasText
|
|
{
|
|
get => hasText;
|
|
set
|
|
{
|
|
if (hasText == value)
|
|
{
|
|
return;
|
|
}
|
|
hasText = value;
|
|
UpdateText();
|
|
}
|
|
}
|
|
|
|
void OnValueChanged()
|
|
{
|
|
if(maxValue < 0)
|
|
{
|
|
maxValue = 100.0f;
|
|
}
|
|
if(currentValue < 0)
|
|
{
|
|
currentValue = 0f;
|
|
}
|
|
currentValue = Mathf.Clamp(currentValue, 0, maxValue);
|
|
UpdateGUI();
|
|
}
|
|
|
|
void Start(){
|
|
UpdateGUI();
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
if(Application.isPlaying)
|
|
{
|
|
UpdateGUI();
|
|
}
|
|
else
|
|
{
|
|
if(bDelayedUpdate)
|
|
{
|
|
bDelayedUpdate = false;
|
|
OnValueChanged();
|
|
}
|
|
}
|
|
}
|
|
|
|
void UpdateGUI()
|
|
{
|
|
UpdateForeground();
|
|
UpdateText();
|
|
}
|
|
|
|
void UpdateForeground()
|
|
{
|
|
float maxWidth = foregroundArea.rect.width;
|
|
Vector2 offsetMax = foreground.offsetMax;
|
|
offsetMax.x = -(maxWidth - maxWidth*(currentValue / maxValue));
|
|
foreground.offsetMax = offsetMax;
|
|
}
|
|
|
|
void UpdateText()
|
|
{
|
|
if (text != null && text.gameObject.activeSelf != hasText)
|
|
{
|
|
text.gameObject.SetActive(hasText);
|
|
}
|
|
if (hasText && (text != null))
|
|
{
|
|
text.text = Mathf.Floor(currentValue) +"/"+ Mathf.Floor(maxValue);
|
|
}
|
|
}
|
|
|
|
#if UNITY_EDITOR
|
|
protected void OnValidate()
|
|
{
|
|
bDelayedUpdate = true;
|
|
}
|
|
#endif
|
|
}
|
|
}
|
|
|