[Unity]Lua本地时间、倒计时和正计时。

惯例,直接上代码:

Lua 复制代码
--正计时开始时的时间戳
self.begin_time = os.time()
--倒计时时长,01:30:00
self.countdown_time = 5400
--是否开始计时
self.is_update_local_time = true

--Unity Update
function time_transition:update_local_timer()
    if not self.is_update_local_time then 
        return
    end
    self.begin_time = self.begin_time + 1
    --正计时
    local positive_time = self.begin_time
    if positive_time > self.countdown_time then
        self.is_update_local_time = false
        self.text_positive_time.text = "01:30:00"
    else
        local positive_time_table = {}
        if positive_time < 60 then
            positive_time_table.hour = 0
            positive_time_table.minute = 0
            positive_time_table.second = positive_time
        elseif positive_time > 60 and positive_time < 3600 then
            positive_time_table.hour = 0
            positive_time_table.minute = math.modf(positive_time / 60)
            positive_time_table.second = positive_time - positive_time_table.minute * 60
        else
            positive_time_table.hour = math.modf(positive_time / 3600)
            positive_time_table.minute = math.modf((positive_time - positive_time_table.hour * 3600) / 60)
            positive_time_table.second = positive_time - positive_time_table.minute * 60 - positive_time_table.hour * 3600
        end
        self.text_positive_time.text = self:local_time_format(positive_time_table)
    end
    --倒计时
    local countdown_time = self.countdown_time - positive_time
    local countdown_time_table = {}
    if countdown_time > 3600 then
        countdown_time_table.hour = math.modf(countdown_time / 3600)
        countdown_time_table.minute = math.modf((countdown_time - countdown_time_table.hour * 3600) / 60)
        countdown_time_table.second = countdown_time - countdown_time_table.minute * 60 - countdown_time_table.hour * 3600
    elseif countdown_time > 60 and countdown_time < 3600 then
        countdown_time_table.hour = 0
        countdown_time_table.minute = math.modf(countdown_time / 60)
        countdown_time_table.second = countdown_time - countdown_time_table.minute * 60
    else
        countdown_time_table.hour = 0
        countdown_time_table.minute = 0
        countdown_time_table.second = countdown_time
    end
    if countdown_time > 0 then
        self.text_countdown_time.text = self:local_time_format(countdown_time_table)
    else
        self.text_countdown_time.text = "00:00:00"
    end
end

--格式化时间:00:00:00
function time_transition:local_time_format(time_table)
	local str_time = ""
	if time_table.hour then
		if time_table.hour >= 0 and time_table.hour < 10 then
			str_time = string.format("0%d" ,time_table.hour)
		else
			str_time = string.format("%d" ,time_table.hour)
		end
	end
	if time_table.minute then
		if time_table.minute >= 0 and time_table.minute < 10 then
			str_time = string.format("%s:0%d" ,str_time ,time_table.minute)
		else
			str_time = string.format("%s:%d" ,str_time ,time_table.minute)
		end
	end
	if time_table.second then
		if time_table.second >= 0 and time_table.second < 10 then
			str_time = string.format("%s:0%d" ,str_time ,time_table.second)
		else
			str_time = string.format("%s:%d" ,str_time ,time_table.second)
		end
	end
	return str_time
end
相关推荐
软件黑马王子4 小时前
Unity游戏制作中的C#基础(5)条件语句和循环语句知识点全解析
游戏·unity·c#
龚子亦9 小时前
Unity结合Vuforia虚拟按键实现AR机械仿真动画效果
unity·游戏引擎·ar·数字孪生·虚拟仿真
煤炭里de黑猫10 小时前
Lua C API :使用 lua_tonumber 函数从 Lua 栈中提取数值
开发语言·lua
煤炭里de黑猫12 小时前
Lua C API:深入理解 lua_pushnumber 函数 — 将数字压入 Lua 栈中
开发语言·lua
煤炭里de黑猫18 小时前
Lua C API :lua_insert 函数详解
开发语言·lua
程序猿多布18 小时前
Unity Excel导表工具转Lua文件
unity·excel
mikey棒棒棒1 天前
Redis——优惠券秒杀问题(分布式id、一人多单超卖、乐悲锁、CAS、分布式锁、Redisson)
数据库·redis·lua·redisson·watchdog·cas·并发锁
avi91111 天前
[AI相关]Unity的C#代码如何简写
unity·c#·语法糖
心疼你的一切2 天前
C# 中关于补位的写法 PadLeft,PadRight 函数
开发语言·unity·c#·游戏引擎·csdn·心疼你的一切
沐沐森的故事2 天前
Unity for Python —— 强大的 Python 脚本支持提升 Unity 编辑器效率
python·unity·编辑器·pythonrunner·pythonengine