This commit is contained in:
CortexCore
2025-03-09 13:38:23 +08:00
parent 8261a458e2
commit 18239a5ae4
67 changed files with 8573 additions and 831 deletions

View File

@@ -42,7 +42,9 @@ namespace BITKit.Pool
ReadyPool.Clear();
}
public async UniTask<T> Spawn<T>(string path) where T : class
public int DefaultCapacity { get; set; } = 8;
public async UniTask<T> Spawn<T>(string path,object prefab) where T : class
{
if (Pool.ContainsKey(path))
{
@@ -55,7 +57,7 @@ namespace BITKit.Pool
return obj as T;
}
if (usingList.Count>0)
if (usingList.Count>=DefaultCapacity)
{
obj = usingList[0];
usingList.RemoveAt(0);
@@ -81,13 +83,33 @@ namespace BITKit.Pool
#if UNITY_5_3_OR_NEWER
if (typeof(Object).IsAssignableFrom(typeof(T)))
{
var asset =await ModService.LoadAsset<T>(path);
var asset =prefab as T ?? await ModService.LoadAsset<T>(path);
if (asset is Object o)
{
var instance = Object.Instantiate(o);
list.Add(instance);
UsingPool.GetOrCreate(path).Add(instance);
ReadyPool.GetOrCreate(path);
if (instance is GameObject gameObject)
{
gameObject.GetCancellationTokenOnDestroy().Register(DisposeGo);
void DisposeGo()
{
Pool.GetOrCreate(path).TryRemove(gameObject);
UsingPool.GetOrCreate(path).TryRemove(gameObject);
var queue = ReadyPool.GetOrCreate(path);
var removeObjectList = new List<object>(queue);
removeObjectList.TryRemove(gameObject);
queue.Clear();
foreach (var x in removeObjectList)
{
queue.Enqueue(x);
}
}
}
return instance as T;
}
}