Class: AssetRegistry

pc.AssetRegistry

Container for all assets that are available to this application

Constructor

new AssetRegistry(loader)

Create an instance of an AssetRegistry. Note: PlayCanvas scripts are provided with an AssetRegistry instance as 'app.assets'.
Parameters:
Name Type Description
loader pc.ResourceLoader The ResourceLoader used to load the asset files.
Properties:
Name Type Description
prefix String A URL prefix that will be added to all asset loading requests.
Source:

Methods

add(asset)

Add an asset to the registry
Parameters:
Name Type Description
asset pc.Asset The asset to add
Source:
Example
var asset = new pc.Asset("My Asset", "texture", {url: "../path/to/image.jpg"});
app.assets.add(asset);

filter(callback) → {Array.<pc.Asset>}

Return all Assets that satisfy filter callback
Parameters:
Name Type Description
callback function The callback function that is used to filter assets, return `true` to include asset to result list
Source:
Returns:
A list of all Assets found
Type
Array.<pc.Asset>
Example
var assets = app.assets.filter(function(asset) {
    return asset.name.indexOf('monster') !== -1;
});
console.log("Found " + assets.length + " assets, where names contains 'monster'");

find(name, typeopt) → {pc.Asset}

Return the first Asset with the specified name and type found in the registry
Parameters:
Name Type Attributes Description
name String The name of the Asset to find
type String <optional>
The type of the Asset to find
Source:
Returns:
A single Asset or null if no Asset is found
Type
pc.Asset
Example
var asset = app.assets.find("myTextureAsset", "texture");

findAll(name, typeopt) → {Array.<pc.Asset>}

Return all Assets with the specified name and type found in the registry
Parameters:
Name Type Attributes Description
name String The name of the Assets to find
type String <optional>
The type of the Assets to find
Source:
Returns:
A list of all Assets found
Type
Array.<pc.Asset>
Example
var assets = app.assets.findAll("myTextureAsset", "texture");
console.log("Found " + assets.length + " assets called " + name);

findByTag(tag) → {Array.<pc.Asset>}

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
Parameters:
Name Type Description
tag String Name of a tag or array of tags
Source:
Returns:
A list of all Assets matched query
Type
Array.<pc.Asset>
Examples
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`)

get(id) → {pc.Asset}

Retrieve an asset from the registry by its id field
Parameters:
Name Type Description
id Number the id of the asset to get
Source:
Returns:
The asset
Type
pc.Asset
Example
var asset = app.assets.get(100);

getByUrl(url) → {pc.Asset}

Retrieve an asset from the registry by it's file's URL field
Parameters:
Name Type Description
url String The url of the asset to get
Source:
Returns:
The asset
Type
pc.Asset
Example
var asset = app.assets.getByUrl("../path/to/image.jpg");

list(filters) → {Array.<pc.Asset>}

Create a filtered list of assets from the registry
Parameters:
Name Type Description
filters Object Properties to filter on, currently supports: 'preload: true|false'
Source:
Returns:
The filtered list of assets.
Type
Array.<pc.Asset>

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
Parameters:
Name Type Description
asset pc.Asset The asset to load
Source:
Example
// 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)
}

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
Parameters:
Name Type Description
url String The url to load
type String The type of asset to load
callback function Function called when asset is loaded, passed (err, asset), where err is null if no errors were encountered
Source:
Example
app.assets.loadFromUrl("../path/to/texture.jpg", "texture", function (err, asset) {
    var texture = asset.resource;
});

remove(asset) → {Boolean}

Remove an asset from the registry
Parameters:
Name Type Description
asset pc.Asset The asset to remove
Source:
Returns:
True if the asset was successfully removed and false otherwise
Type
Boolean
Example
var asset = app.assets.get(100);
app.assets.remove(asset);

Events

add:[id]

Fired when an asset is added to the registry
Parameters:
Name Type Description
asset pc.Asset The asset that was added
Source:
Example
var id = 123456;
app.assets.on("add:" + id, function (asset) {
    console.log("Asset 123456 loaded");
});

add:url:[url]

Fired when an asset is added to the registry
Parameters:
Name Type Description
asset pc.Asset The asset that was added
Source:

error:[id]

Fired when an error occurs during asset loading
Parameters:
Name Type Description
asset pc.Asset The asset that generated the error
Source:
Example
var id = 123456;
var asset = app.assets.get(id);
app.assets.on("error:" + id, function (err, asset) {
    console.error(err);
});
app.assets.load(asset);

add

Fired when an asset is added to the registry
Parameters:
Name Type Description
asset pc.Asset The asset that was added
Source:
Example
app.assets.on("add", function (asset) {
    console.log("New asset added: " + asset.name);
});

error

Fired when an error occurs during asset loading
Parameters:
Name Type Description
err String The error message
asset pc.Asset The asset that generated the error
Source:
Example
var id = 123456;
var asset = app.assets.get(id);
app.assets.on("error", function (err, asset) {
    console.error(err);
});
app.assets.load(asset);

load

Fired when an asset completes loading
Parameters:
Name Type Description
asset pc.Asset The asset that has just loaded
Source:
Example
app.assets.on("load", function (asset) {
    console.log("asset loaded: " + asset.name);
});

remove

Fired when an asset is removed from the registry
Parameters:
Name Type Description
asset pc.Asset The asset that was removed
Source:
Example
app.assets.on("remove", function (aseet) {
    console.log("Asset removed: " + asset.name);
});

load:[id]

Fired when an asset completes loading
Parameters:
Name Type Description
asset pc.Asset The asset that has just loaded
Source:
Example
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);

load:url:[url]

Fired when an asset completes loading
Parameters:
Name Type Description
asset pc.Asset The asset that has just loaded
Source:
Example
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);

remove:[id]

Fired when an asset is removed from the registry
Parameters:
Name Type Description
asset pc.Asset The asset that was removed
Source:
Example
var id = 123456;
app.assets.on("remove:" + id, function (asset) {
    console.log("Asset removed: " + asset.name);
});

remove:url:[url]

Fired when an asset is removed from the registry
Parameters:
Name Type Description
asset pc.Asset The asset that was removed
Source: