Create a new Asset record. Generally, Assets are created in the loading process and you won't need to create them by hand.
A non-unique but human-readable name which can be later used to retrieve the asset.
Type of asset. One of ["animation", "audio", "binary", "bundle", "container", "cubemap", "css", "font", "json", "html", "material", "model", "script", "shader", "sprite", "template", text", "texture", "textureatlas"]
Optional
file: { Details about the file the asset is made from. At the least must contain the 'url' field. For assets that don't contain file data use null.
Optional file contents. This is faster than wrapping the data in a (base64 encoded) blob. Currently only used by container assets.
The filename of the resource file or null if no filename was set (e.g from using AssetRegistry#loadFromUrl).
The MD5 hash of the resource file data and the Asset data field or null if hash was set (e.g from using AssetRegistry#loadFromUrl).
The size of the resource file or null if no size was set (e.g. from using AssetRegistry#loadFromUrl).
The URL of the resource file that contains the asset data.
Optional
data: string | objectJSON object or string with additional data about the asset. (e.g. for texture and model assets) or contains the asset data itself (e.g. in the case of materials).
Optional
options: { The asset handler options. For container options see ContainerHandler.
For use with texture assets that are loaded using the browser. This setting overrides the default crossOrigin specifier. For more details on crossOrigin and its use, see https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/crossOrigin.
const asset = new pc.Asset("a texture", "texture", {
url: "http://example.com/my/assets/here/texture.png"
});
True if the asset has finished attempting to load the resource. It is not guaranteed that the resources are available as there could have been a network error.
True if the resource is currently being loaded.
Optional JSON data that contains the asset handler options.
The asset registry that this Asset belongs to.
Asset tags. Enables finding of assets by tags using the AssetRegistry#findByTag method.
The type of the asset. One of ["animation", "audio", "binary", "container", "cubemap", "css", "font", "json", "html", "material", "model", "render", "script", "shader", "sprite", "template", "text", "texture", "textureatlas"]
Optional JSON data that contains either the complete resource data. (e.g. in the case of a material) or additional data (e.g. in the case of a model it contains mappings from mesh to material).
The file details or null if no file.
The asset id.
The asset name.
If true the asset will be loaded during the preload phase of application set up.
A reference to the resources of the asset when it's loaded. An asset can hold more runtime resources than one e.g. cubemaps.
Fire an event, all additional arguments are passed on to the event listener.
Name of event to fire.
Optional
arg1: anyFirst argument that is passed to the event handler.
Optional
arg2: anySecond argument that is passed to the event handler.
Optional
arg3: anyThird argument that is passed to the event handler.
Optional
arg4: anyFourth argument that is passed to the event handler.
Optional
arg5: anyFifth argument that is passed to the event handler.
Optional
arg6: anySixth argument that is passed to the event handler.
Optional
arg7: anySeventh argument that is passed to the event handler.
Optional
arg8: anyEighth argument that is passed to the event handler.
Self for chaining.
obj.fire('test', 'This is the message');
Test if there are any handlers bound to an event name.
The name of the event to test.
True if the object has handlers bound to the specified event name.
obj.on('test', function () { }); // bind an event to 'test'
obj.hasEvent('test'); // returns true
obj.hasEvent('hello'); // returns false
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.
Optional
name: stringName of the event to unbind.
Optional
callback: HandleEventCallbackFunction to be unbound.
Optional
scope: objectScope that was used as the this when the event is fired.
Self for chaining.
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
Attach an event handler to an event.
Name of the event to bind the callback to.
Function that is called when event is fired. Note the callback is limited to 8 arguments.
Optional
scope: object = ...Object to use as 'this' when the event is fired, defaults to current this.
Can be used for removing event in the future.
obj.on('test', function (a, b) {
console.log(a + b);
});
obj.fire('test', 1, 2); // prints 3 to the console
const evt = obj.on('test', function (a, b) {
console.log(a + b);
});
// some time later
evt.off();
Attach an event handler to an event. This handler will be removed after being fired once.
Name of the event to bind the callback to.
Function that is called when event is fired. Note the callback is limited to 8 arguments.
Optional
scope: object = ...Object to use as 'this' when the event is fired, defaults to current this.
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
Take a callback which is called as soon as the asset is loaded. If the asset is already loaded the callback is called straight away.
The function called when the asset is ready. Passed the (asset) arguments.
Optional
scope: objectScope object to use when calling the callback.
const asset = app.assets.find("My Asset");
asset.ready(function (asset) {
// asset loaded
});
app.assets.load(asset);
Static
EVENT_Fired when we add a new localized asset id to the asset.
asset.on('add:localized', (locale, assetId) => {
console.log(`Asset ${asset.name} has added localized asset ${assetId} for locale ${locale}`);
});
Static
EVENT_Fired when one of the asset properties file
, data
, resource
or resources
is changed.
asset.on('change', (asset, property, newValue, oldValue) => {
console.log(`Asset ${asset.name} has property ${property} changed from ${oldValue} to ${newValue}`);
});
Static
EVENT_Fired if the asset encounters an error while loading.
asset.on('error', (err, asset) => {
console.error(`Error loading asset ${asset.name}: ${err}`);
});
Static
EVENT_Fired when the asset has completed loading.
asset.on('load', (asset) => {
console.log(`Asset loaded: ${asset.name}`);
});
Static
EVENT_Fired when the asset is removed from the asset registry.
asset.on('remove', (asset) => {
console.log(`Asset removed: ${asset.name}`);
});
Static
EVENT_Fired when we remove a localized asset id from the asset.
asset.on('remove:localized', (locale, assetId) => {
console.log(`Asset ${asset.name} has removed localized asset ${assetId} for locale ${locale}`);
});
Static
EVENT_Fired just before the asset unloads the resource. This allows for the opportunity to prepare for an asset that will be unloaded. E.g. Changing the texture of a model to a default before the one it was using is unloaded.
asset.on('unload', (asset) => {
console.log(`Asset about to unload: ${asset.name}`);
});
An asset record of a file or data resource that can be loaded by the engine. The asset contains four important fields:
file
: contains the details of a file (filename, url) which contains the resource data, e.g. an image file for a texture asset.data
: contains a JSON blob which contains either the resource data for the asset (e.g. material data) or additional data for the file (e.g. material mappings for a model).options
: contains a JSON blob with handler-specific load options.resource
: contains the final resource when it is loaded. (e.g. a StandardMaterial or a Texture).See the AssetRegistry for details on loading resources from assets.