Skip to content

Commit aa5429d

Browse files
authored
Add custom file source examples. #182
Based on a question from Discord
1 parent 67bc57c commit aa5429d

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

assets-async/README.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -925,6 +925,54 @@ official APIs. In particular, `Texture.setAssetManager` and `Cubemap.setAssetMan
925925
If you heavily rely on these unsupported APIs or custom asset loaders, you might need to use `AsyncAssetManager`
926926
instead.
927927

928+
##### Loading assets from multiple sources
929+
930+
`AssetStorage` constructor takes a `FileHandleResolver` that defaults to the `InternalFileHandleResolver`.
931+
The resolver is passed to the default libGDX `AssetLoader` instances that handle the actual file loading.
932+
Whenever an asset is scheduled for loading using just its path or extra resources have to be loaded
933+
by an `AssetLoader` internally, the resolver is used to convert a string path into a `FileHandle`.
934+
935+
To load files from a different source:
936+
937+
* If all assets are meant to be loaded from a different source than the internal asset folder,
938+
pass a different `FileHandleResolver` implementation to the `AssetStorage` constructor.
939+
940+
```kotlin
941+
val assetStorage = AssetStorage(fileResolver = LocalFileHandleResolver())
942+
```
943+
944+
* If only specific asset types are meant to be loaded from a different source, loaders can be overridden or
945+
defined individually.
946+
947+
```kotlin
948+
assetStorage.setLoader { MusicLoader(LocalFileHandleResolver()) }
949+
```
950+
951+
* If some but not all assets of a specific type loaded are to be loaded from a different source, `AssetDescriptor`
952+
with an overridden `FileHandle` can be used to customize file loading on a case by case basis.
953+
954+
```kotlin
955+
val path = "local.png"
956+
val assetDescriptor = assetStorage.getAssetDescriptor<Texture>(path, fileHandle = Gdx.files.local(path))
957+
assetStorage.loadAsync(assetDescriptor)
958+
```
959+
960+
The caveat is that if the underlying libGDX `AssetLoader` loads any extra files or otherwise uses the file resolver
961+
even if given a `FileHandle`, it might not work correctly. This is especially true for assets that consists of multiple
962+
files like `Skin` in which case the dependencies might have to be scheduled manually with a correct `FileHandle` before
963+
loading the actual asset. Please review the libGDX `AssetLoader` implementations associated with the loaded files before
964+
commiting to the customized asset descriptors.
965+
966+
* If some assets are to be loaded from multiple sources with possible path clashes, it is recommended to maintain
967+
separate `AssetStorage` instances with different `FileHandleResolver` implementations. Note that the `asyncContext`
968+
necessary for the asynchronous operations can be safely reused between multiple storages.
969+
970+
```kotlin
971+
val asyncContext = newAsyncContext(threads = 2, threadName = "AssetStorage")
972+
val internalAssetStorage = AssetStorage(asyncContext, InternalFileHandleResolver())
973+
val localAssetStorage = AssetStorage(asyncContext, LocalFileHandleResolver())
974+
```
975+
928976
#### Synergy
929977

930978
While [`ktx-assets`](../assets) module does provide some extensions to the `AssetManager`, which is a direct

0 commit comments

Comments
 (0)