[Unity3D]計時器/Timer


原地址:http://blog.sina.com.cn/s/blog_5b6cb9500101aejs.html

https://github.com/xuzhiping7/Unity3d-Timer

項目中管理計時器太混亂難看了,用好聽點的話來說就是代碼不優雅。

 
想了下就隨手簡單寫了個時間管理模塊了。其實有好幾種實現方式的,但是選用了U3D最為合適和簡單的方式寫。效率可能不高,但做小游戲是壓根沒問題的了。
 
原理簡單點來說就是統一管理計時器。
 
每個計時器有自己的開始、暫停、結束、重新開始。當計時結束之后則調用相應的一個或者多個函數。
 
Timer.cs算是基類,TimerManager.cs則是管理每一個計時器的管理程序。根據不同的項目改至適用即可。
 
就那樣,代碼非常簡單。就是一個委托回調。
 
具體代碼放到GITHUB了,有興趣的同學可以上去看看。
 
https://github.com/xuzhiping7/Unity3d-Timer
 
//Coded by ZhipingXu  xuzhiping7@qq.com 
//Too simple, so I do not need to explain, just see the code. Help yourself.

public class Timer{

    //If the Timer is running 
    private bool b_Tricking;

    //Current time
    private float f_CurTime;

    //Time to reach
    private float f_TriggerTime;

    //Use delegate to hold the methods
    public delegate void EventHandler();

    //The trigger event list
    public event EventHandler tick;

    /// <summary>
    /// Init
    /// </summary>
    /// <param name="second">Trigger Time</param>
    public Timer(float second)
    {
        f_CurTime = 0.0f;
        f_TriggerTime = second;
    }
    
    /// <summary>
    /// Start Timer
    /// </summary>
    public void Start()
    {
        b_Tricking = true;
    }
    
    /// <summary>
    /// Update Time
    /// </summary>
    public void Update(float deltaTime)
    {
        if (b_Tricking)
        {
            f_CurTime += deltaTime;

            if (f_CurTime > f_TriggerTime)
            {
                //b_Tricking must set false before tick() , cause if u want to restart in the tick() , b_Tricking would be reset to fasle .
                b_Tricking = false;
                tick();
            }
        }
    }
    

    
    /// <summary>
    /// Stop the Timer
    /// </summary>
    public void Stop()
    {
        b_Tricking = false;
    }

    /// <summary>
    /// Continue the Timer
    /// </summary>
    public void Continue()
    {
        b_Tricking = true;
    }

    /// <summary>
    /// Restart the this Timer
    /// </summary>
    public void Restart()
    {
        b_Tricking = true;
        f_CurTime = 0.0f;
    }

    /// <summary>
    /// Change the trigger time in runtime
    /// </summary>
    /// <param name="second">Trigger Time</param>
    public void ResetTriggerTime(float second)
    {
        f_TriggerTime = second;
    }
}
View Code
using UnityEngine;
using System.Collections;

public class TimerManager : MonoBehaviour
{
    Timer test;

    // Use this for initialization
    void Start () {
        test = new Timer(3.0f);
        test.tick += Test;
        test.tick += Test2;
        test.Start();
    }
    
    // Update is called once per frame
    void Update () {
        
        //If u have many timer 
        //u also can serval frame call one time to save some performance, but the deltaTime u should calculate youself
        //like :(u should define lastTime youself-- float)
        
        /*
        if(Time.frameCount%5 == 0)
        {
            delta = Time.time - lastTime;
            test.Update(Time.deltaTime);
            lastTime = Time.time;
        }
        */
        
        test.Update(Time.deltaTime);
    }
    
    //Some time u may need this to avoid conflict when re-init something , just a tip .
    void OnDestory(){
        test.tick -= Test;
        test.tick -= Test2;
    }
    
    void Test()
    {
        Debug.Log("1");
    }


    void Test2()
    {
        Debug.Log("2");
    }
}
View Code

 

 
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM