2026-04-22 09:52:55 +08:00
|
|
|
using System;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
2026-04-27 11:20:13 +08:00
|
|
|
namespace RedHotRoast
|
2026-04-22 09:52:55 +08:00
|
|
|
{
|
|
|
|
|
public class GObjectsPool
|
|
|
|
|
{
|
|
|
|
|
private Transform m_releaseRoot;
|
|
|
|
|
private Transform m_getRoot;
|
|
|
|
|
private Action<GameObject> m_onNew;
|
|
|
|
|
private Action<GameObject> m_onGet;
|
|
|
|
|
private Action<GameObject> m_onRelease;
|
|
|
|
|
|
|
|
|
|
private Dictionary<string, GObjectPool> m_pools = new();
|
|
|
|
|
|
|
|
|
|
public GameObject Get(string prefabPath, string assetName)
|
|
|
|
|
{
|
|
|
|
|
var poolName = $"{prefabPath} | {assetName}";
|
|
|
|
|
if (!m_pools.ContainsKey(poolName))
|
|
|
|
|
{
|
|
|
|
|
RegisterNew(prefabPath, assetName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GObjectPool pool = m_pools[poolName];
|
|
|
|
|
return pool.Get();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void RegisterNew(string prefabPath, string assetName)
|
|
|
|
|
{
|
|
|
|
|
GObjectPool pool = new GObjectPool();
|
|
|
|
|
pool.Init(prefabPath, assetName, m_releaseRoot, m_getRoot);
|
|
|
|
|
pool.InitCallBack(m_onNew, m_onGet, m_onRelease);
|
|
|
|
|
m_pools.Add($"{prefabPath} | {assetName}", pool);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|