109 lines
2.0 KiB
C#
109 lines
2.0 KiB
C#
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using OHM.UnityToolkit.Localization;
|
|
|
|
namespace OHM.UnityToolkit.UI
|
|
{
|
|
public class UIIncrementableCounter : MonoBehaviour
|
|
{
|
|
[SerializeField]
|
|
public float delay;
|
|
|
|
[SerializeField]
|
|
public float incDuration = 0.1f;
|
|
[SerializeField]
|
|
public Animator animator;
|
|
|
|
[SerializeField]
|
|
public TextWrapper text;
|
|
|
|
[SerializeField]
|
|
public bool localized;
|
|
|
|
[SerializeField]
|
|
[TextArea]
|
|
public string strFormat;
|
|
|
|
private long targetValue;
|
|
|
|
private float _lastTargetChangeTime;
|
|
|
|
private long _lastTargetChangeValue;
|
|
|
|
public long currentValue { get; set; }
|
|
|
|
public void Init(long value)
|
|
{
|
|
targetValue = value;
|
|
SetCurrentValue(value);
|
|
}
|
|
|
|
public void SetTargetValue(long value)
|
|
{
|
|
if (targetValue == value)
|
|
{
|
|
return;
|
|
}
|
|
if (animator != null && animator.gameObject.activeSelf)
|
|
{
|
|
if (value > targetValue)
|
|
{
|
|
animator.ResetTrigger("Decrement");
|
|
animator.SetTrigger("Increment");
|
|
}
|
|
else
|
|
{
|
|
animator.ResetTrigger("Increment");
|
|
animator.SetTrigger("Decrement");
|
|
}
|
|
}
|
|
_lastTargetChangeTime = Time.unscaledTime;
|
|
_lastTargetChangeValue = currentValue;
|
|
targetValue = value;
|
|
}
|
|
|
|
private void Awake()
|
|
{
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (currentValue == targetValue)
|
|
{
|
|
return;
|
|
}
|
|
float t = (Time.unscaledTime - _lastTargetChangeTime - delay) / incDuration;
|
|
if (t < 0f)
|
|
{
|
|
return;
|
|
}
|
|
if (t >= 1f)
|
|
{
|
|
SetCurrentValue(targetValue);
|
|
return;
|
|
}
|
|
SetCurrentValue((long)Mathf.Lerp(_lastTargetChangeValue, targetValue, t));
|
|
}
|
|
|
|
private void SetCurrentValue(long value)
|
|
{
|
|
currentValue = value;
|
|
string display;
|
|
if (string.IsNullOrEmpty(strFormat))
|
|
{
|
|
display = value.ToString();
|
|
}
|
|
else
|
|
{
|
|
string format = (localized ? LocalizationUtils.Localize(LocalizationManager.Instance.CurrentLocale, strFormat) : strFormat);
|
|
display = string.Format(format, value);
|
|
}
|
|
if (text != null)
|
|
{
|
|
text.text = display;
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|