using System.Collections.Generic;
namespace NBC.Asset
{
public interface IRecyclable
{
bool IsDestroyed { get; }
bool CanDestroy { get; }
void Destroy();
}
///
/// 资源回收器
///
public static class Recycler
{
///
/// 当前运行的回收任务
///
static readonly List Coroutines = new List();
///
/// 准备要运行的回收任务
///
static readonly List ReadyTask = new List();
public static void Add(IRecyclable recyclable)
{
ReadyTask.Add(recyclable);
}
///
/// 取消回收
///
///
public static void Cancel(IRecyclable recyclable)
{
ReadyTask.Remove(recyclable);
}
public static void Update()
{
//正在加载时,不卸载资源
if (TaskRunner.ProviderRunner.RunningTaskNum > 0) return;
for (var i = 0; i < ReadyTask.Count; i++)
{
var task = ReadyTask[i];
if (!task.CanDestroy) continue;
ReadyTask.RemoveAt(i);
Coroutines.Add(task);
i--;
}
for (var i = 0; i < Coroutines.Count; i++)
{
var task = Coroutines[i];
Coroutines.RemoveAt(i);
i--;
if (task.IsDestroyed) continue;
if (task.CanDestroy) task.Destroy();
}
}
}
}