- Home /
Question by
MagnusZane · Mar 08, 2012 at 04:43 PM ·
gameobjectarraysfoldergetdirectories
Get GameObject array from folder
I need to find a way to create a game object array in JS from a folder containing models and prefabs. is there any way to accomplish this?
to be more precise,
need a way to load ALL game objects from the folder without necessarily knowing what their names are..... is there any way to do this(after building)???
Comment
Answer by MagnusZane · Mar 13, 2012 at 04:20 PM
I found the solution. you have to convert the array to a builtin array every time you want to add/Remove an object. i'll post an example:
//*******************************************//
//***************Created by Eddy*************//
//******************************************//
var Contents:Transform[];
//Although in the demo I am going to identify items as its own script *Item* I will still
//identify and add them by thier transform to allow more of a versitile of ways to program a game
//using this simple type of script :P.
function AddItem(Item:Transform){//Add an item to the inventory.
var newContents=new Array(Contents);
newContents.Add(Item);
Debug.Log(Item.name+" Has been added to inventroy");
Contents=newContents.ToBuiltin(Transform);// array to unity builtin array
}
function RemoveItem(Item:Transform){//Removed an item from the inventory.
var newContents=new Array(Contents);
var index=0;
var shouldend=false;
for(var i:Transform in newContents){
if(i==Item){
Debug.Log(Item.name+" Has been removed from inventroy");
newContents.RemoveAt(index);
shouldend=true;
//No need to continue running through the loop since we found our item.
}
index++;//keep track of what index the item is and remove it.
if(shouldend){
Contents=newContents.ToBuiltin(Transform);
return;
}
}
}
function Start(){
var rsources=new Array(Resources.LoadAll("RPGOBJ", Transform));
Contents = rsources;
}
all credit goes to eddy for his inventory example.