ย  ย  public struct ComponentPool

{

ย  ย  public GameObject gameObject;

ย  ย  public Component component;


ย  ย  public ComponentPool(GameObject go, Component comp)

ย  ย  {

ย  ย  ย  ย  this.gameObject = go;

ย  ย  ย  ย  this.component = comp;

ย  ย  }

}


public class ObjectPooler : MonoBehaviour // ์ด๊ฒŒ ๋งค๋‹ˆ์ €์—ญํ• 

{

ย  ย  static ObjectPooler inst;

ย  ย  void Awake() => inst = this;


ย  ย  Dictionary<string, Queue<ComponentPool>> poolDictionary;


ย  ย  public static void ReturnToPool(ComponentPool obj)

ย  ย  {

ย  ย  ย  ย  inst.poolDictionary[obj.gameObject.name].Enqueue(obj);

ย  ย  }

ย  ย  // ์ดํ•˜ ํ•จ์ˆ˜๋Š” ์ƒ๋žต

}


public class Missile : MonoBehaviour // ์ด๊ฒŒ ๋ถˆ๋Ÿฌ์˜ค๊ณ ์žํ•˜๋Š” ์ปดํฌ๋„ŒํŠธ

{

ย  ย  public MissileData missileData;

ย  ย  private void Start()

ย  ย  {

ย  ย  ย  ย  c_pool = new ComponentPool(gameObject, this);

ย  ย  }


ย  ย  private void OnDisable()

ย  ย  {

ย  ย  ย  ย  ObjectPooler.ReturnToPool(c_pool);

ย  ย  }

}


public class TurretManager : MonoBehaviour // ์—ฌ๊ธฐ์„œ ๋ฏธ์‚ฌ์ผ ๋ถˆ๋Ÿฌ์˜ค๊ณ  ์‹ถ์Œ

{

ย  ย  public IEnumerator Shooting()

ย  ย  {

ย  ย  ย  ย  while (true)

ย  ย  ย  ย  {

ย  ย  ย  ย  ย  ย  for (int i = 0; i < missileCount; i++)

ย  ย  ย  ย  ย  ย  {

ย  ย  ย  ย  ย  ย  ย  ย  ComponentPool pool = ObjectPooler.SpawnFromPool("Missile_P", transform.position, turretHead.transform.rotation);

ย  ย  ย  ย  ย  ย  ย  ย  (pool.component as Missile).missileData = this.missileData;

ย  ย  ย  ย  ย  ย  }

ย  ย  ย  ย  ย  ย  yield return new WaitForSeconds(interval);

ย  ย  ย  ย  }

ย  ย  }

}

}


๋Œ€์ถฉ ์ด๋Ÿฐ๋А๋‚Œ์ด์—ˆ๋Š”๋ฐ..