当前位置: 首页 > news >正文

[Unity]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 returnendself.begin_time = self.begin_time + 1--正计时local positive_time = self.begin_timeif positive_time > self.countdown_time thenself.is_update_local_time = falseself.text_positive_time.text = "01:30:00"elselocal positive_time_table = {}if positive_time < 60 thenpositive_time_table.hour = 0positive_time_table.minute = 0positive_time_table.second = positive_timeelseif positive_time > 60 and positive_time < 3600 thenpositive_time_table.hour = 0positive_time_table.minute = math.modf(positive_time / 60)positive_time_table.second = positive_time - positive_time_table.minute * 60elsepositive_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 * 3600endself.text_positive_time.text = self:local_time_format(positive_time_table)end--倒计时local countdown_time = self.countdown_time - positive_timelocal countdown_time_table = {}if countdown_time > 3600 thencountdown_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 * 3600elseif countdown_time > 60 and countdown_time < 3600 thencountdown_time_table.hour = 0countdown_time_table.minute = math.modf(countdown_time / 60)countdown_time_table.second = countdown_time - countdown_time_table.minute * 60elsecountdown_time_table.hour = 0countdown_time_table.minute = 0countdown_time_table.second = countdown_timeendif countdown_time > 0 thenself.text_countdown_time.text = self:local_time_format(countdown_time_table)elseself.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 thenif time_table.hour >= 0 and time_table.hour < 10 thenstr_time = string.format("0%d" ,time_table.hour)elsestr_time = string.format("%d" ,time_table.hour)endendif time_table.minute thenif time_table.minute >= 0 and time_table.minute < 10 thenstr_time = string.format("%s:0%d" ,str_time ,time_table.minute)elsestr_time = string.format("%s:%d" ,str_time ,time_table.minute)endendif time_table.second thenif time_table.second >= 0 and time_table.second < 10 thenstr_time = string.format("%s:0%d" ,str_time ,time_table.second)elsestr_time = string.format("%s:%d" ,str_time ,time_table.second)endendreturn str_time
end

http://www.lryc.cn/news/134772.html

相关文章:

  • 探究HTTP API接口测试:工具、方法与自动化
  • CSS中如何实现文字溢出省略号(text-overflow: ellipsis)效果?
  • CSDN编程题-每日一练(2023-08-21)
  • 面试题-React(四):React中的事件绑定如何实现?有几种方式?
  • Docker容器:docker镜像的创建及dockerfile案例
  • Java虚拟机(JVM):引用计数算法
  • 【AGC】Publishing api怎么上传绿色认证审核材料
  • 改变住宅区空气质量,你一定要知道!
  • 【SpringCloud】Gateway使用
  • Spring之域对象共享数据
  • Redis巩固加强(帮助迅速梳理知识,同时适用初学者理解)
  • Sui生态项目|集隐私通信、移动钱包、链上朋友圈和红包功能一体的社交应用ComingChat
  • I2S/PCM board-level 约束及同步(latencyskewbitsync)
  • vue 富文本编辑器
  • 为什么说ChatGPT还不是搜索引擎的对手
  • 2308C++协程流程
  • C#实现稳定的ftp下载文件方法
  • 八股文之计算机网络
  • kotlin 比较 let apply
  • springboot跨域踩坑笔记
  • 基于STM32+FreeRTOS的四轴机械臂
  • 【C语言】三子棋游戏——超细教学
  • redux的介绍、安装、三大核心与执行流程
  • Redis 5环境搭建
  • stm32红绿灯源代码示例(附带Proteus电路图)
  • Qt与电脑管家4
  • 使用css美化gradio界面
  • Flink流批一体计算(13):PyFlink Tabel API之SQL DDL
  • java笔试手写算法面试题大全含答案
  • 点云平面拟合和球面拟合