目前使用NGUI进行界面开发,请在测试前加入NGUI插件。
using UnityEngine; using System.Collections; public class NumberAnimation : MonoBehaviour { public UILabel ValueText; float mNumUpdateTime = 0; float mAnimTotalTime = 1.5f; int mStartValue = 0; int mFinalValue = 0; int _CurrentValue; bool mUpdating = false; void Start() { ValueText = GetComponent<UILabel>(); } //开始播放动画 public void Play(int _StartValue, int _finalValue) { if (_StartValue == _finalValue) return; mStartValue = _StartValue; mFinalValue = _finalValue; mNumUpdateTime = 0; mUpdating = true; } int CurrentValue { get { return _CurrentValue; } set { _CurrentValue = value; ValueText.text = _CurrentValue+""; //string.Format("{0:N0}", value); } } void Update() { if (mUpdating) { mNumUpdateTime += Time.deltaTime; if (mNumUpdateTime >= mAnimTotalTime) { CurrentValue = mFinalValue; mUpdating = false; } else { CurrentValue = t_GetLocomotorValue(mStartValue, mFinalValue, mNumUpdateTime / this.mAnimTotalTime); } } } //根据比例显示当前的数值 private int t_GetLocomotorValue(int p_StartValue, int p_EndValue, float p_Ratio) { int returnValue; if (p_Ratio <= 0) { returnValue = p_StartValue; } else if (p_Ratio >= 1) { returnValue = p_EndValue; } else { returnValue = (int)(p_StartValue + (p_EndValue - p_StartValue) * p_Ratio); } return returnValue; } } //使用方法 //void PlayNumberAnamiaton(int nowAmount, int oldAmount, UILabel target) // { // if (nowAmount - oldAmount > 0) // { // CClientApp.get().getUiMgr().setPropChangeLab(target.gameObject, nowAmount - oldAmount, true); // } // else if (nowAmount - oldAmount < 0) // { // CClientApp.get().getUiMgr().setPropChangeLab(target.gameObject, nowAmount - oldAmount, false); // } // //播放动画 // NumberAnimation numAnim = target.GetComponent<NumberAnimation>(); // if (numAnim == null) // numAnim = target.gameObject.AddComponent<NumberAnimation>(); // numAnim.Play(oldAmount, nowAmount); // oldAmount = nowAmount; // }
时间: 2024-10-14 01:57:09