Don't know where to start, first i don't speak perfect english sorry for that heh.
What you need is a function to loop from all your objects and when it finds one inactive store it on a variable.
You already have an array or list, now you have to make a for loop or for each loop (if you don't know what im talking make a quick search its not a difficult concept, im not a pro neither)
for example:
//THIS GOES THROUGH YOU WHOLE ARRAY ONE BY ONE
for(var i = 0;i < objectList.Length; i++) {
var objectToReturn : GameObject;
if(!objectList[i].activeInHerarchy){
// THE "!" MEANS THAT "IF NOT" IN THIS CASE IF ITS NOT ACTIVE IN HERARCHY
objectToReturn = objectList[i];
return objectToReturn;
}
}
if you put this on a function and sotre it inside a variable you'll have the object inside that variable :
var poolObject : GameObject;
poolObject = GiveMeOnePoolObject();
function GiveMeOnePoolObject(){
//THIS GOES THROUGH YOU WHOLE ARRAY ONE BY ONE
for(var i = 0;i < objectList.Length; i++) {
var objectToReturn : GameObject;
if(!objectList[i].activeInHerarchy){
// THE "!" MEANS THAT "IF NOT" IN THIS CASE IF ITS NOT ACTIVE IN HERARCHY
objectToReturn = objectList[i];
return objectToReturn;
}
}
}
I have made my own script for multiple pools of objects i just have to fill the quantity and object i want to instantiate inside the pool and then use some functions to play with them
i'll leave it here if you want to use it or study it.
#pragma strict
import System.Collections.Generic;
//CREAR PILETAS DINAMICAS =START=
public class ObjectsPool extends System.Object {
var Objeto : GameObject;
var Cantidad : int;
var Escalable : boolean;
var Pileta = new List.();
}
var GrupoDePiletas : ObjectsPool[];
function Start () {
for(var piletas : ObjectsPool in GrupoDePiletas){
for(var i = 0;i < piletas.Cantidad;i++){
var obj = Instantiate(piletas.Objeto,Vector3(0,0,0),Quaternion.identity);
obj.SetActive(false);
piletas.Pileta.Add(obj);
}
}
}
//CREAR PILETAS DINAMICAS =END=
function LoadObject(PoolNumber : int) {
for(var i = 0;i < GrupoDePiletas[PoolNumber].Pileta.Count;i++){
if(!GrupoDePiletas[PoolNumber].Pileta[i].activeInHierarchy){
return GrupoDePiletas[PoolNumber].Pileta[i];
}else if(GrupoDePiletas[PoolNumber].Pileta[i].activeInHierarchy && i == GrupoDePiletas[PoolNumber].Pileta.Count-1 && GrupoDePiletas[PoolNumber].Escalable){
var obj = Instantiate(GrupoDePiletas[PoolNumber].Objeto,Vector3(0,0,0),Quaternion.identity);
obj.SetActive(false);
GrupoDePiletas[PoolNumber].Pileta.Add(obj);
return obj;
}
}
return null;
}
function CheckActives(PoolNumber : int) {
var activeObjects : int;
for(var i = 0;i < GrupoDePiletas[PoolNumber].Pileta.Count;i++){
if(GrupoDePiletas[PoolNumber].Pileta[i].activeInHierarchy){
activeObjects++;
}
}
return activeObjects;
}
function DisablePool(PoolNumber : int) {
for(var i = 0;i < GrupoDePiletas[PoolNumber].Pileta.Count;i++){
if(GrupoDePiletas[PoolNumber].Pileta[i].activeInHierarchy){
GrupoDePiletas[PoolNumber].Pileta[i].SetActive(false);
}
}
}
↧