using System.Collections.Generic;
using UnityEngine;
using static UnityEngine.ParticleSystem;

public class PlayerBulletPools : MonoBehaviour
{
    public static PlayerBulletPools instance;

    [SerializeField]
    private GameObject bulletPrefab1;
    [SerializeField]
    private GameObject bulletPrefab2;
    [SerializeField]
    private GameObject bulletPrefab3;

    [SerializeField] private GameObject PlayerBullet1Pool;
    [SerializeField] private GameObject PlayerBullet2Pool;
    [SerializeField] private GameObject PlayerBullet3Pool;




    private List<GameObject> bullet1Pool = new List<GameObject>();
    private List<GameObject> bullet2Pool = new List<GameObject>();
    private List<GameObject> bullet3Pool = new List<GameObject>();

    [SerializeField]
    private int maxbullets = 10;

    private void Awake()
    {
        if (instance == null)
        {
            instance = this;
        }
        else if (instance != this)
        {
            Destroy(this.gameObject);
        }  
    }

    private void Start()
    {
        for (int i = 0; i < this.maxbullets; i++)
        {
            GameObject bullet1Go = Instantiate(this.bulletPrefab1);
            bullet1Go.SetActive(false);
            bullet1Go.transform.SetParent(PlayerBullet1Pool.transform);
            bullet1Pool.Add(bullet1Go);
        }
        for (int i = 0; i < this.maxbullets; i++)
        {
            GameObject bullet2Go = Instantiate(this.bulletPrefab2);
            bullet2Go.SetActive(false);
            bullet2Go.transform.SetParent(PlayerBullet2Pool.transform);
            bullet2Pool.Add(bullet2Go);
        }
        for (int i = 0; i < this.maxbullets; i++)
        {
            GameObject bullet3Go = Instantiate(this.bulletPrefab3);
            bullet3Go.SetActive(false);
            bullet3Go.transform.SetParent(PlayerBullet3Pool.transform);
            bullet3Pool.Add(bullet3Go);
        }





    }
    public GameObject GetBullet1()
    {
        foreach (GameObject bullet1Go in bullet1Pool)
        {
            if (!bullet1Go.activeSelf)
            {
                return bullet1Go;
            }
        }
        GameObject newBullet1 = Instantiate(bulletPrefab1, PlayerBullet1Pool.transform);
        newBullet1.SetActive(false);
        bullet1Pool.Add(newBullet1);
        return newBullet1;
    }
    public GameObject GetBullet2()
    {
        foreach (GameObject bullet2Go in bullet2Pool)
        {
            if (!bullet2Go.activeSelf)
            {
                return bullet2Go;
            }
        }
        GameObject newBullet2 = Instantiate(bulletPrefab2, PlayerBullet2Pool.transform);
        newBullet2.SetActive(false);
        bullet2Pool.Add(newBullet2);
        return newBullet2;
    }
    public GameObject GetBullet3()
    {
        foreach (GameObject bullet3Go in bullet3Pool)
        {
            if (!bullet3Go.activeSelf)
            {
                return bullet3Go;
            }
        }
        GameObject newBullet3 = Instantiate(bulletPrefab3, PlayerBullet3Pool.transform);
        newBullet3.SetActive(false);
        bullet3Pool.Add(newBullet3);
        return newBullet3;
    }
}

나머지 충돌 감지되서 파괴되는 부분은 collision.gameObject.SetActive(false); 로 변경

반응형

+ Recent posts