70 lines
1.2 KiB
C#
70 lines
1.2 KiB
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class PoolManager : MonoBehaviour
|
|
{
|
|
public enum InstanceType
|
|
{
|
|
Deformer = 0,
|
|
Splash = 1,
|
|
Ball = 2
|
|
}
|
|
|
|
public static Dictionary<InstanceType, PoolManager> Instances;
|
|
|
|
public float maxCount = 16f;
|
|
|
|
public bool recycle;
|
|
|
|
private int recycleIndex;
|
|
|
|
public InstanceType type;
|
|
|
|
public GameObject prefab;
|
|
|
|
private void Awake()
|
|
{
|
|
if (Instances == null)
|
|
{
|
|
Instances = new Dictionary<InstanceType, PoolManager>();
|
|
}
|
|
if (!Instances.ContainsKey(type))
|
|
{
|
|
Instances.Add(type, this);
|
|
}
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
for (int i = 0; (float)i < maxCount; i++)
|
|
{
|
|
GameObject obj = Object.Instantiate(prefab);
|
|
obj.transform.parent = base.transform;
|
|
obj.name = type.ToString() + "_" + i;
|
|
obj.SetActive(value: false);
|
|
}
|
|
}
|
|
|
|
public GameObject getNextAvailable()
|
|
{
|
|
foreach (Transform item in base.transform)
|
|
{
|
|
if (!item.gameObject.activeSelf)
|
|
{
|
|
return item.gameObject;
|
|
}
|
|
}
|
|
if (recycle && base.transform.childCount > recycleIndex)
|
|
{
|
|
if ((float)recycleIndex >= maxCount - 1f)
|
|
{
|
|
recycleIndex = 0;
|
|
}
|
|
GameObject result = base.transform.GetChild(recycleIndex).gameObject;
|
|
recycleIndex++;
|
|
return result;
|
|
}
|
|
return null;
|
|
}
|
|
}
|