53 lines
1.4 KiB
C#
53 lines
1.4 KiB
C#
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using UnityEngine;
|
|
using VContainer;
|
|
using VContainer.Unity;
|
|
|
|
public class PlatformPool : MonoBehaviour
|
|
{
|
|
public List<GameObject> platformPool = new List<GameObject>();
|
|
public int amountToPool=10;
|
|
public GameObject[] platformPrefab;
|
|
public GameObject PlatformContainer; //parent
|
|
[Inject] private IObjectResolver container;
|
|
|
|
void Awake()
|
|
{
|
|
for(int i=0; i<amountToPool;i++)
|
|
{
|
|
GameObject obj = Instantiate(platformPrefab[Random.Range(0,platformPrefab.Length)]);
|
|
obj.SetActive(false);
|
|
obj.transform.SetParent(PlatformContainer.transform);
|
|
container.InjectGameObject(obj);
|
|
platformPool.Add(obj);
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public GameObject GetPlatformFromPool()
|
|
{
|
|
for(int i=0;i<platformPool.Count;i++)
|
|
{
|
|
if(!platformPool[i].activeInHierarchy)
|
|
{
|
|
return platformPool[i];
|
|
}
|
|
}
|
|
|
|
GameObject newObj = Instantiate(platformPrefab[Random.Range(0,platformPrefab.Length)]);
|
|
platformPool.Add(newObj);
|
|
newObj.transform.SetParent(PlatformContainer.transform);
|
|
container.InjectGameObject(newObj);
|
|
return newObj;
|
|
}
|
|
|
|
public void ReturnToPool(GameObject obj)
|
|
{
|
|
obj.SetActive(false);
|
|
}
|
|
|
|
}
|