Class: Asset

pc.Asset

An asset record of a file or data resource that can be loaded by the engine. The asset contains three 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)
resource: contains the final resource when it is loaded. (e.g. a pc.StandardMaterial or a pc.Texture)
See the pc.AssetRegistry for details on loading resources from assets.

Constructor

new Asset(name, type, file, dataopt)

Create a new Asset record. Generally, Assets are created in the loading process and you won't need to create them by hand.
Parameters:
Name Type Attributes Description
name String A non-unique but human-readable name which can be later used to retrieve the asset.
type String Type of asset. One of ["animation", "audio", "binary", "cubemap", "css", "font", "json", "html", "material", "model", "script", "shader", "text", "texture"]
file Object 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.
data Object <optional>
JSON object 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)
Properties:
Name Type Description
name String The name of the asset
id Number The asset id
type String The type of the asset. One of ["animation", "audio", "binary", "cubemap", "css", "font", "json", "html", "material", "model", "script", "shader", "text", "texture"]
tags pc.Tags Interface for tagging. Allows to find assets by tags using pc.AssetRegistry#findByTag method.
file Object The file details or null if no file
Properties
Name Type Attributes Description
url String <optional>
The URL of the resource file that contains the asset data
filename String <optional>
The filename of the resource file
size Number <optional>
The size of the resource file
hash String <optional>
The MD5 hash of the resource file data and the Asset data field
data Object 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)
resource Object A reference to the resource when the asset is loaded. e.g. a pc.Texture or a pc.Model
resources Array A reference to the resources of the asset when it's loaded. An asset can hold more runtime resources than one e.g. cubemaps
preload Boolean If true the asset will be loaded during the preload phase of application set up.
loaded Boolean True if the resource is loaded. e.g. if asset.resource is not null
registry pc.AssetRegistry The asset registry that this Asset belongs to
Source:
Examples
var file = {
  filename: "filename.txt",
  url: "/example/filename.txt",
}
var asset = new pc.Asset("a texture", "texture", {
    url: "http://example.com/my/assets/here/texture.png"
});

Methods

(private) addLocalizedAssetId(locale, assetId)

Adds a replacement asset id for the specified locale. When the locale in pc.Application#i18n changes then references to this asset will be replaced with the specified asset id. (Currently only supported by the pc.ElementComponent).
Parameters:
Name Type Description
locale String The locale e.g. ar-AR.
assetId Number The asset id
Source:

getFileUrl() → {String}

Return the URL required to fetch the file for this asset.
Source:
Returns:
The URL
Type
String
Example
var assets = app.assets.find("My Image", "texture");
var img = "&lt;img src='" + assets[0].getFileUrl() + "'&gt;";

(private) getLocalizedAssetId(locale) → {Number}

Returns the asset id of the asset that corresponds to the specified locale.
Parameters:
Name Type Description
locale String The desired locale e.g. ar-AR.
Source:
Returns:
An asset id or null if there is no asset specified for the desired locale.
Type
Number

ready(callback, scope)

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
Parameters:
Name Type Description
callback function The function called when the asset is ready. Passed the (asset) arguments
scope Object Scope object to use when calling the callback
Source:
Example
var asset = app.assets.find("My Asset");
asset.ready(function (asset) {
  // asset loaded
});
app.assets.load(asset);

(private) removeLocalizedAssetId(locale)

Removes a localized asset
Parameters:
Name Type Description
locale String The locale e.g. ar-AR.
Source:

unload()

Destroys the associated resource and marks asset as unloaded.
Source:
Example
var asset = app.assets.find("My Asset");
asset.unload();
// asset.resource is null

Events

add:localized

Fired when we add a new localized asset id to the asset.
Parameters:
Name Type Description
locale String The locale
assetId Number The asset id we added.
Source:

change

Fired when one of the asset properties `file`, `data`, `resource` or `resources` is changed
Parameters:
Name Type Description
asset pc.Asset The asset that was loaded
property String The name of the property that changed
value * The new property value
oldValue * The old property value
Source:

error

Fired if the asset encounters an error while loading
Parameters:
Name Type Description
err String The error message
asset pc.Asset The asset that generated the error
Source:

load

Fired when the asset has completed loading
Parameters:
Name Type Description
asset pc.Asset The asset that was loaded
Source:

remove

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

remove:localized

Fired when we remove a localized asset id from the asset.
Parameters:
Name Type Description
locale String The locale
assetId Number The asset id we removed.
Source: