using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BulletPool : MonoBehaviour
{
public static BulletPool instance;
[SerializeField]
int MAX_SIZE = 20;
[SerializeField]
public GameObject bulletPrefab = null;
Queue<Bullet> bulletQueue = null;
Bullet tempBullet = null;
void Awake()
{
instance = this;
InitializePool();
}
void InitializePool()
{
bulletQueue = new Queue<Bullet>();
for(int i = 0; i < MAX_SIZE; i++)
{
tempBullet = Instantiate(bulletPrefab, transform.position, Quaternion.identity).GetComponent<Bullet>();
if(tempBullet != null)
{
tempBullet.transform.SetParent(transform);
tempBullet.gameObject.SetActive(false);
bulletQueue.Enqueue(tempBullet);
}
else
{
Debug.Log("Bullet not found");
}
}
}
public void AllocateBullet(Gun currentGun)
{
if(bulletQueue.Count != 0)
{
Bullet spawnedBullet = bulletQueue.Dequeue();
spawnedBullet.transform.position = currentGun.transform.position;
spawnedBullet.transform.rotation = currentGun.transform.rotation;
spawnedBullet.gameObject.SetActive(true);
}
}
public void FreeBullet(Bullet freedBullet)
{
freedBullet.gameObject.SetActive(false);
freedBullet.transform.position = transform.position;
bulletQueue.Enqueue(freedBullet);
}
}
์ฝ๋๊ฐ ๊ฐ์ ์ ํ์ ์ ๋๋ก ๋์ํ๋๋ฐ ๋ถ๋ฆฟ ํ์ Instantiate๊ฐ null์ ๋ฐํํด์ ์๋์ ๋์๋ค์ด ์คํ์ด ์๋ฉ๋๋ค..
์์ฑ์ ์ ๋๋ก ๋๋๋ฐ ์ ์ด๋ฌ๋ ๊ฑธ๊น์
ํด๋น ๋๊ธ์ ์ญ์ ๋์์ต๋๋ค.
๋น๊ทผ - dc App
ํ์ ์ง์ฐ๊ณ ๋ค์ ํ๋ฆฌํน์ ์ง์ฐ๊ณ ๋ค์ ๋๋ค ํด๋ด - dc App
์ ํ๋ฆฌํน ์์ ํ๊ฑฐ ์ ์ฉ์ํจ ๊ณ ๋ง์.. - dc App
ใฑใ ใฑใ - dc App
InitializePool ์ Awake ๊ฐ ์๋๋ผ Start ์ ๋ฃ๊ณ ํด๋ณด์
์์ ํด๊ฒฐ..๋ ๋ธ ์ ์ - dc App