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实现简单的倒计时组件的更多相关文章

  1. Vue2.0进阶组件 短信倒计时组件

    原本我想隔个几天再发文章,刚好今天项目上线,环境有问题,导致只有干等,刚好要为公司打造一套属于公司自己的一系列功能组件,这个使命就交给我了,大家也一直叫我来点干货,说实话我只是一个湿货,肚子里干一点就 ...

  2. Vue3语法快速入门以及写一个倒计时组件

    Vue3写一个倒计时组件 vue3 beta版本发布已有一段时间了,文档也大概看了一下,不过对于学一门技术,最好的方法还是实战,于是找了一个比较简单的组件用vue3来实现,参考的是vant的count ...

  3. JavaScript学习笔记-简单的倒计时跳转页面

    <!DOCTYPE html> <html lang="en" xmlns="http://www.w3.org/1999/xhtml"> ...

  4. setbuffer和freopen做一个简单的日志组件

    目标场景是这样的: 多线程的应用程序要频繁打一些小字节的日志,也不想引用很重的日志库. 设想了一个极其简单的日志组件,main线程中重定向stdout到文件,同时setbuffer设置一个10k的缓冲 ...

  5. [.Net Core] 在 Mvc 中简单使用日志组件

    在 Mvc 中简单使用日志组件 基于 .Net Core 2.0,本文只是蜻蜓点水,并非深入浅出. 目录 使用内置的日志组件 简单过渡到第三方组件 - NLog 使用内置的日志 下面使用控制器 Hom ...

  6. React Native之倒计时组件的实现(ios android)

    React Native之倒计时组件的实现(ios android) 一,需求分析 1,app需实现类似于淘宝的活动倒计时,并在倒计时结束时,活动也结束. 2,实现订单倒计时,并在倒计时结束时,订单关 ...

  7. 微信小程序倒计时组件开发

    今天给大家带来微信小程序倒计时组件具体开发步骤: 先来看下最终效果: git源:http://git.oschina.net/dotton/CountDown 分步骤-性子急的朋友,可以直接看最后那段 ...

  8. JS简单的倒计时(代码优化)

    倒计时网上一大堆,所以也没有什么好说的,支持:1.年,月,日,天,时分秒等倒计时. JS代码如下: /* * js简单的倒计时 * @param {date,obj} 日期 对象格式 */ funct ...

  9. 演练:使用VS2010 C# 创作简单的多线程组件

    BackgroundWorker 组件取代了 System.Threading 命名空间并添加了功能:但是,可以选择保留 System.Threading 命名空间以实现向后兼容并供将来使用.有关更多 ...

随机推荐

  1. discuz财付通也阵亡了

    今日做交易部分,然后焦头烂额. 首先这积分,威望,金钱,什么鬼,乱七八糟的...... 然后这支付宝,啊,,,,,竟然停止个人接口了,不得已要使用财付通. %&……*&……&不 ...

  2. 形象易懂讲解算法I——小波变换

    https://zhuanlan.zhihu.com/p/22450818?refer=dong5 最早发于回答:能不能通俗的讲解下傅立叶分析和小波分析之间的关系? - 咚懂咚懂咚的回答现收入专栏. ...

  3. 结合ajax 的表单验证

    浪费了我两天的时间 我也是醉了 html  结构 <!-- 密码修改 --> <div class="modal fade" id="operatePa ...

  4. Solr安装中文分词器IK

    安装环境 jdk1.7 solr-4.10.3.tgz KAnalyzer2012FF_u1.jar tomcat7 VM虚拟机redhat6.5-x64:192.168.1.201 Xshell4 ...

  5. Python 迭代器切片

    函数itertools.islice() 正好适用于在迭代器和生成器上做切片操作 >>> def count(n): ... while True: ... yield n ... ...

  6. Python: 读文件,写文件

    读写文件是最常见的IO操作.Python内置了读写文件的函数. 读写文件前,我们先了解一下,在磁盘上读写文件的功能都是有操作系统提供的,现代操作系统不允许普通的程序直接操作磁盘,所以,读写文件就是请求 ...

  7. dba和运维专家们说有丰富的大型分布式系统架构设计经验纯属扯淡

    如果,一开始就从事dba和运维的专家们说他们有丰富的大型分布式系统架构设计经验,那纯属扯淡.除非,他们从是从开发专家或者架构师转型而来,那么他们才有资格说自己有丰富的大型分布式系统架构设计经验. 运维 ...

  8. 20165211 2017-2018-2 《Java程序设计》第2周学习总结

    20165211 2017-2018-2 <Java程序设计>第2周学习总结 教材学习内容总结 本周,我学习了书本上二三两章的内容,以下是我整理的主要知识 第二章 基本数据类型与数 标识符 ...

  9. [Java]接受拖拽文件的窗口

    至于这个问题,Java的awt.dnd包下提供了许多完成这一功能的类 例如DropTarget.DropTargetListener等 先来讲一下DropTarget类,这个类完成和拖拽.复制文件等操 ...

  10. bzoj 3343: 教主的魔法

    Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 924  Solved: 402[Submit][Status][Discuss] Descriptio ...