pc.AssetRegistry
Container for all assets that are available to this application
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 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 it's 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. |
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 |
Details
Constructor
AssetRegistry(loader)
Create an instance of an AssetRegistry.
Note: PlayCanvas scripts are provided with an AssetRegistry instance as 'app.assets'.
Parameters
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
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
callback | function | The 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
name | String | The name of the Asset to find |
type | String | The 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
name | String | The name of the Assets to find |
type | String | The 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
tag | String | Name 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
id | Number | the 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
url | String | The url of the asset to get |
Returns
pc.Asset The asset
list(filters)
Create a filtered list of assets from the registry
Parameters
filters | Object | Properties 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
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
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 |
remove(asset)
Remove an asset from the registry
var asset = app.assets.get(100);
app.assets.remove(asset);
Parameters
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
add:url:[url]
Fired when an asset is added to the registry
Parameters
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
asset | pc.Asset | 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
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
err | String | The error message |
asset | pc.Asset | 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
asset | pc.Asset | The asset that has just loaded |
remove
Fired when an asset is removed from the registry
app.assets.on("remove", function (aseet) {
console.log("Asset removed: " + asset.name);
});
Parameters
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
asset | pc.Asset | 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
asset | pc.Asset | 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
remove:url:[url]
Fired when an asset is removed from the registry
Parameters