? 色综合青青网,久久精品国产四虎
聯(lián)系我們

給我們留言

聯(lián)系我們

地址:福建省晉江市青陽街道洪山路國際工業(yè)設(shè)計園納金網(wǎng)

郵箱:info@narkii.com

電話:0595-82682267

(周一到周五, 周六周日休息)

當(dāng)前位置:主頁 > 3D教程 > 圖文教程

Unity時間管理

來源: 未知 | 責(zé)任編輯:六月芳菲 | 發(fā)布時間: 2018-03-07 09:38 | 瀏覽量:

更多精彩unity教程:http://trusteddivorcelawyers.com/resource/



時間管理類TimerManager.cs
using UnityEngine;
using System;
using System.Collections.Generic;
 
/// <summary>
/// 時間管理
/// </summary>
public class TimerManager
{
    /// <summary>
    /// 當(dāng)前游戲運行的時間
    /// </summary>
    public static float time;
 
    /// <summary>
    /// 對象池
    /// </summary>
    public static Dictionary<object, TimerItem> timerList = new Dictionary<object, TimerItem>();
 
    /// <summary>
    /// 啟動
    /// </summary>
    public static void Run()
    {
        // 設(shè)置時間值
        TimerManager.time = Time.time;
 
        TimerItem[] objectList = new TimerItem[timerList.Values.Count];
        timerList.Values.CopyTo(objectList, 0);
 
        // 鎖定
        foreach (TimerItem timerItem in objectList)
        {
            if (timerItem != null) timerItem.Run(TimerManager.time);
        }
    }
 
    /// <summary>
    /// 注冊
    /// </summary>
    /// <param name="objectItem">Object item.</param>
    /// <param name="delayTime">Delay magicTime.</param>
    /// <param name="callback">Callback.</param>
    public static void Register(object objectItem, float delayTime, Action callback)
    {
        if (!timerList.ContainsKey(objectItem))
        {
            TimerItem timerItem = new TimerItem(TimerManager.time, delayTime, callback);
            timerList.Add(objectItem, timerItem);
        }
    }
 
    /// <summary>
    /// 取消注冊
    /// </summary>
    /// <param name="objectItem">Object item.</param>
    public static void UnRegister(object objectItem)
    {
        if (timerList.ContainsKey(objectItem))
        {
            timerList.Remove(objectItem);
        }
    }
}
 
時間對象類TimerItem.cs
using UnityEngine;
using System;
 
public class TimerItem
{
    /// <summary>
    /// 當(dāng)前時間
    /// </summary>
    public float currentTime;
 
    /// <summary>
    /// 延遲時間
    /// </summary>
    public float delayTime;
 
    /// <summary>
    /// 回調(diào)函數(shù)
    /// </summary>
    public Action callback;
 
    public TimerItem(float time, float delayTime, Action callback)
    {
        this.currentTime = time;
        this.delayTime = delayTime;
        this.callback = callback;
    }
 
    /// <summary>
    /// 啟動
    /// </summary>
    /// <param name="time">Time.</param>
    public void Run(float time)
    {
        // 計算差值
        float offsetTime = time - this.currentTime;
 
        // 如果差值大等于延遲時間
        if (offsetTime >= this.delayTime)
        {
            float count = offsetTime / this.delayTime - 1;
            float mod = offsetTime % this.delayTime;
 
            for (int index = 0; index < count; index++)
            {
                this.callback();
            }
 
            this.currentTime = time - mod;
        }
    }
}
 
啟動計時管理器UpdateItem.cs
using UnityEngine;
/// <summary>
/// 掛靠在一個永遠不會被銷毀的GameObject上面
/// </summary>
public class UpdateItem : MonoBehaviour {
     
    void Update()
    {
        // 啟動計時管理器
        TimerManager.Run();
    }
}
 
測試類Test.cs
using UnityEngine;
 
public class Test : MonoBehaviour {
    float time = 5f;
        // Use this for initialization
        void Start () {
        TimerManager.Register(this, 1f, () => {
            if (time>0)
            {
                time -= 1f;
                Debug.Log("倒計時測試:"+time);
            }
            else
            {
                Debug.Log("倒計時結(jié)束");
                TimerManager.UnRegister(this);
            }
 
        });
        }
         
}
 

更多精彩unity教程:http://trusteddivorcelawyers.com/resource/


相關(guān)文章
網(wǎng)友評論

您需要登錄后才可以發(fā)帖 登錄 | 立即注冊

關(guān)閉

全部評論:0條

推薦
熱門