(1) Object Pool 이란

인스턴스화하는 데 특히 비용이 많이 드는 많은 수의 개체로 작업해야하고 각 개체가 짧은 시간 동안 만 필요한 경우 전체 응용 프로그램의 성능이 저하를 해결하기 위한 디자인 패턴이며 오브젝트를 생성 / 삭제를 반복하지 않으며 생성 한 오브젝트를 가지고 재사용 하는 방식


(2) Object를 이용한 코드 (Unity Tutorial)

총알생성을 Object Pool로구현

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PoolManager : MonoBehaviour
{

    //ttruen this class into a singleton for easy accessbility.
    private static PoolManager _instance;
    public static PoolManager Instance
    {
        get
        {
            if (_instance == null)
                Debug.LogError("The PoolManager is NULL.");
            return _instance;
        }
    }

    [SerializeField]
    private GameObject _bulletContainer;
    [SerializeField]
    private GameObject _bulletPrefab;
    [SerializeField] //List로 관리
    private List<GameObject> _bulletPool;

    private void Awake()
    {
        _instance = this;
    }

    private void Start()
    {
        _bulletPool = GenerateBullets(10);
    }
    //pregenerate a list of bullets using the bullet prefab

    List<GameObject> GenerateBullets(int amountOfBullets)
    {
        for (int i = 0; i < amountOfBullets; i++)
        {
            GameObject bullet = Instantiate(_bulletPrefab);
            bullet.transform.parent = _bulletContainer.transform;
            bullet.SetActive(false);
            _bulletPool.Add(bullet);
        }
        return _bulletPool;
    }

    public GameObject RequestBullet()
    {
        //loop through the bullet list
        //현재 Hierarchy 창에 활성화가 되지 않은 Object를 찾음
        foreach (var bullet in _bulletPool)
        {
            if (bullet.activeInHierarchy == false)
            {
                //bullet is available
                bullet.SetActive(true);
                return bullet;
            }
        }

		//List의모든 Object가 활성화시 새로 생성
        //need to create a new bullet
        GameObject newBullet = Instantiate(_bulletPrefab);
        newBullet.transform.parent = _bulletContainer.transform;
        _bulletPool.Add(newBullet);

        return newBullet;
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour
{
    [SerializeField]
    private GameObject _bulletPrefab;

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            //communicate with the object pool system
            //Request Bullet
            GameObject bullet = PoolManager.Instance.RequestBullet();
            bullet.transform.position = Vector3.zero;

        }
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Bullet : MonoBehaviour {

    private void OnEnable()
    {
        Invoke("Hide", 1);
    }

    void Hide()
    {
        //re-cycle the gameObject

        this.gameObject.SetActive(false);

        Debug.Log("Hiding GameObject");
    }
}

 

아래 링크는 Github에 있는 프로젝트입니다.

https://github.com/Loafly/ObjectPool

 

Loafly/ObjectPool

Contribute to Loafly/ObjectPool development by creating an account on GitHub.

github.com

 

+ Recent posts