Unity時(shí)間管理
來源:
未知 |
責(zé)任編輯:六月芳菲 |
發(fā)布時(shí)間: 2018-03-07 09:38 | 瀏覽量:
更多精彩unity教程:http://m.594ljc.cn/resource/
時(shí)間管理類TimerManager.cs
using UnityEngine;
using System;
using System.Collections.Generic;
/// <summary>
/// 時(shí)間管理
/// </summary>
public class TimerManager
{
/// <summary>
/// 當(dāng)前游戲運(yùn)行的時(shí)間
/// </summary>
public static float time;
/// <summary>
/// 對(duì)象池
/// </summary>
public static Dictionary<object, TimerItem> timerList = new Dictionary<object, TimerItem>();
/// <summary>
/// 啟動(dòng)
/// </summary>
public static void Run()
{
// 設(shè)置時(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>
/// 注冊(cè)
/// </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>
/// 取消注冊(cè)
/// </summary>
/// <param name="objectItem">Object item.</param>
public static void UnRegister(object objectItem)
{
if (timerList.ContainsKey(objectItem))
{
timerList.Remove(objectItem);
}
}
}
時(shí)間對(duì)象類TimerItem.cs
using UnityEngine;
using System;
public class TimerItem
{
/// <summary>
/// 當(dāng)前時(shí)間
/// </summary>
public float currentTime;
/// <summary>
/// 延遲時(shí)間
/// </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>
/// 啟動(dòng)
/// </summary>
/// <param name="time">Time.</param>
public void Run(float time)
{
// 計(jì)算差值
float offsetTime = time - this.currentTime;
// 如果差值大等于延遲時(shí)間
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;
}
}
}
啟動(dòng)計(jì)時(shí)管理器UpdateItem.cs
using UnityEngine;
/// <summary>
/// 掛靠在一個(gè)永遠(yuǎn)不會(huì)被銷毀的GameObject上面
/// </summary>
public class UpdateItem : MonoBehaviour {
void Update()
{
// 啟動(dòng)計(jì)時(shí)管理器
TimerManager.Run();
}
}
測(cè)試類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("倒計(jì)時(shí)測(cè)試:"+time);
}
else
{
Debug.Log("倒計(jì)時(shí)結(jié)束");
TimerManager.UnRegister(this);
}
});
}
}
更多精彩unity教程:http://m.594ljc.cn/resource/
-
分享到:
相關(guān)文章
網(wǎng)友評(píng)論
您需要登錄后才可以發(fā)帖 登錄 | 立即注冊(cè)
關(guān)閉
- 用戶名:
- 密 碼:
- 驗(yàn)證碼: 看不清? 點(diǎn)擊更換
- 忘記密碼?
全部評(píng)論:0條
推薦
熱門