728x90
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class ObjectInfo
{
public string key;
public GameObject goPrefab;
public int count;
public Transform tfPoolParent;
}
public class ObjectPool : MonoBehaviour
{
public static ObjectPool Instance { get; private set; }
public ObjectInfo[] objectInfo = null;
private readonly Dictionary<string, Queue<GameObject>> pool = new();
private void Awake()
{
Instance = this;
}
public GameObject GetObject(string key)
{
var obj = pool[key].Dequeue();
obj.SetActive(true);
return obj;
}
public void PoolObject(string key, GameObject obj)
{
obj.SetActive(false);
pool[key].Enqueue(obj);
}
private void Start()
{
foreach (var item in objectInfo)
{
Queue<GameObject> queue = new();
for (int i = 0; i < item.count; i++)
{
var obj = Instantiate(item.goPrefab, item.tfPoolParent);
obj.SetActive(false);
queue.Enqueue(obj);
}
pool.Add(item.key, queue);
}
}
}
728x90
'기타 기술 > C#' 카테고리의 다른 글
ResourceManager (0) | 2023.08.12 |
---|---|
Singleton pattern (0) | 2023.08.12 |
NavMeshAgent (0) | 2023.08.11 |
이진 트리로 미로 생성 (0) | 2023.08.11 |
LoadingScene (0) | 2023.07.07 |