UICommonAction = UICommonAction or {} local cc = cc -- local TweenFunc = TweenFunc -- 通用UI进度条动画 -- 参数: img 挂载了image组件的对象,必传 -- round 当进度条有转满表现的时候,这里代表转满了几次 整数 -- end_progress 进度条最终的进度 这里为0~1的值 -- time 动画持续时间,不传代表立即转完 -- duration_callback 跑进度时的回调,参数会传当前的动画进度百分数 -- end_callback 结束回调,可不传 -- tween_func 进度条缓动函数,可不传,默认 TweenFunc.easeOutQuint function ImageProgressAction(img, round, end_progress, time, duration_callback, end_callback, tween_func) assert(img ~= nil, "Image Transform can not be null!") local img_cpn = img:GetComponent(typeof(UnityEngine.UI.Image)) assert(img_cpn ~= nil, "Image Transform doesn't have Image Component!") ClearImageProgressAction(img) -- 设置默认缓动函数 tween_func = tween_func or TweenFunc.easeOutQuint -- 当前进度百分数 local cur_percentage = img_cpn.fillAmount -- 动画中【进度的增量】 local add_percentage = 0 if round <= 0 then -- 从当前进度过渡到最终进度,没有出现转弯一圈的情况,增量就是简单的最终进度百分数减去当前进度百分数 add_percentage = end_progress - cur_percentage else -- 进度转了一圈以上,需要处理增加的进度,由于要从当前进度百分数开始动画,所以要先减去当前进度百分数 add_percentage = round - cur_percentage + end_progress end -- 进度增量不能小于0 -- add_percentage = add_percentage <= 0 and 0 or add_percentage local function action_percentage_func(percent) local real_pec = tween_func(percent) -- 加上cur_percentage的原因是要从当前进度的位置开始做动画表现 local _, final_percentage = math.modf(cur_percentage + add_percentage * real_pec); img_cpn.fillAmount = final_percentage -- 将当前的动画进度百分数传入回调 if duration_callback then duration_callback(real_pec) end end if not time or time <= 0 then action_percentage_func(1) else local action_percentage = cc.CustomUpdate.New(time, action_percentage_func) if end_callback then action_percentage = cc.Sequence.New(action_percentage, cc.CallFunc.New(end_callback)) end cc.ActionManager:getInstance():addAction(action_percentage, img) end end function ClearImageProgressAction(img) assert(img ~= nil, "Image Transform can not be null!") cc.ActionManager:getInstance():removeAllActionsFromTarget(img) end