using System;
using UnityEngine;
public class UILabelNumberAnima : MonoBehaviour
{
public CommonDelegate.VoidDelegate OnAnimaEnd = null;
UIRichLabel _RichLabel;
UILabel _Label;
bool _BeginAnima = false;
long _CurrentNum = 0;
long _MaxNum = 0;
float _AnimaTime = 1.0f;
float _TotalTime = 0.0f;
long _CachedNum = 0;
string _Format = "{0}";
private void Start()
{
Init();
}
private void OnDisable()
{
_BeginAnima = false;
_TotalTime = 0.0f;
enabled = false;
//Int32 zeroNum = 0;
//if (_RichLabel != null) _RichLabel.text = zeroNum.ToString();
//if (_Label != null) _Label.text = zeroNum.ToString();
}
private void Update()
{
if (!_BeginAnima)
{
return;
}
_TotalTime += Time.unscaledDeltaTime;
_CachedNum = (int)(_CurrentNum + _TotalTime * (_MaxNum - _CurrentNum) / _AnimaTime);
if (_TotalTime >= _AnimaTime)
{
if (_RichLabel != null) _RichLabel.text = string.Format(_Format, _MaxNum);
if (_Label != null) _Label.text = string.Format(_Format, _MaxNum);
if (OnAnimaEnd != null) OnAnimaEnd();
OnDisable();
}
else
{
if (_RichLabel != null) _RichLabel.text = string.Format(_Format, _CachedNum);
if (_Label != null) _Label.text = string.Format(_Format, _CachedNum);
}
}
public void Init()
{
if (_RichLabel == null) _RichLabel = gameObject.GetComponent<UIRichLabel>();
if (_Label == null) _Label = gameObject.GetComponent<UILabel>();
}
public void SetAnima(long currentNum, long maxNum, float animaTime = 1f)
{
_CurrentNum = currentNum;
_MaxNum = maxNum;
_AnimaTime = animaTime;
enabled = true;
}
public void SetAnimaStr(long currentNum, long maxNum, string format, float animaTime = 1f)
{
_CurrentNum = currentNum;
_MaxNum = maxNum;
_AnimaTime = animaTime;
_Format = format;
enabled = true;
}
public void BeginAnima()
{
_BeginAnima = true;
_TotalTime = 0.0f;
if (_AnimaTime <= 0.0f)
{
_AnimaTime = 1.0f;
}
}
}
使用方法
_BattleScoreLabel.SetAnima(lastBattleScore, curBattleScore, 0.5f);
_BattleScoreLabel.BeginAnima();