using System;
using System.Collections.Generic;
using UnityEngine;
public class CountDownData
{
public int index;
public float lastTimeRecord;
public float intervalTime;
public bool isPlay;
public Action Callback;
public void UpdateDate(int index, float intervalTime, bool isPlay, Action Callback)
{
this.index = index;
this.lastTimeRecord = Time.timeSinceLevelLoad;
this.intervalTime = intervalTime;
this.isPlay = isPlay;
this.Callback = Callback;
}
}
public class TimeTool : MonoBehaviour
{
public static TimeTool Instance { get; set; }
private int index;
private List
public void Awake()
{
Instance = this;
}
// Start is called before the first frame update
public int AddCountdown(float intervalTime, bool isPlay, Action Callback)
{
CountDownData timeData = new CountDownData();
index++;
timeData.UpdateDate(index, intervalTime, isPlay, Callback);
countDownDataList.Add(timeData);
return index;
}
public void UpdateCountDown(float intervalTime, bool isPlay, Action Callback)
{
GetCountData(index).UpdateDate(index, intervalTime, isPlay, Callback);
}
public void SetCountdownState(int index, bool isPlay)
{
GetCountData(index).isPlay = isPlay;
}
public void RemoveCountdown(int index)
{
countDownDataList.RemoveAt(index - 1);
//CountDownData countDownData = null;
//for (int i = 0; i < countDownDataList.Count; i++)
//{
// if (countDownDataList[i].index == index)
// {
// countDownData = countDownDataList[i];
// break;
// }
//}
//if (countDownData != null)
//{
// countDownDataList.Remove(countDownData);
//}
}
private CountDownData GetCountData(int index)
{
for (int i = 0; i < countDownDataList.Count; i++)
{
if (countDownDataList[i].index == index)
{
return countDownDataList[i];
}
}
return null;
}
// Update is called once per frame
void Update()
{
float time = Time.timeSinceLevelLoad;
if (countDownDataList.Count > 0)
{
for (int i = 0; i < countDownDataList.Count; i++)
{
CountDownData item = countDownDataList[i];
if (time - item.lastTimeRecord >= item.intervalTime && item.isPlay)
{
item.Callback();
item.lastTimeRecord = time;
}
}
}
}
public void RemoveAll()
{
countDownDataList.Clear();
}
}