天天看点

【小松教你手游开发】【系统模块开发】u3d c# 写一个通用倒计时器

计时器管理类

using UnityEngine;
using System.Collections;
using System;

public class TimerManager : MonoBehaviour 
{

    private static TimerManager m_instance = null;
    public static TimerManager Instance { get { return m_instance; } }

    void Awake()
    {
        m_instance = this;
    }
	// Use this for initialization
	void Start () {
	    
	}
	
	// Update is called once per frame
	void Update () {
	
	}

    public void GetTimer(int second,MyTimer.Mode mode,MyTimer.TimesUp TimesUpDelegate,MyTimer.PerSecond PerSecondDelegate=null)
    {
  GameObject Timer = Instantiate(Resources.Load("Prefabs/Time/MyTimer")) as GameObject;
        Timer.transform.parent = transform;

        Timer.GetComponent<MyTimer>().SetTimer(second);
        Timer.GetComponent<MyTimer>().SetMode(mode);
        Timer.GetComponent<MyTimer>().SetTimesUpDelegate(TimesUpDelegate);
        Timer.GetComponent<MyTimer>().SetPerSecondDelegate(PerSecondDelegate);
        Timer.GetComponent<MyTimer>().StartTimer();
    }
   public String GetTimeBySecond(float seconds)
    {
        return DateTime.Parse(DateTime.Now.ToString("00:00:00")).AddSeconds(seconds).ToString("HH:mm:ss");
    }
}
           

倒计时类

using UnityEngine;
using System.Collections;

public class MyTimer : MonoBehaviour {

    public enum Mode
    {
        PerSecond,
        Once,
        Loop
    }

    public float TotalSeconds;
    public Mode m_mode;
    public delegate void TimesUp();
    public TimesUp timesUp = null;

    public delegate void PerSecond(float timesLeft);
    public PerSecond perSecond = null;

    int m_leaveSeconds;
  void Awake()
    {

    }

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
	
	}
  public void StartTimer()
    {
        switch (m_mode)
        {
            case Mode.PerSecond:
                StartCoroutine(TimerBeginPerSocond());
                break;
            case Mode.Once:
                StartCoroutine(TimerBegin());
                break;
            case Mode.Loop:
                break;
        }
        
        
    }
  public void SetTimer(int seconds)
    {
        TotalSeconds = seconds;
    }

    public void SetMode(Mode mode)
    {
        m_mode = mode;
    }

    public void SetTimesUpDelegate(TimesUp del)
    {
        timesUp = del;
    }
 public void SetPerSecondDelegate(PerSecond del)
    {
        perSecond = del;
    }

    IEnumerator TimerBegin()
    {
        yield return new WaitForSeconds(TotalSeconds);
        timesUp();
        Destroy(gameObject);
    }
  IEnumerator TimerBeginPerSocond()
    {
        yield return new WaitForSeconds(1f);
        if ((TotalSeconds--) > 0)
        {
            perSecond(TotalSeconds);
            StartCoroutine(TimerBeginPerSocond());
        }
        else
        {
            timesUp();
            Destroy(gameObject);
        }

    }
    


}
           

分别挂在两个预制上。管理类预先加载到场景中

public void CreateTimerManager()
    {
        GameObject TimerManager = GameObject.Instantiate(Resources.Load("Prefabs/Manager/TimerManager")) as GameObject;
        TimerManager.name = "TimerManager";
    }
           

每请求一个定时器,调管理类函数,选择模式和返回的回调。可选每秒返回,每次返回,循环也可以但是暂时没这个需求