
Assets manager class. Used to create, load and cache game assets, which includes textures, audio files, JSON objects, etc. As a rule of thumb, all methods to load or create assets are async and return a promise.
To access the Assets manager you use Shaku.assets.
Kind: global class
Array.<string>Array.<string>PromiseAssetPromise.<SoundAsset>Promise.<TextureAsset>Promise.<TextureAsset>Promise.<TextureAtlas>Promise.<FontTextureAsset>Promise.<MsdfFontTextureAsset>Promise.<JsonAsset>Promise.<JsonAsset>Promise.<BinaryAsset>Promise.<BinaryAsset>Create the manager.
Optional URL root to prepend to all loaded assets URLs. For example, if all your assets are under ‘/static/assets/’, you can set this url as root and omit it when loading assets later.
Kind: instance property of Assets
Optional suffix to add to all loaded assets URLs. You can use this for anti-cache mechanism if you want to reload all assets. For example, you can set this value to “‘?dt=’ + Date.now()”.
Kind: instance property of Assets
Array.<string>Get list of assets waiting to be loaded. This list will be reset if you call clearCache().
Kind: instance property of Assets
Returns: Array.<string> - URLs of assets waiting to be loaded.
Array.<string>Get list of assets that failed to load. This list will be reset if you call clearCache().
Kind: instance property of Assets
Returns: Array.<string> - URLs of assets that had error loading.
PromiseReturn a promise that will be resolved only when all pending assets are loaded. If an asset fails, will reject.
Kind: instance method of Assets
Returns: Promise - Promise to resolve when all assets are loaded, or reject if there are failed assets.
Example
await Shaku.assets.waitForAll();
console.log("All assets are loaded!");
AssetGet asset directly from cache, synchronous and without a Promise.
Kind: instance method of Assets
Returns: Asset - Asset or null if not loaded.
| Param | Type | Description |
|---|---|---|
| url | String |
Asset URL or name. |
Promise.<SoundAsset>Load a sound asset. If already loaded, will use cache.
Kind: instance method of Assets
Returns: Promise.<SoundAsset> - promise to resolve with asset instance, when loaded. You can access the loading asset with .asset on the promise.
| Param | Type | Description |
|---|---|---|
| url | String |
Asset URL. |
Example
let sound = await Shaku.assets.loadSound("assets/my_sound.ogg");
Promise.<TextureAsset>Load a texture asset. If already loaded, will use cache.
Kind: instance method of Assets
Returns: Promise.<TextureAsset> - promise to resolve with asset instance, when loaded. You can access the loading asset with .asset on the promise.
| Param | Type | Description |
|---|---|---|
| url | String |
Asset URL. |
| [params] | \* |
Optional params dictionary. See TextureAsset.load() for more details. |
Example
let texture = await Shaku.assets.loadTexture("assets/my_texture.png", {generateMipMaps: false});
Promise.<TextureAsset>Create a render target texture asset. If already loaded, will use cache.
Kind: instance method of Assets
Returns: Promise.<TextureAsset> - promise to resolve with asset instance, when loaded. You can access the loading asset with .asset on the promise.
| Param | Type | Description |
|---|---|---|
| name | String | null |
Asset name (matched to URLs when using cache). If null, will not add to cache. |
| width | Number |
Texture width. |
| height | Number |
Texture height. |
| [channels] | Number |
Texture channels count. Defaults to 4 (RGBA). |
Example
let width = 512;
let height = 512;
let renderTarget = await Shaku.assets.createRenderTarget("optional_render_target_asset_id", width, height);
Promise.<TextureAtlas>Create a texture atlas asset.
Kind: instance method of Assets
Returns: Promise.<TextureAtlas> - Promise to resolve with asset instance, when loaded. You can access the loading asset with .asset on the promise.
| Param | Type | Description |
|---|---|---|
| name | String | null |
Asset name (matched to URLs when using cache). If null, will not add to cache. |
| sources | Array.<String> |
List of URLs to load textures from. |
| [maxWidth] | Number |
Optional atlas textures max width. |
| [maxHeight] | Number |
Optional atlas textures max height. |
| [extraMargins] | Vector2 |
Optional extra empty pixels to add between textures in atlas. |
Promise.<FontTextureAsset>Load a font texture asset. If already loaded, will use cache.
Kind: instance method of Assets
Returns: Promise.<FontTextureAsset> - promise to resolve with asset instance, when loaded. You can access the loading asset with .asset on the promise.
| Param | Type | Description |
|---|---|---|
| url | String |
Asset URL. |
| params | \* |
Optional params dictionary. See FontTextureAsset.load() for more details. |
Example
let fontTexture = await Shaku.assets.loadFontTexture('assets/DejaVuSansMono.ttf', {fontName: 'DejaVuSansMono'});
Promise.<MsdfFontTextureAsset>Load a MSDF font texture asset. If already loaded, will use cache.
Kind: instance method of Assets
Returns: Promise.<MsdfFontTextureAsset> - promise to resolve with asset instance, when loaded. You can access the loading asset with .asset on the promise.
| Param | Type | Description |
|---|---|---|
| url | String |
Asset URL. |
| [params] | \* |
Optional params dictionary. See MsdfFontTextureAsset.load() for more details. |
Example
let fontTexture = await Shaku.assets.loadMsdfFontTexture('DejaVuSansMono.font', {jsonUrl: 'assets/DejaVuSansMono.json', textureUrl: 'assets/DejaVuSansMono.png'});
Promise.<JsonAsset>Load a json asset. If already loaded, will use cache.
Kind: instance method of Assets
Returns: Promise.<JsonAsset> - promise to resolve with asset instance, when loaded. You can access the loading asset with .asset on the promise.
| Param | Type | Description |
|---|---|---|
| url | String |
Asset URL. |
Example
let jsonData = await Shaku.assets.loadJson('assets/my_json_data.json');
console.log(jsonData.data);
Promise.<JsonAsset>Create a new json asset. If already exist, will reject promise.
Kind: instance method of Assets
Returns: Promise.<JsonAsset> - promise to resolve with asset instance, when ready. You can access the loading asset with .asset on the promise.
| Param | Type | Description |
|---|---|---|
| name | String |
Asset name (matched to URLs when using cache). If null, will not add to cache. |
| data | Object | String |
Optional starting data. |
Example
let jsonData = await Shaku.assets.createJson('optional_json_data_id', {"foo": "bar"});
// you can now load this asset from anywhere in your code using 'optional_json_data_id' as url
Promise.<BinaryAsset>Load a binary data asset. If already loaded, will use cache.
Kind: instance method of Assets
Returns: Promise.<BinaryAsset> - promise to resolve with asset instance, when loaded. You can access the loading asset with .asset on the promise.
| Param | Type | Description |
|---|---|---|
| url | String |
Asset URL. |
Example
let binData = await Shaku.assets.loadBinary('assets/my_bin_data.dat');
console.log(binData.data);
Promise.<BinaryAsset>Create a new binary asset. If already exist, will reject promise.
Kind: instance method of Assets
Returns: Promise.<BinaryAsset> - promise to resolve with asset instance, when ready. You can access the loading asset with .asset on the promise.
| Param | Type | Description |
|---|---|---|
| name | String |
Asset name (matched to URLs when using cache). If null, will not add to cache. |
| data | Array.<Number> | Uint8Array |
Binary data to set. |
Example
let binData = await Shaku.assets.createBinary('optional_bin_data_id', [1,2,3,4]);
// you can now load this asset from anywhere in your code using 'optional_bin_data_id' as url
Destroy and free asset from cache.
Kind: instance method of Assets
| Param | Type | Description |
|---|---|---|
| url | String |
Asset URL to free. |
Example
Shaku.assets.free("my_asset_url");
Free all loaded assets from cache.
Kind: instance method of Assets
Example
Shaku.assets.clearCache();