ResourceLoader
Load resource data, potentially from remote sources. Caches resource on load to prevent multiple requests. Add ResourceHandlers to handle different types of resources.
Summary
Methods
addHandler | Add a ResourceHandler for a resource type. |
clearCache | Remove resource from cache. |
destroy | Destroys the resource loader. |
getFromCache | Check cache for resource from a URL. |
getHandler | Get a ResourceHandler for a resource type. |
load | Make a request for a resource from a remote URL. |
open | Convert raw resource data into a resource instance. |
patch | Perform any operations on a resource, that requires a dependency on its asset data or any other asset data. |
removeHandler | Remove a ResourceHandler for a resource type. |
Details
Constructor
Methods
addHandler(type, handler)
Add a ResourceHandler for a resource type. Handler should support at least load()
and open()
. Handlers can optionally support patch(asset, assets) to handle dependencies on
other assets.
const loader = new ResourceLoader();
loader.addHandler("json", new pc.JsonHandler());
Parameters
type | string | The name of the resource type that the handler will be registered with. Can be: |
handler | ResourceHandler | An instance of a resource handler
supporting at least |
clearCache(url, type)
Remove resource from cache.
Parameters
url | string | The URL of the resource. |
type | string | The type of resource. |
destroy()
Destroys the resource loader.
getFromCache(url, type)
Check cache for resource from a URL. If present, return the cached value.
Parameters
url | string | The URL of the resource to get from the cache. |
type | string | The type of the resource. |
Returns
*The resource loaded from the cache.
getHandler(type)
Get a ResourceHandler for a resource type.
Parameters
type | string | The name of the resource type that the handler is registered with. |
Returns
ResourceHandler, undefinedThe registered handler, or undefined if the requested handler is not registered.
load(url, type, callback, [asset])
Make a request for a resource from a remote URL. Parse the returned data using the handler for the specified type. When loaded and parsed, use the callback to return an instance of the resource.
app.loader.load("../path/to/texture.png", "texture", function (err, texture) {
// use texture here
});
Parameters
url | string | The URL of the resource to load. |
type | string | The type of resource expected. |
callback | ResourceLoaderCallback | The callback used when the resource is loaded or an error occurs. Passed (err, resource) where err is null if there are no errors. |
asset | Asset | Optional asset that is passed into handler. |
open(type, data)
Convert raw resource data into a resource instance. E.g. Take 3D model format JSON and return a Model.
Parameters
type | string | The type of resource. |
data | * | The raw resource data. |
Returns
*The parsed resource data.
patch(asset, assets)
Perform any operations on a resource, that requires a dependency on its asset data or any other asset data.
Parameters
asset | Asset | The asset to patch. |
assets | AssetRegistry | The asset registry. |
removeHandler(type)
Remove a ResourceHandler for a resource type.
Parameters
type | string | The name of the type that the handler will be removed. |