源战役客户端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

59 line
2.7 KiB

  1. UICommonAction = UICommonAction or {}
  2. local cc = cc
  3. -- local TweenFunc = TweenFunc
  4. -- 通用UI进度条动画
  5. -- 参数: img 挂载了image组件的对象,必传
  6. -- round 当进度条有转满表现的时候,这里代表转满了几次 整数
  7. -- end_progress 进度条最终的进度 这里为0~1的值
  8. -- time 动画持续时间,不传代表立即转完
  9. -- duration_callback 跑进度时的回调,参数会传当前的动画进度百分数
  10. -- end_callback 结束回调,可不传
  11. -- tween_func 进度条缓动函数,可不传,默认 TweenFunc.easeOutQuint
  12. function ImageProgressAction(img, round, end_progress, time, duration_callback, end_callback, tween_func)
  13. assert(img ~= nil, "Image Transform can not be null!")
  14. local img_cpn = img:GetComponent(typeof(UnityEngine.UI.Image))
  15. assert(img_cpn ~= nil, "Image Transform doesn't have Image Component!")
  16. ClearImageProgressAction(img)
  17. -- 设置默认缓动函数
  18. tween_func = tween_func or TweenFunc.easeOutQuint
  19. -- 当前进度百分数
  20. local cur_percentage = img_cpn.fillAmount
  21. -- 动画中【进度的增量】
  22. local add_percentage = 0
  23. if round <= 0 then -- 从当前进度过渡到最终进度,没有出现转弯一圈的情况,增量就是简单的最终进度百分数减去当前进度百分数
  24. add_percentage = end_progress - cur_percentage
  25. else -- 进度转了一圈以上,需要处理增加的进度,由于要从当前进度百分数开始动画,所以要先减去当前进度百分数
  26. add_percentage = round - cur_percentage + end_progress
  27. end
  28. -- 进度增量不能小于0
  29. -- add_percentage = add_percentage <= 0 and 0 or add_percentage
  30. local function action_percentage_func(percent)
  31. local real_pec = tween_func(percent)
  32. -- 加上cur_percentage的原因是要从当前进度的位置开始做动画表现
  33. local _, final_percentage = math.modf(cur_percentage + add_percentage * real_pec);
  34. img_cpn.fillAmount = final_percentage
  35. -- 将当前的动画进度百分数传入回调
  36. if duration_callback then
  37. duration_callback(real_pec)
  38. end
  39. end
  40. if not time or time <= 0 then
  41. action_percentage_func(1)
  42. else
  43. local action_percentage = cc.CustomUpdate.New(time, action_percentage_func)
  44. if end_callback then
  45. action_percentage = cc.Sequence.New(action_percentage, cc.CallFunc.New(end_callback))
  46. end
  47. cc.ActionManager:getInstance():addAction(action_percentage, img)
  48. end
  49. end
  50. function ClearImageProgressAction(img)
  51. assert(img ~= nil, "Image Transform can not be null!")
  52. cc.ActionManager:getInstance():removeAllActionsFromTarget(img)
  53. end