2
\$\begingroup\$

This seems like it should be a simple thing, but I cannot seem to figure out a method.

Currently, I have a public List<MapGraphItem> roomList variable, and in the inspector, I add prefabs from Tiled2Unity for a given biome - currently a list of nine tiles for my test biome. Map generation works great via this method. MapGraphItem is a custom class that is applied to these prefabs via script.

I'd like to programmatically build the list so that I can build it appropriately based on the biome that's being called (dungeon, forest, desert, et cetera). I seem to be having trouble getting unity to put together a list of the items anyhow (they are in a specific assets folder and also have a specific prefix, in this case "dng_").

I tried this and some variations on it:

List<GameObject> _obj = GameObject.FindObjectsOfType(typeof(GameObject)).Select(g => g as GameObject).Where(g => g.name.StartsWith("dng_")).ToList();

But it seems the objects need to be loaded first (plus, I'd need to convert it to a List<MapGraphItem> before using it).

I could add another field to the MapGraphItem class for biome type and basically load ALL of the assets, but that's a huge waste of resources. Manually adding these for each biome is really not ideal, and really not efficient.

Thanks!

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

UPDATE: It appears that using the Resources folder was the best way to handle this. In the end, I scrapped the methodology I was using entirely and have gone to something different, so I'm not currently using this solution, though I'm likely to again in the near future.

The basis for using Resources folders can be found here: https://docs.unity3d.com/Manual/LoadingResourcesatRuntime.html

The short answer is to create a folder called Resources anywhere in your project assets area. There can even be more than one of these folders in various locations for organization. You can then programmatically load these at runtime using Resources.Load() as outlined here.

This allows you to control where your resources exist logically in the layout of your project, and even call resources from multiple groups into the same script.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ How has this gone? Did that solution scale well for the game? If so, it would be great to flesh out the description here to help future users implement your solution, then mark it accepted. If you're still looking for a better solution, editing the question to describe problems/limitations you've found with the present version might help clarify the problem space and attract new answers. \$\endgroup\$
    – DMGregory
    Commented Nov 10, 2016 at 21:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .