# AssetReference

Class · category: Asset

Source: https://github.com/playcanvas/engine/blob/970d89f6d3bc6667f4e1f88153abd6e7e82ed52c/src/framework/asset/asset-reference.js#L22

An AssetReference follows a single asset slot on behalf of its owner, such as the texture
property of a component, and calls back when that asset is added to the registry, loads or is
removed. Point it at an asset by setting [id](https://api.playcanvas.com/engine/classes/AssetReference.md#id) or [url](https://api.playcanvas.com/engine/classes/AssetReference.md#url). The asset does not need to
exist yet: the `add` callback fires when an asset with that id or URL is later registered. The
`unload` callback is delivered only for a reference by id.

**Example**

```ts
const reference = new AssetReference('textureAsset', this, this.app.assets, {
    load: this.onTextureAssetLoad,
    remove: this.onTextureAssetRemove
}, this);
reference.id = this.textureAsset.id;
```

## Constructors

### constructor

```ts
new AssetReference(propertyName: string, parent: any, registry: AssetRegistry, callbacks: object, scope?: any)
```

Create a new AssetReference instance.

**Parameters**

- `propertyName` (`string`): The name of the property that the asset is stored under,
  passed into callbacks to enable updating.
- `parent` (`any`): The parent object that contains the asset reference, passed
  into callbacks to enable updating. Currently an asset, but could be component or other.
- `registry` ([`AssetRegistry`](https://api.playcanvas.com/engine/classes/AssetRegistry.md)): The asset registry that stores all assets.
- `callbacks` (`object`): A set of functions called when the asset state changes: load,
  add, remove.
    - `callbacks.add` (`any`, optional): The function called when the asset is added to the
      registry add(propertyName, parent, asset).
    - `callbacks.load` (`any`, optional): The function called when the asset loads
      load(propertyName, parent, asset).
    - `callbacks.remove` (`any`, optional): The function called when the asset is remove from the
      registry remove(propertyName, parent, asset).
    - `callbacks.unload` (`any`, optional): The function called when the asset is unloaded
      unload(propertyName, parent, asset).
- `scope` (`any`, optional): The scope to call the callbacks in.

**Example**

```ts
const reference = new AssetReference('textureAsset', this, this.app.assets, {
    load: this.onTextureAssetLoad,
    add: this.onTextureAssetAdd,
    remove: this.onTextureAssetRemove
}, this);
reference.id = this.textureAsset.id;
```

## Accessors

### id

```ts
get id(): number | null
set id(value: number | null)
```

Gets the asset id which this references.

### url

```ts
get url(): string | null
set url(value: string | null)
```

Gets the asset url which this references.
