简单计时器实现

来源:互联网 发布:淘宝打假吃货仅退款 编辑:程序博客网 时间:2024/06/08 09:50
using UnityEngine;using System.Collections;using UnityEngine.UI;using System;public class NewTimer : MonoBehaviour {    public UILabel curLable;    public float m_fEndTime = 0f;    public delegate void FinishMethod();    public FinishMethod m_FinishMethod;    void Awake () {}    //参数lable 传入显示Lable  参数2 传入剩余时间 单位 秒public void SetInfo(UILabel lable,float leftTime, FinishMethod curMethod)    {curLable = lable;        m_fEndTime = Time.time + (float)(leftTime);        m_FinishMethod = curMethod;    }    void Update()    {        if (m_fEndTime > 0.0f)        {            float deltatime = m_fEndTime - Time.time;            if(deltatime >= 0.0f)            {                TimeSpan sp = new TimeSpan(0, 0, (int)deltatime);                if (curLable != null)                {                    curLable.text = Convert.ToDateTime(sp.ToString()).ToString("hh:mm:ss");                }            }            else            {                if (curLable != null)                {                    curLable.text = "";                }                m_fEndTime = 0f;                if (m_FinishMethod != null) {                    m_FinishMethod();                }            }        }        else        {            if (curLable != null)            {                curLable.text = "";            }            m_fEndTime = 0f;            if (m_FinishMethod != null)            {                m_FinishMethod();            }        }    }} 
        使用:NewTimer tf = LeftTime.gameObject.AddComponent<NewTimer>();        if (tf == null)        {            tf = LeftTime.gameObject.AddComponent<NewTimer>();            tf.SetInfo(LeftTime, 60000, delegate { });        }        else        {            tf.SetInfo(LeftTime,60000, delegate { });        }
原创粉丝点击