38 lines
944 B
C#
38 lines
944 B
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class ObjectPool : MonoBehaviour
|
|
{
|
|
[SerializeField] private GameObject poolObj;
|
|
[SerializeField] private int poolSize;
|
|
[SerializeField] private Transform poolParent;
|
|
|
|
[SerializeField] private List<GameObject> poolList;
|
|
void Start()
|
|
{
|
|
for (int i = 0; i < poolSize; i++)
|
|
{
|
|
GameObject poolTempObj = Instantiate(poolObj);
|
|
poolTempObj.transform.position = Vector3.zero;
|
|
poolTempObj.transform.parent = poolParent;
|
|
poolTempObj.gameObject.SetActive(false);
|
|
|
|
poolList.Add(poolTempObj);
|
|
}
|
|
}
|
|
|
|
public GameObject GetFromPool()
|
|
{
|
|
for (int i = 0; i < poolSize; i++)
|
|
{
|
|
if (!poolList[i].activeInHierarchy)
|
|
{
|
|
return poolList[i];
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
}
|