UnityProjekt/Assets/Scripts/ObjectPool.cs
NorbiPeti eef0a307d0
Remove enemy on many hits, multiple rocket hits
Also added an object pool limit but buffing the rockets seemed to be better
2020-11-07 02:21:50 +01:00

42 lines
No EOL
1.1 KiB
C#

using System.Collections.Generic;
using UnityEngine;
public class ObjectPool
{
public ObjectPool(GameObject prefab, int initialSize)
{
_prefab = prefab;
_objects = new List<GameObject>(initialSize);
for (int i = 0; i < initialSize; i++)
_objects.Add(Object.Instantiate(_prefab));
}
private List<GameObject> _objects;
private GameObject _prefab;
/// <summary>
/// Visszaad egy új objektumot. Aktiválandó, használat után pedig deaktiválandó.
/// </summary>
/// <returns>Egy objektum a poolból.</returns>
public GameObject GetObject(short maxCount = 0)
{
GameObject theRocket = null;
int c = 0;
foreach (var rocket in _objects)
{
if (!rocket.activeSelf)
{
theRocket = rocket;
break;
}
c++;
if (c == maxCount)
return null;
}
if (theRocket is null)
_objects.Add(theRocket = Object.Instantiate(_prefab));
return theRocket;
}
}