Before 优化 机场

This commit is contained in:
CortexCore
2025-03-10 18:06:44 +08:00
parent 350e6d67b2
commit 1f4e20f512
178 changed files with 17534 additions and 821 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;
}
}