4-3. 2조 Pool 관리(List, Dictionary)

keubung·2024년 10월 25일

- Pool에서 List와 Dictionary 차이

1. List : 하나의 풀로 관리 -> 두개의 오브젝트에서 각각을 관리(bulletPool, monsterPool)
- 단일 타입을 관리한다.

using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.Pool;

public class ObjectPool : MonoBehaviour
{  
    public GameObject prefab;
    List<GameObject> pool = new List<GameObject>();
    public int poolSize;

    public string key;

    void Start()
    {
        InitializePool(poolSize, key);
    }
    public GameObject Get(string objectType)
    {
        if (pools.ContainsKey(objectType))
        {
            foreach (GameObject obj in pools[objectType])
            {
                if (!obj.activeSelf)
                {
                    obj.SetActive(true);
                    return obj;
                }
            }
        }
        return null;
    }

    public void Release(GameObject obj)
    {
        obj.SetActive(false);
        obj.transform.SetParent(null);
    }

    private void InitializePool(int size, string objectType)
    {
        if (!pools.ContainsKey(objectType))
        {
            pools[objectType] = new List<GameObject>();
        }

        for (int i = 0; i < size; i++)
        {
            GameObject obj = Instantiate(prefab);
            obj.SetActive(false);
            obj.name = objectType + "_" + i;
            pools[objectType].Add(obj);
        }
    }
}

2. Dictionary : 두개의 풀로 나누어 관리 -> 하나의 오브젝트에서 관리(PoolManaer)
- 여러 타입을 관리한다.

using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.Pool;

public class ObjectPool : MonoBehaviour
{
    private Dictionary<string, List<GameObject>> pools = new Dictionary<string, List<GameObject>>();

    public GameObject bulletPrefab;
    public GameObject monsterPrefab;
    public int bulletPoolSize;
    public int monsterPoolSize;
    string bulletKey = "Bullet";
    string monsterKey = "Monster";

    void Start()
    {
        InitializePool(bulletPoolSize, bulletKey, bulletPrefab);
        InitializePool(monsterPoolSize, monsterKey, monsterPrefab);
    }
    public GameObject Get(string objectType)
    {
        if (pools.ContainsKey(objectType))
        {
            foreach (GameObject obj in pools[objectType])
            {
                if (!obj.activeSelf)
                {
                    obj.SetActive(true);
                    return obj;
                }
            }
        }
        return null;
    }

    public void Release(GameObject obj)
    {
        obj.SetActive(false);
        obj.transform.SetParent(null);
    }

    private void InitializePool(int size, string objectType, GameObject prefab)
    {
        if (!pools.ContainsKey(objectType))
        {
            pools[objectType] = new List<GameObject>();
        }

        for (int i = 0; i < size; i++)
        {
            GameObject obj = Instantiate(prefab);
            obj.SetActive(false);
            obj.name = objectType + "_" + i;
            pools[objectType].Add(obj);
        }
    }
}

+ activeSelf & activeInHierarchy

  1. activeSelf
    : activeSelf는 자기 자신이 SetActive = true라면, activeSelf = true가 된다.
    부모가 있을 경우, 부모가 SetActive = false이더라도 자신은 true라고 판단하기 때문에 부모가 비활성화 상태에도 다음 코드들이 실행되고 이 경우 씬에 보이지 않는다.
    - 오브젝트가 직접적으로 활성화되었는지를 확인할 때 유용
  2. activeInHierarchy
    : activeInHierarchy는 자기 자신이 SetActive = true이더라도 부모의 영향을 받기 때문에 부모가 SetActive = false라면 자신도 activeSelf = false가 된다.
    - 실제로 렌더링되거나 업데이트될 수 있는 상태인지 확인할 때 사용
    - 물리적 충돌이나 상호작용을 체크할 때, GameObject가 계층 구조에서 활성화되어 있는지를 확인하는 데 필요

profile
김나영(Unity_6기)

0개의 댓글