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 命名空间以实现向后兼容并供将来使用.有关更多 ...
随机推荐
- checkBox的使用和事件监听
直接上代码: <!DOCTYPE html> <html> <head> <title></title> </head> < ...
- Mybatis—三剑客之generator使用方法
三剑客之generator主要用于自动生成POJO实体类 准备素材: mybatis-generator-core-1.3.2.jar mysql-connector-java-5.1.2 ...
- unity Texture贴图纹理及相关属性
Texture资源是Unity3d游戏开发中用途最广泛的资源之一,被引用于诸如界面UI. Mesh模型 .粒子效果等.还有一些特殊的Texture资源,如:Movie Texture:视频资源.Ren ...
- CAMediaTiming`协议(9.1 图层时间)
#CAMediaTiming`协议 CAMediaTiming协议定义了在一段动画内用来控制逝去时间的属性的集合,CALayer和CAAnimation都实现了这个协议,所以时间可以被任意基于一个图层 ...
- Ignite缓存大小管理
Ignite使用计算机内存存储缓存数据,达到提升缓存读写性能的.但是计算机内存往往是有限的,我们必须合理管理Ignite对内存的使用. Ignite可以使用JVM堆外内存和堆内内存.使用堆外内存基本上 ...
- centos7源码编译安装Subversion 1.9.5
svn是Subversion的简称,是一个开放源代码的版本控制系统.svn有两种运行方式:1.独立服务器(svn://xxx.xxx/xxx) 2.借助apache(http://svn.xxx.xx ...
- HTML5 -canvas拖拽、移动 绘制图片可操作移动,拖动
关于canvas 的基础知识就不多说了,可以进这个网址学习 http://www.w3school.com.cn/html5/html_5_canvas.asp 对于canvas 和 SVG 其实一开 ...
- JMeter:全面的乱码解决方案
中文乱码一直都是比较让人棘手的问题,我们在使用Jmeter的过程中,也会遇到中文乱码问题 接口:http://127.0.0.1:8090/test 这个接口有一个参数name,返回结果就是你传的na ...
- camera按键采集图像及waitKey的用法
前言 项目需要通过摄像头采集图像并保存,主要是用于后续的摄像头标定.实现过程其实很简单,需要注意一些细节. 系统环境 系统版本:ubuntu16.04:opencv版本:opencv2.4.13:编程 ...
- Linux学习笔记之Linux Centos关闭防火墙
# Centos6.x /etc/init.d/iptables stop chkconfig iptables off sed -i 's/SELINUX=enforcing/SELINUX=dis ...