AssetImporterPlugin

View source

ExtendsPlugin

Registers one typed Importer with an app so the AssetServer can use it for matching load requests.

Keep options.asset equal to Importer#asset. The asset server uses the importer’s constructor and the exact labels from Importer#getExtensions to select a converter. Use this plugin with AssetServerPlugin; it registers the importer but does not fetch or deserialize assets itself.

Examples

import { App } from '@wimaengine/app'
import { AssetImporterPlugin, AssetServerPlugin, Importer } from '@wimaengine/asset'
import { CorePlugin } from '@wimaengine/core'
import { ReflectPlugin } from '@wimaengine/reflect'

class TextAsset {
  constructor(value) {
    this.value = value
  }
}

class TextImporter extends Importer {
  constructor() {
    super(TextAsset)
  }

  async deserialize(response, _typeRegistry) {
    return new TextAsset(await response.text())
  }

  getExtensions() {
    return ['txt']
  }
}

new App()
  .registerPlugin(new ReflectPlugin())
  .registerPlugin(new CorePlugin())
  .registerPlugin(new AssetServerPlugin())
  .registerPlugin(new AssetImporterPlugin({
    asset: TextAsset,
    importer: new TextImporter()
  }))
  .setRunner(() => {})
  .run()
// The app can now load TextAsset values from `.txt` files.

Constructor


constructor<T>(options: AssetImporterPluginOptions<T>)

View source

Creates a plugin for one asset constructor and its importer.

The asset and importer values are stored unchanged. Keep asset equal to Importer#asset because the importer’s constructor identifies the type selected by the asset server.

Type Parameters

T

Parameters

NameDescriptionDefault
options

Asset/importer pair to register.

—

Properties


readonlyasset: Constructor<T>

View source

Asset constructor paired with this plugin’s importer.

Pass the same constructor stored in Importer#asset. The importer’s constructor controls asset-server lookup, while this value also contributes to the plugin’s identity.

Methods


requires(): TypeId[]

View source

Returns the plugins required before this plugin can register its importer.

The app validates these dependencies but does not install them automatically.

Returns

Type IDs for CorePlugin and AssetServerPlugin.

name(): TypeId

View source

Returns this plugin’s identity for its asset constructor.

Different asset constructors receive different identities. Registering a second importer plugin for the same constructor produces a duplicate identity rejected by the app.

Returns

Plugin identity as a TypeId.