pc.AssetRegistry
Extends: pc.EventHandler
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. |
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. Note: PlayCanvas scripts are provided with an AssetRegistry instance as 'app.assets'.
Parameters
| loader | pc.ResourceLoader | The ResourceLoader used to load the asset files. |
Properties
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
| asset | pc.Asset | The 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
| callback | pc.callbacks.FilterAsset | The callback function that is used to filter assets, return |
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.AssetA 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(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
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.AssetThe 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.AssetThe 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 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
| asset | pc.Asset | The asset to load. |
loadFromUrl(url, type, callback, [filename])
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 | pc.callbacks.LoadAsset | Function called when asset is loaded, passed (err, asset), where err is null if no errors were encountered. |
| filename | string | Optional asset filename. |
remove(asset)
Remove an asset from the registry.
var asset = app.assets.get(100);
app.assets.remove(asset);
Parameters
| asset | pc.Asset | The asset to remove. |
Returns
booleanTrue 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
| asset | pc.Asset | The asset that was added. |
add:url:[url]
Fired when an asset is added to the registry.
Parameters
| asset | pc.Asset | 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
| 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
| asset | pc.Asset | 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
| 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
| asset | pc.Asset | 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
| 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
| asset | pc.Asset | The asset that was removed. |
remove:url:[url]
Fired when an asset is removed from the registry.
Parameters
| asset | pc.Asset | 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
| name | object | 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
pc.EventHandlerSelf 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
| name | string | The name of the event to test. |
Returns
booleanTrue 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 hander functions, called 'test' with scope this
Parameters
| name | string | Name of the event to unbind. |
| callback | pc.callbacks.HandleEvent | Function to be unbound. |
| scope | object | Scope that was used as the this when the event is fired. |
Returns
pc.EventHandlerSelf 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
| name | string | Name of the event to bind the callback to. |
| callback | pc.callbacks.HandleEvent | Function that is called when event is fired. Note the callback is limited to 8 arguments. |
| scope | object | Object to use as 'this' when the event is fired, defaults to current this. |
Returns
pc.EventHandlerSelf 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
| name | string | Name of the event to bind the callback to. |
| callback | pc.callbacks.HandleEvent | Function that is called when event is fired. Note the callback is limited to 8 arguments. |
| scope | object | Object to use as 'this' when the event is fired, defaults to current this. |
Returns
pc.EventHandlerSelf for chaining.