Рубрики
Unity Object Pool Синглтон

Использование пула объектов на примере

BadShooting.cs
не эффиктивность без использования пула объектов

youtube

в каждом Update
инстализируется объект «пуля»
назначается объекту родитель
вызывает метод, который устанавливает начальную позицию и поворот

public class BadShooting:MonoBehaviour;
{
[SerializableField]
private GameObject bullet;

[SerializableField]
private Transform bulletParent;

[SerializableField]
private Vector3 bulletSpawnPosition;

private void Update()
{
var bullet=GetBullet();
bullet.transform.SetParent(bulletParent);

bullet.GetComponent<BadBullet>().OnCreate(bulletSpawnPosition,transform.rotation);
}

private GameObject GetBullet()
{
return Instantiate(bullet);
}

}

BadBullet.cs
пуля летит прямо и через 3 секунды уничтожается

public class BadBullet:MonoBehaviour;
{
private float lifeTime=3;
private float currentLifeTime;
private float speed=10;

public void OnCreate(Vector3 position, Quaternion rotation)
{
transform.position=position;
transfor.rotation=rotation;
currentLifeTime=lifeTime;
}

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

if ((currentLifeTime-=Time.deltaTime)<0)
Destroy(gameObject);
}
}