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)

Parameters

appApplication

The application.

Methods

addHandler(type, handler)

Add a ResourceHandler for a resource type. Handler should support atleast load() and open(). Handlers can optionally support patch(asset, assets) to handle dependencies on other assets.

var 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 atleast 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

The registered handler.

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.

callbackcallbacks.ResourceLoader

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.