Reputation: 1
im making an unity2d game and need an object pooler to generate the ground. however in this version, instead of generating the ground sprites individually, i need to generate a group of them in a specific layout. The pooler i have only generates them individually
using JetBrains.Annotations;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ObjectPooler : MonoBehaviour
{
public GameObject pooledObject;
public int pooledamnt;
List<GameObject> pooledObjects;
// Start is called before the first frame update
void Start()
{
pooledObjects = new List<GameObject>();
for (int i = 0; i < pooledamnt; i++)
{
GameObject obj = (GameObject)Instantiate(pooledObject);
obj.SetActive(false);
pooledObjects.Add(obj);
}
}
public GameObject GetPooledObject()
{
for (int i = 0; i < pooledObjects.Count; i++)
{
if (pooledObjects[i].activeInHierarchy)
{
return pooledObjects[i];
}
}
GameObject obj = (GameObject)Instantiate(pooledObject);
obj.SetActive(false);
pooledObjects.Add(obj);
return obj;
}
// Update is called once per frame
void Update()
{
}
}
all help appreciated.
Upvotes: 0
Views: 50
Reputation: 131
In Unity, for each layout you want, you can make prefabs with your objects manually. Create an empty object and make your ground objects child of it. Then assign them
Like:
public List<GameObject> pooledObjects;
Upvotes: 1