您的位置:首页 > 其它

倒计时更新组件 每秒更新一次时间文字

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 ();

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: