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

54 lines
1.2 KiB
C#

using System.Collections.Generic;
using UnityEngine;
public class ProjectileMgr : MonoBehaviourSingletonTemplate<ProjectileMgr>
{
[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(Transform tf)
{
return Get(tf.position, tf.rotation);
}
public Projectile Get(Vector3 pos, Quaternion q)
{
Projectile p = pool.Count > 0 ? pool.Dequeue() : CreateExtra();
p.gameObject.SetActive(true);
p.transform.position = pos;
p.transform.rotation = q;
return p;
}
public void Return(Projectile p)
{
p.gameObject.SetActive(false);
pool.Enqueue(p);
}
Projectile CreateExtra()
{
Projectile p = Instantiate(projectilePrefab, tf_parent);
p.SetOwner(this);
return p;
}
}