API Reference

Class List

AssetRegistry

Extends: EventHandler

Container for all assets that are available to this application. Note that PlayCanvas scripts are provided with an AssetRegistry instance as app.assets.

Summary

Properties

prefix

A URL prefix that will be added to all asset loading requests.

Methods

add

Add an asset to the registry.

filter

Return all Assets that satisfy a filter callback.

find

Return the first Asset with the specified name and type found in the registry.

findAll

Return all Assets with the specified name and type found in the registry.

findByTag

Return all Assets that satisfy the search query.

get

Retrieve an asset from the registry by its id field.

getByUrl

Retrieve an asset from the registry by its file's URL field.

list

Create a filtered list of assets from the registry.

load

Load the asset's file from a remote source.

loadFromUrl

Use this to load and create an asset if you don't have assets created.

loadFromUrlAndFilename

Use this to load and create an asset when both the URL and filename are required.

remove

Remove an asset from the registry.

Events

add:[id]

Fired when an asset is added to the registry.

add:url:[url]

Fired when an asset is added to the registry.

error:[id]

Fired when an error occurs during asset loading.

add

Fired when an asset is added to the registry.

error

Fired when an error occurs during asset loading.

load

Fired when an asset completes loading.

remove

Fired when an asset is removed from the registry.

load:[id]

Fired when an asset completes loading.

load:url:[url]

Fired when an asset completes loading.

remove:[id]

Fired when an asset is removed from the registry.

remove:url:[url]

Fired when an asset is removed from the registry.

Inherited

Methods

fire

Fire an event, all additional arguments are passed on to the event listener.

hasEvent

Test if there are any handlers bound to an event name.

off

Detach an event handler from an event.

on

Attach an event handler to an event.

once

Attach an event handler to an event.

Details

Constructor

AssetRegistry(loader)

Create an instance of an AssetRegistry.

Parameters

loaderResourceLoader

The ResourceLoader used to load the asset files.

Properties

stringprefix

A URL prefix that will be added to all asset loading requests.

Methods

add(asset)

Add an asset to the registry.

var asset = new pc.Asset("My Asset", "texture", {
    url: "../path/to/image.jpg"
});
app.assets.add(asset);

Parameters

assetAsset

The asset to add.

filter(callback)

Return all Assets that satisfy a filter callback.

var assets = app.assets.filter(function (asset) {
    return asset.name.indexOf('monster') !== -1;
});
console.log("Found " + assets.length + " assets, where names contains 'monster'");

Parameters

callbackFilterAssetCallback

The callback function that is used to filter assets. Return true to include an asset in the returned array.

Returns

Asset[]

A list of all Assets found.

find(name, [type])

Return the first Asset with the specified name and type found in the registry.

var asset = app.assets.find("myTextureAsset", "texture");

Parameters

namestring

The name of the Asset to find.

typestring

The type of the Asset to find.

Returns

Asset, null

A single Asset or null if no Asset is found.

findAll(name, [type])

Return all Assets with the specified name and type found in the registry.

var assets = app.assets.findAll("myTextureAsset", "texture");
console.log("Found " + assets.length + " assets called " + name);

Parameters

namestring

The name of the Assets to find.

typestring

The type of the Assets to find.

Returns

Asset[]

A list of all Assets found.

findByTag(query)

Return all Assets that satisfy the search query. Query can be simply a string, or comma separated strings, to have inclusive results of assets that match at least one query. A query that consists of an array of tags can be used to match assets that have each tag of array.

var assets = app.assets.findByTag("level-1");
// returns all assets that tagged by `level-1`
var assets = app.assets.findByTag("level-1", "level-2");
// returns all assets that tagged by `level-1` OR `level-2`
var assets = app.assets.findByTag(["level-1", "monster"]);
// returns all assets that tagged by `level-1` AND `monster`
var assets = app.assets.findByTag(["level-1", "monster"], ["level-2", "monster"]);
// returns all assets that tagged by (`level-1` AND `monster`) OR (`level-2` AND `monster`)

Parameters

query*

Name of a tag or array of tags.

Returns

Asset[]

A list of all Assets matched query.

get(id)

Retrieve an asset from the registry by its id field.

var asset = app.assets.get(100);

Parameters

idnumber

The id of the asset to get.

Returns

Asset

The asset.

getByUrl(url)

Retrieve an asset from the registry by its file's URL field.

var asset = app.assets.getByUrl("../path/to/image.jpg");

Parameters

urlstring

The url of the asset to get.

Returns

Asset

The asset.

list(filters)

Create a filtered list of assets from the registry.

Parameters

filtersobject

Properties to filter on, currently supports: 'preload: true|false'.

Returns

Asset[]

The filtered list of assets.

load(asset)

Load the asset's file from a remote source. Listen for "load" events on the asset to find out when it is loaded.

// load some assets
var assetsToLoad = [
    app.assets.find("My Asset"),
    app.assets.find("Another Asset")
];
var count = 0;
assetsToLoad.forEach(function (assetToLoad) {
    assetToLoad.ready(function (asset) {
        count++;
        if (count === assetsToLoad.length) {
            // done
        }
    });
    app.assets.load(assetToLoad);
});

Parameters

assetAsset

The asset to load.

loadFromUrl(url, type, callback)

Use this to load and create an asset if you don't have assets created. Usually you would only use this if you are not integrated with the PlayCanvas Editor.

app.assets.loadFromUrl("../path/to/texture.jpg", "texture", function (err, asset) {
    var texture = asset.resource;
});

Parameters

urlstring

The url to load.

typestring

The type of asset to load.

callbackLoadAssetCallback

Function called when asset is loaded, passed (err, asset), where err is null if no errors were encountered.

loadFromUrlAndFilename(url, filename, type, callback)

Use this to load and create an asset when both the URL and filename are required. For example, use this function when loading BLOB assets, where the URL does not adequately identify the file.

var file = magicallyAttainAFile();
app.assets.loadFromUrlAndFilename(URL.createObjectURL(file), "texture.png", "texture", function (err, asset) {
    var texture = asset.resource;
});

Parameters

urlstring

The url to load.

filenamestring

The filename of the asset to load.

typestring

The type of asset to load.

callbackLoadAssetCallback

Function called when asset is loaded, passed (err, asset), where err is null if no errors were encountered.

remove(asset)

Remove an asset from the registry.

var asset = app.assets.get(100);
app.assets.remove(asset);

Parameters

assetAsset

The asset to remove.

Returns

boolean

True if the asset was successfully removed and false otherwise.

Events

add:[id]

Fired when an asset is added to the registry.

var id = 123456;
app.assets.on("add:" + id, function (asset) {
    console.log("Asset 123456 loaded");
});

Parameters

assetAsset

The asset that was added.

add:url:[url]

Fired when an asset is added to the registry.

Parameters

assetAsset

The asset that was added.

error:[id]

Fired when an error occurs during asset loading.

var id = 123456;
var asset = app.assets.get(id);
app.assets.on("error:" + id, function (err, asset) {
    console.error(err);
});
app.assets.load(asset);

Parameters

assetAsset

The asset that generated the error.

add

Fired when an asset is added to the registry.

app.assets.on("add", function (asset) {
    console.log("New asset added: " + asset.name);
});

Parameters

assetAsset

The asset that was added.

error

Fired when an error occurs during asset loading.

var id = 123456;
var asset = app.assets.get(id);
app.assets.on("error", function (err, asset) {
    console.error(err);
});
app.assets.load(asset);

Parameters

errstring

The error message.

assetAsset

The asset that generated the error.

load

Fired when an asset completes loading.

app.assets.on("load", function (asset) {
    console.log("asset loaded: " + asset.name);
});

Parameters

assetAsset

The asset that has just loaded.

remove

Fired when an asset is removed from the registry.

app.assets.on("remove", function (asset) {
    console.log("Asset removed: " + asset.name);
});

Parameters

assetAsset

The asset that was removed.

load:[id]

Fired when an asset completes loading.

var id = 123456;
var asset = app.assets.get(id);
app.assets.on("load:" + id, function (asset) {
    console.log("asset loaded: " + asset.name);
});
app.assets.load(asset);

Parameters

assetAsset

The asset that has just loaded.

load:url:[url]

Fired when an asset completes loading.

var id = 123456;
var asset = app.assets.get(id);
app.assets.on("load:url:" + asset.file.url, function (asset) {
    console.log("asset loaded: " + asset.name);
});
app.assets.load(asset);

Parameters

assetAsset

The asset that has just loaded.

remove:[id]

Fired when an asset is removed from the registry.

var id = 123456;
app.assets.on("remove:" + id, function (asset) {
    console.log("Asset removed: " + asset.name);
});

Parameters

assetAsset

The asset that was removed.

remove:url:[url]

Fired when an asset is removed from the registry.

Parameters

assetAsset

The asset that was removed.

Inherited

Methods

fire(name, [arg1], [arg2], [arg3], [arg4], [arg5], [arg6], [arg7], [arg8])

Fire an event, all additional arguments are passed on to the event listener.

obj.fire('test', 'This is the message');

Parameters

namestring

Name of event to fire.

arg1*

First argument that is passed to the event handler.

arg2*

Second argument that is passed to the event handler.

arg3*

Third argument that is passed to the event handler.

arg4*

Fourth argument that is passed to the event handler.

arg5*

Fifth argument that is passed to the event handler.

arg6*

Sixth argument that is passed to the event handler.

arg7*

Seventh argument that is passed to the event handler.

arg8*

Eighth argument that is passed to the event handler.

Returns

EventHandler

Self for chaining.

hasEvent(name)

Test if there are any handlers bound to an event name.

obj.on('test', function () { }); // bind an event to 'test'
obj.hasEvent('test'); // returns true
obj.hasEvent('hello'); // returns false

Parameters

namestring

The name of the event to test.

Returns

boolean

True if the object has handlers bound to the specified event name.

off([name], [callback], [scope])

Detach an event handler from an event. If callback is not provided then all callbacks are unbound from the event, if scope is not provided then all events with the callback will be unbound.

var handler = function () {
};
obj.on('test', handler);

obj.off(); // Removes all events
obj.off('test'); // Removes all events called 'test'
obj.off('test', handler); // Removes all handler functions, called 'test'
obj.off('test', handler, this); // Removes all handler functions, called 'test' with scope this

Parameters

namestring

Name of the event to unbind.

callbackHandleEventCallback

Function to be unbound.

scopeobject

Scope that was used as the this when the event is fired.

Returns

EventHandler

Self for chaining.

on(name, callback, [scope])

Attach an event handler to an event.

obj.on('test', function (a, b) {
    console.log(a + b);
});
obj.fire('test', 1, 2); // prints 3 to the console

Parameters

namestring

Name of the event to bind the callback to.

callbackHandleEventCallback

Function that is called when event is fired. Note the callback is limited to 8 arguments.

scopeobject

Object to use as 'this' when the event is fired, defaults to current this.

Returns

EventHandler

Self for chaining.

once(name, callback, [scope])

Attach an event handler to an event. This handler will be removed after being fired once.

obj.once('test', function (a, b) {
    console.log(a + b);
});
obj.fire('test', 1, 2); // prints 3 to the console
obj.fire('test', 1, 2); // not going to get handled

Parameters

namestring

Name of the event to bind the callback to.

callbackHandleEventCallback

Function that is called when event is fired. Note the callback is limited to 8 arguments.

scopeobject

Object to use as 'this' when the event is fired, defaults to current this.

Returns

EventHandler

Self for chaining.