using System.Collections;
using PlayfulSystems.ProgressBar;
public class ProgressBarPro : MonoBehaviour {
public enum AnimationType { FixedTimeForChange, ChangeSpeed }
[SerializeField] [Range(0f,1f)]
private float m_value = 1f;
private float displayValue = -1f;
[Tooltip("Smoothes out the animation of the bar.")]
[SerializeField] bool animateBar = true;
[SerializeField] AnimationType animationType = AnimationType.FixedTimeForChange;
[SerializeField] float animTime = .25f;
[SerializeField] ProgressBarProView[] views;
private Coroutine sizeAnim;
if (views == null || views.Length == 0)
views = GetComponentsInChildren<ProgressBarProView>();
SetDisplayValue(m_value, true);
public void SetValue(float value, float maxValue) {
SetValue(value / maxValue);
public void SetValue(int value, int maxValue) {
SetValue((float)value / (float)maxValue);
public void SetValue(float percentage, bool forceUpdate = false) {
if (!forceUpdate && Mathf.Approximately(m_value, percentage))
m_value = Mathf.Clamp01(percentage);
for (int i = 0; i < views.Length; i++)
views[i].NewChangeStarted(displayValue, m_value);
if (animateBar && Application.isPlaying && gameObject.activeInHierarchy)
StartSizeAnim(percentage);
SetDisplayValue(percentage);
public bool IsAnimating() {
return !Mathf.Approximately(displayValue, m_value);
public void SetBarColor(Color color) {
for (int i = 0; i < views.Length; i++)
views[i].SetBarColor(color);
void StartSizeAnim(float percentage) {
sizeAnim = StartCoroutine(DoBarSizeAnim());
IEnumerator DoBarSizeAnim() {
float startValue = displayValue;
float change = m_value - displayValue;
float duration = (animationType == AnimationType.FixedTimeForChange ? animTime : Mathf.Abs(change) / animTime);
while (time < duration) {
SetDisplayValue(Utils.EaseSinInOut(time/duration, startValue, change));
SetDisplayValue(m_value, true);
void SetDisplayValue(float value, bool forceUpdate = false) {
if (!forceUpdate && displayValue >= 0f && Mathf.Approximately(displayValue, value))
UpdateBarViews(displayValue, m_value, forceUpdate);
void UpdateBarViews(float currentValue, float targetValue, bool forceUpdate = false) {
for (int i = 0; i < views.Length; i++)
if (forceUpdate || views[i].CanUpdateView(currentValue, targetValue))
views[i].UpdateView(currentValue, targetValue);
void OnDidApplyAnimationProperties() {
private bool m_DelayedUpdateVisuals = false;
private void OnValidate() {
m_value = Mathf.Clamp01(m_value);
m_DelayedUpdateVisuals = true;
if (m_DelayedUpdateVisuals) {
m_DelayedUpdateVisuals = false;
UpdateBarViews(m_value, 0.75f);
UpdateBarViews(m_value, m_value + (1 - m_value) / 2f);
public void AddView(ProgressBarProView view) {
public void DetectViewObjects() {
views = GetComponentsInChildren<ProgressBarProView>(true);