API Reference

Class List

pc.AssetRegistry

Container for all assets that are available to this application

Summary

Properties

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

Methods

addAdd an asset to the registry
filterReturn all Assets that satisfy filter callback
findReturn the first Asset with the specified name and type found in the registry
findAllReturn all Assets with the specified name and type found in the registry
findByTagReturn all Assets that satisfy the search query.
getRetrieve an asset from the registry by its id field
getByUrlRetrieve an asset from the registry by it's file's URL field
listCreate a filtered list of assets from the registry
loadLoad the asset's file from a remote source.
loadFromUrlUse this to load and create an asset if you don't have assets created.
removeRemove 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
addFired when an asset is added to the registry
errorFired when an error occurs during asset loading
loadFired when an asset completes loading
removeFired 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

Details

Constructor

AssetRegistry(loader)

Create an instance of an AssetRegistry. Note: PlayCanvas scripts are provided with an AssetRegistry instance as 'app.assets'.

Parameters

loaderpc.ResourceLoaderThe ResourceLoader used to 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

assetpc.AssetThe asset to add

filter(callback)

Return all Assets that satisfy 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

callbackfunctionThe callback function that is used to filter assets, return `true` to include asset to result list

Returns

pc.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

nameStringThe name of the Asset to find
typeStringThe type of the Asset to find

Returns

pc.Asset 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

nameStringThe name of the Assets to find
typeStringThe type of the Assets to find

Returns

pc.Asset[] A list of all Assets found

findByTag(tag)

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

tagStringName of a tag or array of tags

Returns

pc.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

idNumberthe id of the asset to get

Returns

pc.Asset The asset

getByUrl(url)

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

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

Parameters

urlStringThe url of the asset to get

Returns

pc.Asset The asset

list(filters)

Create a filtered list of assets from the registry

Parameters

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

Returns

pc.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 toload = [app.assets.find("My Asset"), app.assets.find("Another Asset")]
var count = 0;
for (var i = 0; i < toload.length; i++) {
    var asset = toload[i];
    asset.ready(function (asset) {
        count++;
        if (count === toload.length) {
            // done
        }
    });
    app.assets.load(asset)
}

Parameters

assetpc.AssetThe 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

urlStringThe url to load
typeStringThe type of asset to load
callbackfunctionFunction 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

assetpc.AssetThe asset to remove

Events

add:[id]

Fired when an asset is added to the registry

Parameters

assetpc.AssetThe asset that was added

add:url:[url]

Fired when an asset is added to the registry

Parameters

assetpc.AssetThe asset that was added

error:[id]

Fired when an error occurs during asset loading

Parameters

assetpc.AssetThe asset that generated the error

add

Fired when an asset is added to the registry

Parameters

assetpc.AssetThe asset that was added

error

Fired when an error occurs during asset loading

Parameters

errStringThe error message
assetpc.AssetThe asset that generated the error

load

Fired when an asset completes loading

Parameters

assetpc.AssetThe asset that has just loaded

remove

Fired when an asset is removed from the registry

Parameters

assetpc.AssetThe asset that was removed

load:[id]

Fired when an asset completes loading

Parameters

assetpc.AssetThe asset that has just loaded

load:url:[url]

Fired when an asset completes loading

Parameters

assetpc.AssetThe asset that has just loaded

remove:[id]

Fired when an asset is removed from the registry

Parameters

assetpc.AssetThe asset that was removed

remove:url:[url]

Fired when an asset is removed from the registry

Parameters

assetpc.AssetThe asset that was removed