Pool 적용 버그 잡는 중
This commit is contained in:
@@ -17,12 +17,12 @@ namespace UVC.Pool
|
||||
/// {
|
||||
/// public GameObject bulletPrefab;
|
||||
/// public Transform bulletContainer;
|
||||
/// private MonoBehaviourPool<Bullet> _bulletPool;
|
||||
/// private GameObjectPool<Bulle> _bulletPool;
|
||||
///
|
||||
/// void Start()
|
||||
/// {
|
||||
/// // 총알 프리팹과 부모 컨테이너를 사용하여 풀을 초기화합니다.
|
||||
/// _bulletPool = new MonoBehaviourPool<Bullet>(bulletPrefab, bulletContainer);
|
||||
/// _bulletPool = new GameObjectPool<Bulle>(bulletPrefab, bulletContainer);
|
||||
/// }
|
||||
///
|
||||
/// void SpawnBullet()
|
||||
@@ -42,7 +42,7 @@ namespace UVC.Pool
|
||||
/// </code>
|
||||
/// </summary>
|
||||
/// <typeparam name="T">풀링할 MonoBehaviour 타입입니다.</typeparam>
|
||||
public class MonoBehaviourPool<T> where T : MonoBehaviour
|
||||
public class GameObjectPool<T> where T : MonoBehaviour
|
||||
{
|
||||
/// <summary>
|
||||
/// Resources 폴더에 있는 프리팹의 경로입니다.
|
||||
@@ -88,13 +88,19 @@ namespace UVC.Pool
|
||||
/// </summary>
|
||||
public Transform? RecycledItemContainer { get { return _recycledItemContainer; } }
|
||||
|
||||
// --- 통계용 필드 ---
|
||||
private int _inUseCount = 0;
|
||||
private int _peakUsage = 0;
|
||||
private int _poolMisses = 0;
|
||||
private readonly object _statsLock = new object();
|
||||
|
||||
/// <summary>
|
||||
/// GameObject 프리팹을 사용하여 풀을 초기화합니다.
|
||||
/// </summary>
|
||||
/// <param name="originalPrefab">풀링할 오브젝트의 원본 프리팹입니다.</param>
|
||||
/// <param name="activeItemContainer">활성화된 오브젝트가 위치할 부모 Transform입니다.</param>
|
||||
/// <param name="recycledItemContainer">비활성화된 오브젝트가 위치할 부모 Transform입니다. 지정하지 않으면 activeItemContainer가 사용됩니다.</param>
|
||||
public MonoBehaviourPool(GameObject originalPrefab, Transform activeItemContainer, Transform recycledItemContainer = null)
|
||||
public GameObjectPool(GameObject originalPrefab, Transform activeItemContainer, Transform recycledItemContainer = null)
|
||||
{
|
||||
_originalPrefab = originalPrefab;
|
||||
_activeItemContainer = activeItemContainer;
|
||||
@@ -109,7 +115,7 @@ namespace UVC.Pool
|
||||
/// <param name="prefabsPath">Resources 폴더 기준의 프리팹 경로입니다.</param>
|
||||
/// <param name="activeItemContainer">활성화된 오브젝트가 위치할 부모 Transform입니다.</param>
|
||||
/// <param name="recycledItemContainer">비활성화된 오브젝트가 위치할 부모 Transform입니다. 지정하지 않으면 activeItemContainer가 사용됩니다.</param>
|
||||
public MonoBehaviourPool(string prefabsPath, Transform activeItemContainer, Transform recycledItemContainer)
|
||||
public GameObjectPool(string prefabsPath, Transform activeItemContainer, Transform recycledItemContainer)
|
||||
{
|
||||
_prefabsPath = prefabsPath;
|
||||
_originalPrefab = Resources.Load<GameObject>(prefabsPath);
|
||||
@@ -133,6 +139,16 @@ namespace UVC.Pool
|
||||
}
|
||||
|
||||
T? item = null;
|
||||
|
||||
lock (_statsLock)
|
||||
{
|
||||
_inUseCount++;
|
||||
if (_inUseCount > _peakUsage)
|
||||
{
|
||||
_peakUsage = _inUseCount;
|
||||
}
|
||||
}
|
||||
|
||||
if (_recycledItems.Count > 0)
|
||||
{
|
||||
item = _recycledItems[0];
|
||||
@@ -140,6 +156,11 @@ namespace UVC.Pool
|
||||
}
|
||||
else
|
||||
{
|
||||
lock (_statsLock)
|
||||
{
|
||||
_poolMisses++;
|
||||
}
|
||||
|
||||
GameObject go = UnityEngine.Object.Instantiate(_originalPrefab);
|
||||
item = go.GetComponent<T>();
|
||||
}
|
||||
@@ -173,6 +194,11 @@ namespace UVC.Pool
|
||||
return; // 키에 해당하는 아이템이 없으면 반환
|
||||
}
|
||||
|
||||
lock (_statsLock)
|
||||
{
|
||||
_inUseCount--;
|
||||
}
|
||||
|
||||
_activeItems.Remove(key);
|
||||
_recycledItems.Add(item);
|
||||
|
||||
@@ -224,5 +250,26 @@ namespace UVC.Pool
|
||||
}
|
||||
_recycledItems.Clear();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 풀의 현재 성능 통계를 문자열로 반환합니다.
|
||||
/// </summary>
|
||||
/// <returns>풀 통계 (최대 사용량, 현재 사용량, 풀 비어있을 때 생성 횟수, 현재 풀 크기)</returns>
|
||||
public string GetStats()
|
||||
{
|
||||
return $"최대 사용량: {_peakUsage}, 현재 사용량: {_inUseCount}, 풀 비어있을 때 생성 횟수: {_poolMisses}, 현재 풀 크기: {_recycledItems.Count}";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 풀 통계를 초기화합니다.
|
||||
/// </summary>
|
||||
public void ResetStats()
|
||||
{
|
||||
lock (_statsLock)
|
||||
{
|
||||
_peakUsage = 0;
|
||||
_poolMisses = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/UVC/Pool/GameObjectPool.cs.meta
Normal file
2
Assets/Scripts/UVC/Pool/GameObjectPool.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e163d2d37fef9bd49acd0f3fa5b5d2dc
|
||||
@@ -1,2 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e9be41afc37616649924d7190be6215a
|
||||
Reference in New Issue
Block a user