OneShotOneKill/Assets/Script/InGame/Projectile/ProjectileMgr.cs

48 lines
1.0 KiB
C#
Raw Normal View History

2026-01-12 03:23:48 +00:00
using System.Collections.Generic;
using UnityEngine;
2026-01-11 23:53:38 +00:00
public class ProjectileMgr : MonoBehaviourSingletonTemplate<ProjectileMgr>
{
2026-01-12 03:23:48 +00:00
[Header("Pool Setting")]
public Transform tf_parent;
public Projectile projectilePrefab;
public int poolSize = 50;
Queue<Projectile> pool = new Queue<Projectile>();
protected override void Awake()
{
base.Awake();
InitPool();
}
void InitPool()
{
for (int i = 0; i < poolSize; i++)
{
Projectile p = CreateExtra();
p.gameObject.SetActive(false);
pool.Enqueue(p);
}
}
public Projectile Get()
{
Projectile p = pool.Count > 0 ? pool.Dequeue() : CreateExtra();
p.gameObject.SetActive(true);
return p;
}
public void Return(Projectile p)
{
p.gameObject.SetActive(false);
pool.Enqueue(p);
}
2026-01-11 23:53:38 +00:00
2026-01-12 03:23:48 +00:00
Projectile CreateExtra()
{
Projectile p = Instantiate(projectilePrefab, tf_parent);
p.SetOwner(this);
return p;
}
2026-01-11 23:53:38 +00:00
}