NiklasEi / bevy_asset_loader

Bevy plugin helping with asset loading and organization
Apache License 2.0
474 stars 52 forks source link

Support for nested folders #41

Closed makspll closed 2 years ago

makspll commented 2 years ago

I would love to see some support for nested folder structures, for example:

- assets
    - scripts 
         - init.lua
         - hello.lua
         - more_scripts
             - script.lua

perhaps in the form of an argument to the asset attribute, maybe recursive ?

struct LuaAssets {
    #[asset(path = "scripts", folder(typed), recursive(true))]
    folder: Vec<Handle<LuaFile>>
}

alternatively the folder could be modelled as a dictionary, with keys representing the relative/absolute paths, in case these have some meaning to the developer.

In the case that the folder contains mixed assets, these could simply be ignored to accommodate use-cases where different asset types must be stored alongside each other.

Happy to help if necessary!

NiklasEi commented 2 years ago

Under the hood, bevy_asset_loader is just using Bevy's load_folder here, which already is recursive. Your example should work without any new attribute needed. Does it not work for you?

Filtering for specific types is something I would see in the engine, not in this plugin. Possible APIs in Bevy have been discussed before (see e.g. bevyengine/bevy#2292).

makspll commented 2 years ago

Ah! I wasn't aware of the resistivity of folder loading! Perhaps it's me being blind or maybe this could be made clearer in the docs ?

Maybe it would be worth changing:

You can load all assets in a folder and keep them in an AssetCollection as a vector of untyped handles.

to:

You can recursively load all assets in a folder and keep them in an AssetCollection as a vector of untyped handles.

That's brilliant!

I guess this is also highly coupled to bevy's asset loader but would it be possible to support populating a mapping from relative paths to the handles instead of a simple Vec ?

This would likely involve searching the directory tree manually and then loading each asset individually (i.e. bypassing the load_folder, method), not sure if that's something you'd consider, but It would definitely be useful. One use case I can think of is storing prefabs under folders and linking their corresponding assets based on the path.

NiklasEi commented 2 years ago

I added a note in the readme about the recursive behaviour of loading a folder, thanks for pointing that out.

Keeping a path map for files in directories is also something I would see in the engine. This kind of behaviour also depends on your environment (e.g. usually non of this works in the browser) and should be baked into Bevy's AssetServer in my opinion. Maybe you want to file an issue on the engine repo for this? Once Bevy can do this, I would definitely add support here.

makspll commented 2 years ago

No worries,

Yeah that sounds very reasonable, I will go ahead and file an issue.

Thanks!

makspll commented 2 years ago

I see an issue for something like this already exists, I will link it here: https://github.com/bevyengine/bevy/issues/2147