실행 이미지

 

using UnityEngine;
using UnityEngine.Pool;

public class BulletManager : MonoBehaviour
{
    [SerializeField] GameObject bulletPrefab;
    public static BulletManager Instance { get; private set; }

    private IObjectPool<Bullet> bulletPool;

    private void Awake()
    {
        if (Instance == null)
        {
            Instance = this;
            DontDestroyOnLoad(gameObject);
            InitializeBulletPool();
        }
        else if (Instance != this)
        {
            Destroy(gameObject);
        }
    }

    private void InitializeBulletPool()
    {
        bulletPool = new ObjectPool<Bullet>(CreateBullet,ActivateBullet, DeactivateBullet,DestroyBullet,false,10,20);
    }

    private Bullet CreateBullet()
    {
        var bulletGO = Instantiate(bulletPrefab, transform).GetComponent<Bullet>();
        bulletGO.Pool = bulletPool;
        bulletGO.gameObject.SetActive(false);
        return bulletGO;
    }

    private void ActivateBullet(Bullet bullet)
    {
        bullet.gameObject.SetActive(true);
    }

    private void DeactivateBullet(Bullet bullet)
    {
        bullet.gameObject.SetActive(false);
    }

    private void DestroyBullet(Bullet bullet)
    {
        Destroy(bullet.gameObject);
    }


    public Bullet GetBullet()
    {
        return bulletPool.Get();
    }

    public void ReturnBullet(Bullet bullet)
    {
        bulletPool.Release(bullet);
    }
}

BulletManager.cs (싱글톤)

 

using UnityEngine;
using UnityEngine.Pool;

public class Bullet : MonoBehaviour
{
    public float speed = 10f;
    public IObjectPool<Bullet> Pool { get; set; }

    void Update()
    {
        transform.Translate(Vector3.forward * speed * Time.deltaTime);
    }

    private void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.CompareTag("Wall") || other.gameObject.CompareTag("Player"))
        {
            Pool?.Release(this); // 풀로 반환
        }
    }
}

Bullet.cs

 

using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;

public class Enemy : MonoBehaviour
{
    [SerializeField] Transform playerPos;
    [SerializeField] Transform firePos;
    [SerializeField] Transform shoot_bullet_pool;
    [SerializeField] int speed = 10;


    private void Start()
    {
        StartCoroutine("LookAtPlayerPos");
        StartCoroutine("CoShoot");
    }

   
    IEnumerator LookAtPlayerPos()
    {
        while (true)
        {
            this.gameObject.transform.LookAt(playerPos);

            yield return null;

        }
    }

    IEnumerator CoShoot()
    {
        while (true)
        {
            Bullet bullet = BulletManager.Instance.GetBullet();
            bullet.transform.SetParent(shoot_bullet_pool);
            bullet.transform.position = firePos.position;
            bullet.transform.rotation = firePos.rotation;


           


            yield return new WaitForSeconds(1);
        }
    }

    
    

}

Enemy.cs

 

using Newtonsoft.Json.Bson;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player2 : MonoBehaviour
{
    public float moveSpeed = 5f;
    void Start()
    {
        StartCoroutine("CoMove");
    }



    IEnumerator CoMove()
    {
        while (true)
        {
            float h = Input.GetAxisRaw("Horizontal");
            float v = Input.GetAxisRaw("Vertical");


            Vector3 movement = new Vector3(h, 0f, v) * moveSpeed * Time.deltaTime;

            transform.Translate(movement);

            yield return null;
        }
    }




}

Player2.cs

+ Recent posts