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
loader | ResourceLoader | The ResourceLoader used to load the asset files. |
Properties
Methods
add(asset)
Add an asset to the registry.
const asset = new pc.Asset("My Asset", "texture", {
url: "../path/to/image.jpg"
});
app.assets.add(asset);
Parameters
asset | Asset | The asset to add. |
filter(callback)
Return all Assets that satisfy a filter callback.
const assets = app.assets.filter(asset => asset.name.includes('monster'));
console.log(`Found ${assets.length} assets with a name containing 'monster'`);
Parameters
callback | FilterAssetCallback | The callback function that is used to filter assets.
Return |
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.
const 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
Asset, nullA 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.
const assets = app.assets.findAll('brick', 'texture');
console.log(`Found ${assets.length} texture assets named 'brick'`);
Parameters
name | string | The name of the Assets to find. |
type | string | 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.
const assets = app.assets.findByTag("level-1");
// returns all assets that tagged by `level-1`
const assets = app.assets.findByTag("level-1", "level-2");
// returns all assets that tagged by `level-1` OR `level-2`
const assets = app.assets.findByTag(["level-1", "monster"]);
// returns all assets that tagged by `level-1` AND `monster`
const 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.
const asset = app.assets.get(100);
Parameters
id | number | The id of the asset to get. |
Returns
Asset, undefinedThe asset.
getByUrl(url)
Retrieve an asset from the registry by its file's URL field.
const asset = app.assets.getByUrl("../path/to/image.jpg");
Parameters
url | string | The url of the asset to get. |
Returns
Asset, undefinedThe 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
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
const assetsToLoad = [
app.assets.find("My Asset"),
app.assets.find("Another Asset")
];
let count = 0;
assetsToLoad.forEach(function (assetToLoad) {
assetToLoad.ready(function (asset) {
count++;
if (count === assetsToLoad.length) {
// done
}
});
app.assets.load(assetToLoad);
});
Parameters
asset | Asset | 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) {
const texture = asset.resource;
});
Parameters
url | string | The url to load. |
type | string | The type of asset to load. |
callback | LoadAssetCallback | 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.
const file = magicallyObtainAFile();
app.assets.loadFromUrlAndFilename(URL.createObjectURL(file), "texture.png", "texture", function (err, asset) {
const texture = asset.resource;
});
Parameters
url | string | The url to load. |
filename | string | The filename of the asset to load. |
type | string | The type of asset to load. |
callback | LoadAssetCallback | 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.
const asset = app.assets.get(100);
app.assets.remove(asset);
Parameters
asset | 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.
const id = 123456;
app.assets.on("add:" + id, function (asset) {
console.log("Asset 123456 loaded");
});
Parameters
asset | Asset | The asset that was added. |
add:url:[url]
Fired when an asset is added to the registry.
Parameters
asset | Asset | The asset that was added. |
error:[id]
Fired when an error occurs during asset loading.
const id = 123456;
const asset = app.assets.get(id);
app.assets.on("error:" + id, function (err, asset) {
console.error(err);
});
app.assets.load(asset);
Parameters
asset | 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 | Asset | The asset that was added. |
error
Fired when an error occurs during asset loading.
const id = 123456;
const 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 | 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 | Asset | 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
asset | Asset | The asset that was removed. |
load:[id]
Fired when an asset completes loading.
const id = 123456;
const asset = app.assets.get(id);
app.assets.on("load:" + id, function (asset) {
console.log("asset loaded: " + asset.name);
});
app.assets.load(asset);
Parameters
asset | Asset | The asset that has just loaded. |
load:url:[url]
Fired when an asset completes loading.
const id = 123456;
const 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 | Asset | The asset that has just loaded. |
remove:[id]
Fired when an asset is removed from the registry.
const id = 123456;
app.assets.on("remove:" + id, function (asset) {
console.log("Asset removed: " + asset.name);
});
Parameters
asset | Asset | The asset that was removed. |
remove:url:[url]
Fired when an asset is removed from the registry.
Parameters
asset | 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 | string | 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
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.
const 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
name | string | Name of the event to unbind. |
callback | HandleEventCallback | Function to be unbound. |
scope | object | Scope that was used as the this when the event is fired. |
Returns
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 | HandleEventCallback | 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
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 | HandleEventCallback | 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
EventHandlerSelf for chaining.