137 lines
3.7 KiB
C#
137 lines
3.7 KiB
C#
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using System;
|
|
using System.Collections;
|
|
using TMPro;
|
|
|
|
namespace Game
|
|
{
|
|
public class LoadingProgressBar : MonoBehaviour
|
|
{
|
|
public Image BarRecTrans;
|
|
public TMP_Text ProgressLabel;
|
|
|
|
public Action OnComplete;
|
|
public Action OnBarFull;
|
|
|
|
public float progress { get; private set; }
|
|
|
|
bool _isInited;
|
|
|
|
void Start()
|
|
{
|
|
_isInited = true;
|
|
BarRecTrans.fillAmount = progress;
|
|
}
|
|
|
|
public void SetProgressValue(float value, float tween_sec)
|
|
{
|
|
value = Mathf.Clamp01(value);
|
|
|
|
if (!Mathf.Approximately(progress, value))
|
|
{
|
|
if (tween_sec > Mathf.Epsilon)
|
|
{
|
|
StopAllCoroutines();
|
|
|
|
if (gameObject.activeInHierarchy)
|
|
{
|
|
StartCoroutine(TweenProgressValue(value, tween_sec));
|
|
}
|
|
}
|
|
else
|
|
{
|
|
StopAllCoroutines();
|
|
SetProgressValue(value);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
//GameDebug.Log($"[SlotLoadingProgressBar]SetProgressValue: Same progress value {progress}, just call it done");
|
|
SetProgressDone();
|
|
}
|
|
}
|
|
|
|
IEnumerator TweenProgressValue(float value, float tween_sec)
|
|
{
|
|
//GameDebug.LogError("Start tween progress: target {0}, sec {1}", value, tween_sec);
|
|
var tween_target = Mathf.Clamp01(value);
|
|
float start_value = progress;
|
|
float start_time = Time.realtimeSinceStartup;
|
|
float end_time = Time.realtimeSinceStartup + tween_sec;
|
|
yield return null;
|
|
|
|
while (Time.realtimeSinceStartup < end_time)
|
|
{
|
|
float t = (Time.realtimeSinceStartup - start_time) / tween_sec;
|
|
float current_value = Mathf.Lerp(start_value, tween_target, t);
|
|
//GameDebug.LogError("-==current_value: {0}", current_value);
|
|
SetProgressValue(current_value);
|
|
yield return new WaitForEndOfFrame();
|
|
}
|
|
|
|
SetProgressValue(tween_target);
|
|
SetProgressDone();
|
|
}
|
|
|
|
public void SetProgressValue(float value)
|
|
{
|
|
value = Mathf.Clamp01(value);
|
|
|
|
if (_isInited)
|
|
{
|
|
BarRecTrans.fillAmount = value;
|
|
}
|
|
|
|
if (ProgressLabel != null)
|
|
{
|
|
var percent_text = (int)(value * 100f);
|
|
ProgressLabel.text = $"{percent_text}%";
|
|
}
|
|
|
|
progress = value;
|
|
//GameDebug.LogError($"-==SetProgressValue: {progress}");
|
|
}
|
|
|
|
public void SetProgressValue(float value, string text)
|
|
{
|
|
value = Mathf.Clamp01(value);
|
|
|
|
if (_isInited)
|
|
{
|
|
BarRecTrans.fillAmount = value;
|
|
}
|
|
|
|
if (ProgressLabel != null)
|
|
{
|
|
var percent_text = (int)(value * 100f);
|
|
ProgressLabel.text = $"[{text}] {percent_text}%";
|
|
}
|
|
|
|
progress = value;
|
|
}
|
|
|
|
void SetProgressDone()
|
|
{
|
|
//GameDebug.Log("[SlotLoadingProgressBar]SetProgressDone");
|
|
|
|
if (OnComplete != null)
|
|
{
|
|
OnComplete.Invoke();
|
|
OnComplete = null;
|
|
}
|
|
|
|
if (Mathf.Approximately(progress, 1))
|
|
{
|
|
if (OnBarFull != null)
|
|
{
|
|
OnBarFull.Invoke();
|
|
OnBarFull = null;
|
|
}
|
|
}
|
|
|
|
StopAllCoroutines();
|
|
}
|
|
}
|
|
}
|