之前在手机上玩游戏,觉得加钱的时候,货币动态上涨的动画挺炫酷的,就手痒痒随手写了一个。
先上图看下效果
然后我又添加了两种增长方式
1.先快后慢的增长方式
这个增长曲线是正弦函数,所以看起来不是那么明显
2.先慢后快的增长方式
这个增长曲线是二次函数的曲线,所以会明显一点
写完之后发现dotween好像就能实现。。。。
原理什么的就不讲了,代码看一看就都能看懂
下面上代码,为了图方便,我把脚本都写在一个文件里了,按理说分开会好一点儿。
using UnityEngine; using UnityEngine.UI; using System.Text.RegularExpressions; using System; public class CountingNumber : MonoBehaviour { public ChangeType changeType = ChangeType.Linear; Text mText; int startNum = 100; int currTime; int targetNum = 0; float duration = 0; bool isStartChange = false; float time = 0; ChangeTypeBase changeText; private void Start() { mText = transform.GetComponent<Text>(); if (IsInt(mText.text)) startNum = int.Parse(mText.text); } public void ChangeTo(int targetNum, float duration) { this.targetNum = targetNum; currTime = startNum; this.duration = duration; time = 0; isStartChange = true; switch (changeType) { case ChangeType.Linear: changeText = new LinearType(startNum,targetNum,duration); break; case ChangeType.Easein: changeText = new EaseinType(startNum, targetNum, duration); break; case ChangeType.Easeout: changeText = new EaseoutType(startNum, targetNum, duration); break; default: break; } } private void Update() { UpdateText(); UpdateBehaviour(); } void UpdateBehaviour() { if (Input.GetMouseButtonDown(1)) ChangeTo(500, 3); } void UpdateText() { if (!isStartChange) return; time += Time.deltaTime; if (time>=duration) { mText.text = targetNum.ToString(); startNum = targetNum; changeText = null; time = 0; isStartChange = false; return; } int cacheNum = startNum + changeText.ChangeText(time); if (cacheNum == currTime) return; currTime = cacheNum; mText.text = currTime.ToString(); } public bool IsInt(string value) { return Regex.IsMatch(value, @"^[+-]?\d*$"); } } public abstract class ChangeTypeBase { protected int startNum; protected int targetNum; protected float duration; public ChangeTypeBase(int startNum, int targetNum, float duration) { this.startNum = startNum; this.targetNum = targetNum; this.duration = duration; } public abstract int ChangeText(float time); } public class LinearType : ChangeTypeBase { public LinearType(int startNum, int targetNum, float duration) : base(startNum, targetNum, duration) { } public override int ChangeText(float time) { return (int)((targetNum -startNum) * (time / duration)); } } public class EaseinType : ChangeTypeBase { public EaseinType(int startNum, int targetNum, float duration) : base(startNum, targetNum, duration) { } public override int ChangeText(float time) { return (int)((targetNum - startNum) * (time / duration)* (time / duration)); } } public class EaseoutType : ChangeTypeBase { public EaseoutType(int startNum, int targetNum, float duration) : base(startNum, targetNum, duration) { } public override int ChangeText(float time) { return (int)((targetNum - startNum) * Math.Sin(Math.PI/2 * (time / duration))); } } public enum ChangeType { Linear, Easein, Easeout, }