본문 바로가기
게임프로젝트/슈팅게임

유니티 에셋을 이용한 오브젝트 풀링

by pudding81 2024. 3. 20.

 

 

 

 

 

 

https://prf.hn/l/5N3Y4Dx

 

Pools | 유틸리티 도구 | Unity Asset Store

Use the Pools from Redcode Games on your next project. Find this utility tool & more on the Unity Asset Store.

assetstore.unity.com

 

 

 

 

 

using Redcode.Pools;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.Tracing;
using UnityEngine;

public class EnemyGenerator2 : MonoBehaviour
{
    [SerializeField]
    private PoolManager poolManager;

    private void Start()
    {
        // PoolManager 인스턴스를 찾습니다.
        PoolManager poolManager = FindObjectOfType<PoolManager>();
        // 코루틴 시작
        StartCoroutine(CreateEnemy());

    }



    IEnumerator CreateEnemy()
    {

        for (int i = 0; i < 10; i++)
        {

            EnemyController enemy = poolManager.GetFromPool<EnemyController>(0);
            // 여기서 0은 풀의 인덱스입니다.

            Debug.LogFormat("enemy {0}", enemy);


            // 가져온 오브젝트를 사용합니다.
            if (enemy != null)
            {
                enemy.gameObject.SetActive(true);

                // 오브젝트가 성공적으로 가져와졌다면, 여기서 추가 작업을 수행합니다.
                enemy.transform.position = new Vector3(Random.Range(-2f, 2f), 6f, 0); // 예를 들어, 오브젝트의 위치를 월드의 중앙으로 설정합니다.

            }

            yield return new WaitForSeconds(2f);

        }


    }

}