当前位置:   article > 正文

让数字变化炫酷起来,数字滚动Text组件[Unity](lua+NGUI)_unity 数字滚动

unity 数字滚动

让数字滚动起来

上周我的策划又提了样需求,当玩家评分发生变动时,屏幕出现人物评分浮层UI,播放评分数字滚动动画。这类数字滚动需求非常常见,我就按一般思路,将startvalue与endvalue每隔一点时间做插值变化并显示,从而实现数字滚动的效果,这也是大部分app及游戏采取的实现,效果如下:

几行代码写完给策划看效果,策划说不是这样的效果,跟XX游戏做得不一样,得像lao虎机数字一样,有真实的数字滚动效果,好吧,此滚动非彼滚动,期望效果应该是下面这样,看起来更确实炫酷:

Unity中的样式:

代码实现

  1. Module_Tql_Award_Reels = LuaBaseClass("Module_Tql_Award_Reels")
  2. local Tweening = DG.Tweening
  3. function Module_Tql_Award_Reels:ctor(gameObject,numberSize,numLength,aniTime,numSpName, grid, symbolLenth)
  4. self.gameObject = gameObject;
  5. self.transform = gameObject.transform;
  6. self.grid = grid;
  7. self.numberSize = numberSize;
  8. self.aniTime = aniTime;
  9. self.numLength = numLength;
  10. self.symbolLenth = symbolLenth;
  11. self.activeInHierarchyLenth = numLength;
  12. self.rollerTable = {};
  13. self.symbolTable = {};
  14. for i = 1, self.numLength do
  15. self.rollerTable[i] = ModuleTqlAwardRoller.new(self.transform:FindChild("numbers/rollerNum" .. i).gameObject,numberSize,aniTime,numSpName);
  16. end
  17. for i = 1, self.symbolLenth do
  18. self.symbolTable[i] = self.grid.transform:FindChild("rollerSymbol" .. i).gameObject;
  19. end
  20. end
  21. -----------------------------
  22. function Module_Tql_Award_Reels:split(str)
  23. local list = {};
  24. local len = string.len(str);
  25. str = string.reverse(str);
  26. for i=1,len do
  27. list[i]=string.sub(str,i,i);
  28. end
  29. return list;
  30. end
  31. function Module_Tql_Award_Reels:change(to,isTween)
  32. if to == nil or to == "Null" then
  33. for i = #(self.rollerTable), 1, -1 do
  34. self.rollerTable[i]:StopTween();
  35. end
  36. return;
  37. end
  38. local splitstr = Module_Tql_Award_Reels:split(tostring(to));
  39. local intsplitstr = 1;
  40. local lensplitstr = #splitstr;
  41. for i = #(self.rollerTable), 1, -1 do
  42. if intsplitstr <= lensplitstr then
  43. self.rollerTable[i].gameObject:SetActive(true);
  44. self.rollerTable[i]:setNumber(splitstr[intsplitstr], isTween);
  45. else
  46. self.rollerTable[i]:setNumber("0", isTween);
  47. self.rollerTable[i].gameObject:SetActive(false);
  48. end
  49. intsplitstr = intsplitstr+1;
  50. end
  51. if self:activeInHierarchy() ~= self.activeInHierarchyLenth then
  52. self.activeInHierarchyLenth = self:activeInHierarchy();
  53. self:number_format(to)
  54. self.grid:Reposition();
  55. end
  56. end
  57. function Module_Tql_Award_Reels:activeInHierarchy()
  58. local num = 0;
  59. for i = 1, #(self.rollerTable) do
  60. if self.rollerTable[i].gameObject.activeInHierarchy then
  61. num = num + 1;
  62. end
  63. end
  64. return num;
  65. end
  66. function Module_Tql_Award_Reels:number_format(num)
  67. local str1 = "";
  68. local str = tostring(math.floor(num));
  69. local strLen = string.len(str);
  70. for i = 1, #(self.symbolTable) do
  71. self.symbolTable[i].gameObject:SetActive(false);
  72. end
  73. local index = #(self.symbolTable);
  74. for i=1,strLen do
  75. str1 = string.char(string.byte(str,strLen+1 - i)) .. str1
  76. if math.mod(i,3) == 0 then
  77. --下一个数 还有
  78. if strLen - i ~= 0 then
  79. self.symbolTable[index].gameObject:SetActive(true);
  80. index = index - 1;
  81. end
  82. end
  83. end
  84. return str1;
  85. end
  86. --单一位动画
  87. ModuleTqlAwardRoller = LuaBaseClass("ModuleTqlAwardRoller")
  88. function ModuleTqlAwardRoller:ctor(gameObject,numberSize,aniTime,numSpName)
  89. self.gameObject = gameObject;
  90. self.transform = gameObject.transform;
  91. self.numTable = {};
  92. self.numSpName=numSpName;
  93. self.rollingDuration =aniTime; --数字每次滚动时长
  94. self.currentrollingTime = aniTime;
  95. self.numberSize =numberSize; --图片高
  96. self.tweener = {}; --各位数字的缓动实例
  97. self.isTweening = false;
  98. self.curNumber = nil; --当前数字
  99. self.toNumber = 0; --最终数字;
  100. for i = 1, 3 do
  101. self.numTable[i] = self.transform:FindChild("Symbol" .. i):GetComponent("UISprite");
  102. end
  103. self.currentnumTable = 2;
  104. end
  105. function ModuleTqlAwardRoller:setNumber(value, isTween)
  106. self.toNumber = tonumber(value);
  107. if self.curNumber == nil then
  108. self.curNumber = self.toNumber;
  109. isTween = false;
  110. end
  111. local numberLocal = self.toNumber-self.curNumber;
  112. if self.toNumber < self.curNumber then
  113. numberLocal = 10-self.curNumber+self.toNumber;
  114. end
  115. if numberLocal <= 0 then
  116. numberLocal = 1;
  117. end
  118. self.currentrollingTime = self.rollingDuration/numberLocal
  119. if isTween then
  120. if not self.isTweening then
  121. if self.toNumber ~= self.curNumber then
  122. self:StartTween();
  123. end
  124. end
  125. else
  126. self:StopTween();
  127. self.toNumber = tonumber(value);
  128. self.curNumber = self.toNumber;
  129. local abovenum = self:currentnumTableAbove();
  130. local belownum = self:currentnumTableBelow();
  131. self.numTable[abovenum].spriteName = self.numSpName..self:curNumberAdd();
  132. self.numTable[abovenum].transform.localPosition = Vector3.New(0,self.numberSize,0);
  133. self.numTable[self.currentnumTable].spriteName = self.numSpName .. self.curNumber;
  134. self.numTable[self.currentnumTable].transform.localPosition = Vector3.New(0,0,0);
  135. self.numTable[belownum].spriteName = self.numSpName..self:curNumberReduction();
  136. self.numTable[belownum].transform.localPosition = Vector3.New(0,-self.numberSize,0);
  137. end
  138. end
  139. function ModuleTqlAwardRoller:TweenOnComplete()
  140. if self.toNumber ~= self.curNumber then
  141. self:StartTween();
  142. else
  143. self.isTweening = false;
  144. for i = 1, 3 do
  145. self.tweener[i] = nil
  146. end
  147. end
  148. end
  149. function ModuleTqlAwardRoller:StartTween()
  150. self.isTweening = true;
  151. local abovenum = self:currentnumTableAbove();
  152. local belownum = self:currentnumTableBelow();
  153. self.numTable[belownum].transform.localPosition = Vector3.New(0,self.numberSize*2,0);
  154. self:doTween(belownum,Vector3.New(0,self.numberSize,0))
  155. self:doTween(self.currentnumTable,Vector3.New(0,-self.numberSize,0))
  156. self:doTween(abovenum,Vector3.New(0,0,0))
  157. self.tweener[abovenum]:OnStepComplete(Utils.ActionToTween(self.TweenOnComplete,self))
  158. self.currentnumTable = abovenum
  159. self.curNumber = self:curNumberAdd()
  160. self.numTable[belownum].spriteName = self.numSpName..self:curNumberAdd();
  161. end
  162. function ModuleTqlAwardRoller:StopTween()
  163. self.curNumber = nil
  164. self.toNumber = nil
  165. for i = 1, 3 do
  166. self.numTable[i].spriteName = "";
  167. if self.tweener[i] then
  168. self.tweener[i]:Complete();
  169. self.tweener[i] = nil
  170. end
  171. end
  172. self.isTweening = false;
  173. end
  174. function ModuleTqlAwardRoller:doTween(tablenum,localPosition)
  175. if self.tweener[tablenum] then
  176. self.tweener[tablenum]:Complete();
  177. self.tweener[tablenum] = nil
  178. end
  179. tween = self.numTable[tablenum].transform:DOLocalMove(localPosition, self.currentrollingTime, true);
  180. tween:SetEase(DG.Tweening.Ease.Linear)
  181. self.tweener[tablenum] = tween;
  182. end
  183. function ModuleTqlAwardRoller:curNumberAdd()
  184. local temopnum = self.curNumber+1
  185. if temopnum > 9 then
  186. temopnum = 0;
  187. end
  188. return temopnum;
  189. end
  190. function ModuleTqlAwardRoller:curNumberReduction()
  191. local temopnum = self.curNumber-1
  192. if temopnum < 0 then
  193. temopnum = 9;
  194. end
  195. return temopnum;
  196. end
  197. function ModuleTqlAwardRoller:currentnumTableAbove()
  198. local temopnumTable = self.currentnumTable-1
  199. if temopnumTable < 1 then
  200. temopnumTable = 3;
  201. end
  202. return temopnumTable;
  203. end
  204. function ModuleTqlAwardRoller:currentnumTableBelow()
  205. local temopnumTable = self.currentnumTable+1
  206. if temopnumTable > 3 then
  207. temopnumTable = 1;
  208. end
  209. return temopnumTable;
  210. end

使用:

  1. this.people_award_reels = this:InitModuleAwardReels(this.room_4.transform:FindChild("type2/room4_TQL/PeopleReelPanel").gameObject, 12, "room4_tql_add_", 3);
  2. function this:InitModuleAwardReels(awardtrans, numLength, numSpName, symbolLenth)
  3. local grid = awardtrans.transform:FindChild("numbers"):GetComponent("UIGrid");
  4. local awardreels = Module_Tql_Award_Reels.new(awardtrans, 30, numLength, 0.7, numSpName, grid, symbolLenth);
  5. return awardreels;
  6. end
this.people_award_reels:change(100, true); --true是需要播放动画,false为不播放动画,直接到这个数据

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

闽ICP备14008679号