• NGUI实现简单的倒计时组件


      1 using System;
      2 using UnityEngine;
      3 
      4 public enum ETimerType
      5 {
      6     CommonFormat,     // 78 77 76 75 ...
      7     TimeFormat,       //以时间格式显示倒计时
      8 }
      9 
     10 //自定义时间
     11 public class CustomTime
     12 {
     13     public int Days, Hours, Minutes, Seconds;
     14 
     15     public CustomTime(int seconds)
     16     {
     17         Days = seconds / 86400;
     18 
     19         seconds = seconds - Days * 86400;
     20         Hours = seconds / 3600;
     21 
     22         seconds = seconds - Hours * 3600;
     23         Minutes = seconds / 60;
     24 
     25         Seconds = seconds % 60;
     26     }
     27 
     28     public string GetTimeFormat()
     29     {
     30         string formatTime = "";
     31         if (Days > 0)
     32         {
     33             formatTime = string.Format("{0}天{1}:{2}:{3}", Days.ToString(), Hours.ToString("D2"), Minutes.ToString("D2"), Seconds.ToString("D2"));
     34         }
     35         else if (Hours > 0)
     36         {
     37             formatTime = string.Format("{0}:{1}:{2}", Hours.ToString("D2"), Minutes.ToString("D2"), Seconds.ToString("D2"));
     38         }
     39         else if (Minutes > 0)
     40         {
     41             formatTime = string.Format("{0}:{1}", Minutes.ToString("D2"), Seconds.ToString("D2"));
     42         }
     43         else if (Seconds > 0)
     44         {
     45             formatTime = string.Format("{0}", Seconds.ToString("D2"));
     46         }
     47         return formatTime;
     48     }
     49 }
     50 
     51 [RequireComponent(typeof(UILabel))]
     52 public class LabelTimer : MonoBehaviour
     53 {
     54     public delegate void OnFinish();
     55     public event OnFinish EvFinish;
     56 
     57     public int totalTime = 0;
     58     public float interval = 0;
     59 
     60     ETimerType timerType = ETimerType.CommonFormat;
     61     UILabel mLabel = null;
     62 
     63     void Start()
     64     {
     65         mLabel = GetComponent<UILabel>();
     66     }
     67 
     68     void Update()
     69     {
     70         interval += Time.deltaTime;
     71         if (interval >= 1)
     72         {
     73             --totalTime;
     74             ShowTime();
     75             interval = 0;
     76         }
     77 
     78         if (totalTime <= 0)
     79         {
     80             enabled = false;
     81             gameObject.SetActive(false);
     82             if (null != EvFinish)
     83                 EvFinish();
     84         }
     85     }
     86 
     87     void ShowTime()
     88     {
     89         if (timerType == ETimerType.CommonFormat)
     90         {
     91             mLabel.text = totalTime.ToString();
     92         }
     93         else if (timerType == ETimerType.TimeFormat)
     94         {
     95             CustomTime ct = new CustomTime(totalTime);
     96             mLabel.text = ct.GetTimeFormat();
     97         }
     98     }
     99 
    100     public void Stop()
    101     {
    102         gameObject.SetActive(false);
    103         totalTime = 0;
    104         EvFinish = null;
    105     }
    106 
    107     public bool TimeKeeping
    108     {
    109         get
    110         {
    111             return totalTime > 0;
    112         }
    113     }
    114 
    115     public void StartCountDown(int totalTime, ETimerType timerType)
    116     {
    117         this.timerType = timerType;
    118         if (null == mLabel)
    119             mLabel = GetComponent<UILabel>();
    120         this.totalTime = totalTime;
    121         interval = 0;
    122         enabled = true;
    123         ShowTime();
    124         if (!gameObject.activeSelf)
    125             gameObject.SetActive(true);
    126     }
    127 
    128     public static LabelTimer Begin(GameObject go, int totalTime, ETimerType timerType = ETimerType.CommonFormat)
    129     {
    130         LabelTimer lt = go.GetComponent<LabelTimer>();
    131         if (null == lt) lt = go.AddComponent<LabelTimer>();
    132         lt.StartCountDown(totalTime, timerType);
    133         return lt;
    134     }
    135 }
  • 相关阅读:
    还原大备份mysql文件失败的解决方法
    第一次招聘
    Data Transformation Services (DTS) Package Example
    .net Page 执行事件顺序
    批量修改相同結構的table layout
    js 中exec、test、match、search、replace、split用法
    存储过程分页,按多条件排序(原创)
    Lisp 的本质 [转]
    【原创】jQuery方法的写法不同导致的兼容性
    WinForm 图片变灰方法
  • 原文地址:https://www.cnblogs.com/luguoshuai/p/9802915.html
Copyright © 2020-2023  润新知