码迷,mamicode.com
首页 > 其他好文 > 详细

简单的Timer初版

时间:2017-11-01 23:53:32      阅读:302      评论:0      收藏:0      [点我收藏+]

标签:style   date   var   inter   value   private   creat   color   isp   

  1 // FILE: ZSTimer.cs
  2 using System.Collections;
  3 using System.Collections.Generic;
  4 using UnityEngine;
  5 using UnityEngine.Events;
  6 
  7 public class ZSTimerManager
  8 {
  9     private List<ZSTimerTask> tasks = new List<ZSTimerTask>();
 10     static private ZSTimerManager _instance;
 11     static public ZSTimerManager Instance
 12     {
 13         get
 14         {
 15             if (_instance == null)
 16             {
 17                 _instance = new ZSTimerManager();
 18                 GameObject obj = new GameObject("ZSTimerManager");
 19                 Object.DontDestroyOnLoad(obj);
 20                 obj.AddComponent<ZSTimerDriver>();
 21             }
 22             return _instance;
 23         }
 24     }
 25 
 26     public void Create(float dura, float interval)
 27     {
 28         ZSTimerTask task = new ZSTimerTask(dura, interval);
 29         tasks.Add(task);
 30     }
 31 
 32     public void Delete(ZSTimerTask task)
 33     {
 34         tasks.Remove(task);
 35     }
 36 
 37     public void Update()
 38     {
 39         foreach (var zstimerTask in tasks)
 40         {
 41             zstimerTask.Update();
 42         }
 43     }
 44 }
 45 
 46 public class ZSTimerTask
 47 {
 48     /// <summary>
 49     /// 总时间
 50     /// </summary>
 51     private float dura;
 52     /// <summary>
 53     /// 计时量
 54     /// </summary>
 55     private float delta;
 56     /// <summary>
 57     /// 间隔事件
 58     /// </summary>
 59     private float interval;
 60     /// <summary>
 61     /// 下次间隔时间
 62     /// </summary>
 63     private float nextinterval;
 64 
 65     /// <summary>
 66     /// 剩余时间
 67     /// </summary>
 68     public float RemainingValue { get; set; }
 69     /// <summary>
 70     /// 剩余百分比
 71     /// </summary>
 72     public float RemainingRate { get { return (dura - delta) / dura; } }
 73 
 74     public UnityEvent OnStart;
 75     public UnityEvent OnDispose;
 76     public UnityEvent OnInterval;
 77 
 78     public ZSTimerTask(float dura, float interval)
 79     {
 80         delta = 0;
 81         this.dura = dura;
 82         this.interval = interval;
 83         this.nextinterval = interval;
 84 
 85         // 启动事件
 86         OnStart.Invoke();
 87     }
 88 
 89     private void Dispose()
 90     {
 91         // 结束事件
 92         OnDispose.Invoke();
 93     }
 94 
 95     public void Update()
 96     {
 97         delta += Time.deltaTime;
 98         if (interval > 0 && delta >= nextinterval)
 99         {           
100             OnInterval.Invoke();
101             nextinterval += interval;
102         }
103 
104         if (delta >= dura)
105         {
106             OnDispose.Invoke();
107             ZSTimerManager.Instance.Delete(this);
108         }
109     }
110 }
 1 // FILE: ZSTimerDriver.cs
 2 using System.Collections;
 3 using System.Collections.Generic;
 4 using UnityEngine;
 5 
 6 public class ZSTimerDriver : MonoBehaviour {
 7 
 8     // Use this for initialization
 9     void Start ()
10     {
11         
12     }
13     
14     // Update is called once per frame
15     void Update ()
16     {
17         ZSTimerManager.Instance.Update();
18     }
19 }

 

简单的Timer初版

标签:style   date   var   inter   value   private   creat   color   isp   

原文地址:http://www.cnblogs.com/linxmouse/p/7769013.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!