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. VueJS 数据驱动和依赖追踪分析

    之前关于 Vue 数据绑定原理的一点分析,最近需要回顾,就顺便发到随笔上了 在之前实现一个自己的Mvvm中,用 setter 来观测model,将界面上所有的 viewModel 绑定到 model ...

  2. LR和SVM的相同和不同

    之前一篇博客中介绍了Logistics Regression的理论原理:http://www.cnblogs.com/bentuwuying/p/6616680.html. 在大大小小的面试过程中,经 ...

  3. HIT 2051

    这题说的是 一辆汽车 每走一单位的距离就消耗一单位的燃料,然后,他要回城里去,当然他与城镇之间有n个加油站 ,他的油箱可以为 无穷大 ,这样分析后发现进不进汽油站 与 汽油站在哪无关 ,只与加油站的 ...

  4. Git 常用的命令

    基本内容: 工作区:就是你在电脑里能看到的目录. 暂存区:英文叫stage, 或index.一般存放在"git目录"下的index文件(.git/index)中,所以我们把暂存区有 ...

  5. shell 命令总结

    删除0字节文件 find . -type f -size 0 -exec rm -rf {} \; find . type f -size 0 -delete 查看进程,按内存从大到小排列 ps -e ...

  6. python import win32clipboard 报错DLL load failed: %1 不是有效的 Win32 应用程序。

    在python中引入win32clipboard时报错,DLL load failed: %1 不是有效的 Win32 应用程序 >>> import win32clipboardT ...

  7. zookeeper与卡夫卡集群搭建

    首先这片博客没有任何理论性的东西,只是详细说明kafka与zookeeper集群的搭建过程,需要三台linux服务器. java环境变量设置 zookeeper集群搭建 kafka集群搭建 java环 ...

  8. 20145301赵嘉鑫 《网络对抗》Exp9 Web安全基础实践

    20145301赵嘉鑫 <网络对抗>Exp9 Web安全基础实践 实验后回答问题 (1)SQL注入攻击原理,如何防御 SQL注入攻击原理:SQL 是一门 ANSI 的标准计算机语言,用来访 ...

  9. [LeetCode] 701. Insert into a Binary Search Tree

    Given the root node of a binary search tree (BST) and a value to be inserted into the tree, insert t ...

  10. hdu 2222 Keywords Search - Aho-Corasick自动机

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submissio ...