API Reference

Class List

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

ResourceLoader(app)

Create a new ResourceLoader instance.

Parameters

appAppBase

The application.

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

typestring

The name of the resource type that the handler will be registered with. Can be:

handlerResourceHandler

An instance of a resource handler supporting at least load() and open().

clearCache(url, type)

Remove resource from cache.

Parameters

urlstring

The URL of the resource.

typestring

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

urlstring

The URL of the resource to get from the cache.

typestring

The type of the resource.

Returns

*

The resource loaded from the cache.

getHandler(type)

Get a ResourceHandler for a resource type.

Parameters

typestring

The name of the resource type that the handler is registered with.

Returns

ResourceHandler, undefined

The 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

urlstring

The URL of the resource to load.

typestring

The type of resource expected.

callbackResourceLoaderCallback

The callback used when the resource is loaded or an error occurs. Passed (err, resource) where err is null if there are no errors.

assetAsset

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

typestring

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

assetAsset

The asset to patch.

assetsAssetRegistry

The asset registry.

removeHandler(type)

Remove a ResourceHandler for a resource type.

Parameters

typestring

The name of the type that the handler will be removed.