倒计时更新组件 每秒更新一次时间文字
2016-06-24 14:14
288 查看
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
/// <summary>
/// Time text update. 倒计时更新组件 每秒更新一次时间文字
/// </summary>
public class TimeTextUpdate : MonoBehaviour {
Text text;
long time;
long increment = -1;
/// <summary>
/// Add the specified go, time and s.
/// </summary>
/// <param name="go">Go.</param>
/// <param name="time">Time. 秒</param>
public static void Add(GameObject go,long time,long increment= -1){
//毫秒 <== 秒 * 1000;
TimeTextUpdate timeTextUpdate = go.GetComponent<TimeTextUpdate>() ;
if(timeTextUpdate == null){
timeTextUpdate = go.AddComponent<TimeTextUpdate>() ;
}
timeTextUpdate.time = time;
timeTextUpdate.increment = increment;
timeTextUpdate.StartUpdate();
}
// Use this for initialization
void Start () {
}
void _setInterval(){
text.text = _getTimeStr(time);
time += increment;
}
/// <summary>
/// _gets the time string.
/// </summary>
/// <returns>The time string.</returns>
/// <param name="timeLong">Time long.毫秒</param>
string _getTimeStr(long timeLong){
if(timeLong <= 0){
StopUpdate();
if(text != null){
Hashtable data = new Hashtable();
data.Add("go",text.gameObject);
EventManager.I.send("OnTimeTextUpdateCmp",data);
}
return "";
}
long h = timeLong/3600;
long m = (timeLong%3600)/60;
long s = (timeLong%3600)%60;
string str = (h > 0 ? (h +"小时") :"" ) + (m > 0 ? (m +"分") :"" ) + (s > 0 ? (s +"秒") :"" );
return str;
}
public void StartUpdate () {
StopUpdate();
text = gameObject.GetComponent<Text>();
if(text != null && time > 0)
this.InvokeRepeating("_setInterval", 1.0f, 1.0f);
}
public void StopUpdate () {
this.CancelInvoke();
}
void OnDestroy() {
StopUpdate ();
}
}
using UnityEngine.UI;
using System.Collections;
/// <summary>
/// Time text update. 倒计时更新组件 每秒更新一次时间文字
/// </summary>
public class TimeTextUpdate : MonoBehaviour {
Text text;
long time;
long increment = -1;
/// <summary>
/// Add the specified go, time and s.
/// </summary>
/// <param name="go">Go.</param>
/// <param name="time">Time. 秒</param>
public static void Add(GameObject go,long time,long increment= -1){
//毫秒 <== 秒 * 1000;
TimeTextUpdate timeTextUpdate = go.GetComponent<TimeTextUpdate>() ;
if(timeTextUpdate == null){
timeTextUpdate = go.AddComponent<TimeTextUpdate>() ;
}
timeTextUpdate.time = time;
timeTextUpdate.increment = increment;
timeTextUpdate.StartUpdate();
}
// Use this for initialization
void Start () {
}
void _setInterval(){
text.text = _getTimeStr(time);
time += increment;
}
/// <summary>
/// _gets the time string.
/// </summary>
/// <returns>The time string.</returns>
/// <param name="timeLong">Time long.毫秒</param>
string _getTimeStr(long timeLong){
if(timeLong <= 0){
StopUpdate();
if(text != null){
Hashtable data = new Hashtable();
data.Add("go",text.gameObject);
EventManager.I.send("OnTimeTextUpdateCmp",data);
}
return "";
}
long h = timeLong/3600;
long m = (timeLong%3600)/60;
long s = (timeLong%3600)%60;
string str = (h > 0 ? (h +"小时") :"" ) + (m > 0 ? (m +"分") :"" ) + (s > 0 ? (s +"秒") :"" );
return str;
}
public void StartUpdate () {
StopUpdate();
text = gameObject.GetComponent<Text>();
if(text != null && time > 0)
this.InvokeRepeating("_setInterval", 1.0f, 1.0f);
}
public void StopUpdate () {
this.CancelInvoke();
}
void OnDestroy() {
StopUpdate ();
}
}
相关文章推荐
- Canvas使用渐变之-径向渐变详解
- jQuery简单日历插件版
- 使用SQLyog连接MySQL
- iOS关于数组的简单排序
- Distinct Subsequences
- 使用apache.poi生成Excel
- 穷人看问题,富人看目标!
- 百度定位Sdk 162错误解决方法之Android Studio
- subline text 插件 整合
- 建造者模式
- solution Of 1095. Cars on Campus (30)
- Windows下C++程序移植到Linux上的几个问题及解决方法
- 计算textview 文字宽度
- 钱:越舍得花越有,越不舍得越穷!
- [LeetCode] Longest Consecutive Sequence
- Spring整合Mybatis简要概括
- Spring AspectJ AOP 完整示例
- MFC消息映射
- iOS关于系统原生通知的实现
- js推拉门