当前位置:   article > 正文

Unity实现数字逐渐增加(一个数字动态变化到另一个数字,连续变化)_unity 数字增长动画

unity 数字增长动画

之前在手机上玩游戏,觉得加钱的时候,货币动态上涨的动画挺炫酷的,就手痒痒随手写了一个。

先上图看下效果

动态1

然后我又添加了两种增长方式

1.先快后慢的增长方式

这个增长曲线是正弦函数,所以看起来不是那么明显

2.先慢后快的增长方式

这个增长曲线是二次函数的曲线,所以会明显一点

写完之后发现dotween好像就能实现。。。。

原理什么的就不讲了,代码看一看就都能看懂

下面上代码,为了图方便,我把脚本都写在一个文件里了,按理说分开会好一点儿。

  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using System.Text.RegularExpressions;
  4. using System;
  5. public class CountingNumber : MonoBehaviour
  6. {
  7. public ChangeType changeType = ChangeType.Linear;
  8. Text mText;
  9. int startNum = 100;
  10. int currTime;
  11. int targetNum = 0;
  12. float duration = 0;
  13. bool isStartChange = false;
  14. float time = 0;
  15. ChangeTypeBase changeText;
  16. private void Start()
  17. {
  18. mText = transform.GetComponent<Text>();
  19. if (IsInt(mText.text))
  20. startNum = int.Parse(mText.text);
  21. }
  22. public void ChangeTo(int targetNum, float duration)
  23. {
  24. this.targetNum = targetNum;
  25. currTime = startNum;
  26. this.duration = duration;
  27. time = 0;
  28. isStartChange = true;
  29. switch (changeType)
  30. {
  31. case ChangeType.Linear:
  32. changeText = new LinearType(startNum,targetNum,duration);
  33. break;
  34. case ChangeType.Easein:
  35. changeText = new EaseinType(startNum, targetNum, duration);
  36. break;
  37. case ChangeType.Easeout:
  38. changeText = new EaseoutType(startNum, targetNum, duration);
  39. break;
  40. default:
  41. break;
  42. }
  43. }
  44. private void Update()
  45. {
  46. UpdateText();
  47. UpdateBehaviour();
  48. }
  49. void UpdateBehaviour()
  50. {
  51. if (Input.GetMouseButtonDown(1))
  52. ChangeTo(500, 3);
  53. }
  54. void UpdateText()
  55. {
  56. if (!isStartChange)
  57. return;
  58. time += Time.deltaTime;
  59. if (time>=duration)
  60. {
  61. mText.text = targetNum.ToString();
  62. startNum = targetNum;
  63. changeText = null;
  64. time = 0;
  65. isStartChange = false;
  66. return;
  67. }
  68. int cacheNum = startNum + changeText.ChangeText(time);
  69. if (cacheNum == currTime)
  70. return;
  71. currTime = cacheNum;
  72. mText.text = currTime.ToString();
  73. }
  74. public bool IsInt(string value)
  75. {
  76. return Regex.IsMatch(value, @"^[+-]?\d*$");
  77. }
  78. }
  79. public abstract class ChangeTypeBase
  80. {
  81. protected int startNum;
  82. protected int targetNum;
  83. protected float duration;
  84. public ChangeTypeBase(int startNum, int targetNum, float duration)
  85. {
  86. this.startNum = startNum;
  87. this.targetNum = targetNum;
  88. this.duration = duration;
  89. }
  90. public abstract int ChangeText(float time);
  91. }
  92. public class LinearType : ChangeTypeBase
  93. {
  94. public LinearType(int startNum, int targetNum, float duration) : base(startNum, targetNum, duration)
  95. {
  96. }
  97. public override int ChangeText(float time)
  98. {
  99. return (int)((targetNum -startNum) * (time / duration));
  100. }
  101. }
  102. public class EaseinType : ChangeTypeBase
  103. {
  104. public EaseinType(int startNum, int targetNum, float duration) : base(startNum, targetNum, duration)
  105. {
  106. }
  107. public override int ChangeText(float time)
  108. {
  109. return (int)((targetNum - startNum) * (time / duration)* (time / duration));
  110. }
  111. }
  112. public class EaseoutType : ChangeTypeBase
  113. {
  114. public EaseoutType(int startNum, int targetNum, float duration) : base(startNum, targetNum, duration)
  115. {
  116. }
  117. public override int ChangeText(float time)
  118. {
  119. return (int)((targetNum - startNum) * Math.Sin(Math.PI/2 * (time / duration)));
  120. }
  121. }
  122. public enum ChangeType
  123. {
  124. Linear,
  125. Easein,
  126. Easeout,
  127. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/496913
推荐阅读
相关标签
  

闽ICP备14008679号