Initial commit
This commit is contained in:
52
Assets/Scripts/PlatformScript/PlatformPool.cs
Normal file
52
Assets/Scripts/PlatformScript/PlatformPool.cs
Normal file
@@ -0,0 +1,52 @@
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user