how to populate a list of gameobjects from a folder

Im not sure the best way to do this.

basically i’d like to have an editor where a GUI element has a list of objects you can spawn

but if i decide later to add a new object you can spawn i dont want it to be a huge pain.

So what i’d like to do is basically have code where i have a gameobject list variable
and add every object in a folder.

That way later when i want to add a new object I just drop the object into a prefab spawn folder and the code automagically adds it to the list of spawnable objects in the GUI.

So basically when i write

List MyList;

MyList.add(x);

how can i make x iterate through objects in a folder.

is there some kind of way to do

foreach (gameobject in “directory”)

2 Answers

2

For this type of functionality you need to use the Resources folder.

Unless you're planning on building your project out with all of those resources then I'd go a different route then using the Resources folder. Everything in there gets put into the build whether its used or not.

i don't quite understand this. You mean like even if i load a scene where none of it's used it's still like cached i ram? or you just talking about the size of the install or what? I mean these items are basically different shapes you use in an editor. Though not all shapes will be used all shapes will be available to be used anytime you run the editor. So for the purpose of the scene it's perfectly fine to have all the objects in memory for that scene.

The assets aren't stored in memory, but they are included in the build. Considering that these assets are for editor use only, and assuming that they'll never be available in a build, then the resources folder wouldn't be where you want them. I'm not sure if there is an editor only version of the resources folder or not...if there is then just update your project accordingly. Something for me to investigate too, that.

oh i'm sorry i made that unclear. I meant an editor in game. (like for a player to use to build stuff) Not as in unity editor objects but like halo 4 kind of editor editor. Built into the game.

Make an editor script that creates a menu item which, when clicked, adds all objects in a folder whatever script you want. That way you only need to click a button when you want to update the list. The benefit of this is that you’re assets are not in the resources folder, and you can define differences in your task by creating several menu items that behave differently when you need it to.

well right now i got it to work with the list script. It uses resources.loadall(editor objects folder dir) stores those in a list and iterates through it to create GUI elements It works great so i'd not feel the need to change it UNLESS this resources folder issue is far worse than i understand. Not sure exactly what its all about