NGUI实现简单的倒计时组件
using System;
using UnityEngine; public enum ETimerType
{
CommonFormat, // 78 77 76 75 ...
TimeFormat, //以时间格式显示倒计时
} //自定义时间
public class CustomTime
{
public int Days, Hours, Minutes, Seconds; public CustomTime(int seconds)
{
Days = seconds / ; seconds = seconds - Days * ;
Hours = seconds / ; seconds = seconds - Hours * ;
Minutes = seconds / ; Seconds = seconds % ;
} public string GetTimeFormat()
{
string formatTime = "";
if (Days > )
{
formatTime = string.Format("{0}天{1}:{2}:{3}", Days.ToString(), Hours.ToString("D2"), Minutes.ToString("D2"), Seconds.ToString("D2"));
}
else if (Hours > )
{
formatTime = string.Format("{0}:{1}:{2}", Hours.ToString("D2"), Minutes.ToString("D2"), Seconds.ToString("D2"));
}
else if (Minutes > )
{
formatTime = string.Format("{0}:{1}", Minutes.ToString("D2"), Seconds.ToString("D2"));
}
else if (Seconds > )
{
formatTime = string.Format("{0}", Seconds.ToString("D2"));
}
return formatTime;
}
} [RequireComponent(typeof(UILabel))]
public class LabelTimer : MonoBehaviour
{
public delegate void OnFinish();
public event OnFinish EvFinish; public int totalTime = ;
public float interval = ; ETimerType timerType = ETimerType.CommonFormat;
UILabel mLabel = null; void Start()
{
mLabel = GetComponent<UILabel>();
} void Update()
{
interval += Time.deltaTime;
if (interval >= )
{
--totalTime;
ShowTime();
interval = ;
} if (totalTime <= )
{
enabled = false;
gameObject.SetActive(false);
if (null != EvFinish)
EvFinish();
}
} void ShowTime()
{
if (timerType == ETimerType.CommonFormat)
{
mLabel.text = totalTime.ToString();
}
else if (timerType == ETimerType.TimeFormat)
{
CustomTime ct = new CustomTime(totalTime);
mLabel.text = ct.GetTimeFormat();
}
} public void Stop()
{
gameObject.SetActive(false);
totalTime = ;
EvFinish = null;
} public bool TimeKeeping
{
get
{
return totalTime > ;
}
} public void StartCountDown(int totalTime, ETimerType timerType)
{
this.timerType = timerType;
if (null == mLabel)
mLabel = GetComponent<UILabel>();
this.totalTime = totalTime;
interval = ;
enabled = true;
ShowTime();
if (!gameObject.activeSelf)
gameObject.SetActive(true);
} public static LabelTimer Begin(GameObject go, int totalTime, ETimerType timerType = ETimerType.CommonFormat)
{
LabelTimer lt = go.GetComponent<LabelTimer>();
if (null == lt) lt = go.AddComponent<LabelTimer>();
lt.StartCountDown(totalTime, timerType);
return lt;
}
}
NGUI实现简单的倒计时组件的更多相关文章
- Vue2.0进阶组件 短信倒计时组件
原本我想隔个几天再发文章,刚好今天项目上线,环境有问题,导致只有干等,刚好要为公司打造一套属于公司自己的一系列功能组件,这个使命就交给我了,大家也一直叫我来点干货,说实话我只是一个湿货,肚子里干一点就 ...
- Vue3语法快速入门以及写一个倒计时组件
Vue3写一个倒计时组件 vue3 beta版本发布已有一段时间了,文档也大概看了一下,不过对于学一门技术,最好的方法还是实战,于是找了一个比较简单的组件用vue3来实现,参考的是vant的count ...
- JavaScript学习笔记-简单的倒计时跳转页面
<!DOCTYPE html> <html lang="en" xmlns="http://www.w3.org/1999/xhtml"> ...
- setbuffer和freopen做一个简单的日志组件
目标场景是这样的: 多线程的应用程序要频繁打一些小字节的日志,也不想引用很重的日志库. 设想了一个极其简单的日志组件,main线程中重定向stdout到文件,同时setbuffer设置一个10k的缓冲 ...
- [.Net Core] 在 Mvc 中简单使用日志组件
在 Mvc 中简单使用日志组件 基于 .Net Core 2.0,本文只是蜻蜓点水,并非深入浅出. 目录 使用内置的日志组件 简单过渡到第三方组件 - NLog 使用内置的日志 下面使用控制器 Hom ...
- React Native之倒计时组件的实现(ios android)
React Native之倒计时组件的实现(ios android) 一,需求分析 1,app需实现类似于淘宝的活动倒计时,并在倒计时结束时,活动也结束. 2,实现订单倒计时,并在倒计时结束时,订单关 ...
- 微信小程序倒计时组件开发
今天给大家带来微信小程序倒计时组件具体开发步骤: 先来看下最终效果: git源:http://git.oschina.net/dotton/CountDown 分步骤-性子急的朋友,可以直接看最后那段 ...
- JS简单的倒计时(代码优化)
倒计时网上一大堆,所以也没有什么好说的,支持:1.年,月,日,天,时分秒等倒计时. JS代码如下: /* * js简单的倒计时 * @param {date,obj} 日期 对象格式 */ funct ...
- 演练:使用VS2010 C# 创作简单的多线程组件
BackgroundWorker 组件取代了 System.Threading 命名空间并添加了功能:但是,可以选择保留 System.Threading 命名空间以实现向后兼容并供将来使用.有关更多 ...
随机推荐
- Scrapy是什么
1.Scrapy是蜘蛛爬虫框架,我们用蜘蛛来获取互联网上的各种信息,然后再对这些信息进行数据分析处理. 2.Scrapy的组成 引擎:处理整个系统的数据流处理,出发事务 调度器: 接受引擎发过来的请求 ...
- Python: 列表推导式--轻量级循环
定义: 列表推导式(list comprehension)是利用其他列表创建新列表的一种方法,其工作方式类似于for循环,对列表进行过滤变种操作 eg1: >>> [x*x for ...
- linux服务器---配置bind
配置bind 1.确定已经安装bind软件,需要安装3 个bind.bind-chroot.bind-util [root@localhost wj]# yum install –y bind bin ...
- 用户可在Messenger内读书并与角色互动
畅销小说作家 James Patterson 与 Facebook 合作,为新书<The Chef>在 Messenger 推出互动体验版. 10 月 30 日下午消息,据<香港经济 ...
- web前端----响应式布局
响应式开发 为什么要进行响应式开发? 随着移动设备的流行,网页设计必须要考虑到移动端的设计.同一个网站为了兼容PC端和移动端显示,就需要进行响应式开发. 什么是响应式? 利用媒体查询,让同一个网站兼容 ...
- mysql8.0无法给用户授权或提示You are not allowed to create a user with GRANT的问题
提示意思是不能用grant创建用户,mysql8.0以前的版本可以使用grant在授权的时候隐式的创建用户,8.0以后已经不支持,所以必须先创建用户,然后再授权,命令如下: mysql> CRE ...
- P3810 【模板】三维偏序(陌上花开)
P3810 [模板]三维偏序(陌上花开) cdq分治+树状数组 三维偏序模板题 前两维用cdq分治,第三维用树状数组进行维护 就像用树状数组搞逆序对那样做--->存权值的出现次数 attenti ...
- 根据wsdl文件,Web工程自动生成webservice客户端调用
根据wsdl文件,Web工程自动生成webservice客户端调用 1,工具:带有webservice插件的eclips 2,步骤: (1),新建一个Web工程:WSDLTest (2),浏览器访问W ...
- 20145335郝昊《网络攻防》Exp 4 利用nmap扫描
20145335郝昊<网络攻防>Exp 4 利用nmap扫描 实验原理 使用msf辅助模块,nmap来扫描发现局域网中的主机ip 实验步骤 首先使用命令创建一个msf所需的数据库 serv ...
- Matchvs 使用记录
Matchvs Matchvs视频教程. https://doc.matchvs.com/VideoTutorials/videogs matchvs下载资源. http://www.matchvs. ...