37 lines
1.1 KiB
C#
37 lines
1.1 KiB
C#
using System;
|
|
using UnityEngine;
|
|
using System.Collections.Generic;
|
|
|
|
namespace BallKingdomCrush
|
|
{
|
|
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);
|
|
}
|
|
}
|
|
} |