💾 코드
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class PoolManagerTemp : MonoBehaviour
{
private Dictionary<string, Queue<GameObject>> poolDictionary;
public static PoolManagerTemp instance;
private void Awake()
{
poolDictionary = new Dictionary<string, Queue<GameObject>>();
}
private void Start()
{
instance = this;
}
private void AddToDictionary(GameObject instanceObj)
//PoolManager의 dictionary에 만들려는 객체의 tag가 없을경우
{
if (!poolDictionary.ContainsKey(instanceObj.tag))
{
poolDictionary.Add(instanceObj.tag, new Queue<GameObject>());
}
AddToQueue(instanceObj);
}
private void AddToQueue(GameObject instanceObj)
//PoolManager의 만들려는 tag의 dictionary의 Queue를 한칸 늘린다
{
GameObject obj = Instantiate(instanceObj);
obj.SetActive(false);
poolDictionary[instanceObj.tag].Enqueue(obj);
}
public GameObject SpawnFromPool(GameObject spawnGameObject)
//소환하려할때 사용
{
if (!poolDictionary.ContainsKey(spawnGameObject.tag))
{
AddToDictionary(spawnGameObject);
}
else if (poolDictionary[spawnGameObject.tag].All(obj => obj.activeSelf))
{
int queueCount = poolDictionary[spawnGameObject.tag].Count;
AddToQueue(spawnGameObject);
while (queueCount == 0)
{
GameObject queueObj = poolDictionary[spawnGameObject.tag].Dequeue();
poolDictionary[spawnGameObject.tag].Enqueue(queueObj);
queueCount--;
}
}
GameObject obj = poolDictionary[spawnGameObject.tag].Dequeue();
poolDictionary[spawnGameObject.tag].Enqueue(obj);
obj.SetActive(true);
return obj;
}
}
📖 참고
참고할 코드 내용