zoukankan      html  css  js  c++  java
  • 简单实现一个Unity3d的Timer

    • 数量使用的不太多,没有实现对象池.

      1 using System.Collections;
      2 using System.Collections.Generic;
      3 using UnityEngine;
      4 using UnityEngine.Events;
      5 
      6 public class ZSTimerManager: SingletonGeneric<ZSTimerManager>
      7 {
      8     private List<ZSTimerTask> timers = new List<ZSTimerTask>();
      9     public static ZSTimerManagerStub stub = new GameObject("ZSTimerManagerStub").AddComponent<ZSTimerManagerStub>();
     10 
     11     public ZSTimerTask Create(int total, int interval, int ID)
     12     {
     13         var t = timers.Find(pt => pt.ID == ID);
     14         if (t != null)
     15         {
     16             Debug.LogErrorFormat("the timer what ID = {0} already exist.", ID);
     17             return null;
     18         }
     19         ZSTimerTask task = new ZSTimerTask(ID);
     20         timers.Add(task);
     21 
     22         return task;
     23     }
     24 
     25     public ZSTimerTask GetTimer(int ID)
     26     {
     27         return timers.Find(task => task.ID == ID);
     28     }
     29 
     30     public void RemoveTimer(int ID)
     31     {
     32         var timer = GetTimer(ID);
     33         if (timer == null) Debug.LogErrorFormat("ID = {0} are not found.", ID);
     34         timers.Remove(GetTimer(ID));
     35     }
     36 }
     37 
     38 public class ZSTimerTask
     39 {
     40     /// <summary>
     41     /// 总时间
     42     /// </summary>
     43     private int _total;
     44     /// <summary>
     45     /// 间隔时间
     46     /// </summary>
     47     private int _interval;
     48     /// <summary>
     49     /// ID
     50     /// </summary>
     51     public int ID { get; set; }
     52 
     53     private Coroutine driver;
     54     /// <summary>
     55     /// 剩余时间
     56     /// </summary>
     57     public int Therest { get; private set; }
     58 
     59     public UnityEvent OnStart = new UnityEvent();
     60     public UnityEvent OnComplete = new UnityEvent();
     61     public UnityEvent OnInterval = new UnityEvent();
     62 
     63     public ZSTimerTask(int id)
     64     {
     65         this.ID = id;
     66     }
     67 
     68     public virtual void Start(int total, int interval)
     69     {
     70         _total = total;
     71         Therest = _total;
     72         _interval = interval;
     73         OnStart.Invoke();
     74         driver = ZSTimerManager.stub.StartCoroutine(TickRoutine());
     75     }
     76 
     77     public virtual void Stop()
     78     {
     79         if (driver != null) ZSTimerManager.stub.StopCoroutine(driver);
     80     }
     81 
     82     public virtual void Reset()
     83     {
     84         Therest = _total;
     85         if (driver != null) ZSTimerManager.stub.StopCoroutine(TickRoutine());
     86         driver = ZSTimerManager.stub.StartCoroutine(TickRoutine());
     87     }
     88 
     89     /// <summary>
     90     /// DRIVER
     91     /// </summary>
     92     protected virtual IEnumerator TickRoutine()
     93     {
     94         do
     95         {
     96             OnInterval.Invoke();
     97             yield return new WaitForSeconds(_interval);
     98             Therest -= _interval; 
     99         } while (Therest >= 0);
    100         OnComplete.Invoke();
    101     }
    102 
    103     public virtual void Close()
    104     {
    105         OnStart.RemoveAllListeners();
    106         OnComplete.RemoveAllListeners();
    107         OnInterval.RemoveAllListeners();
    108         if (driver != null) ZSTimerManager.stub.StopCoroutine(TickRoutine());
    109     }
    110 }
    111 
    112 public sealed class ZSTimerManagerStub : MonoBehaviour { }

     
    Smart Timers Manager

  • 相关阅读:
    正则表达式
    kafka Auto offset commit faild reblance
    安装包问题
    身份证头像截取
    web表单
    模板与继承与控制语句
    虚拟环境安装及Hello World
    flask入门脚本解释
    python3 邮件发送
    ASP.NET MVC文件上传简单示例
  • 原文地址:https://www.cnblogs.com/linxmouse/p/7769013.html
Copyright © 2011-2022 走看看