Asset reference introduction
An AssetReference
is a type that can reference an Addressable asset.
Use the AssetReference
class in a MonoBehaviour
or ScriptableObject
. When you add a serializable AssetReference
field to one of these classes, you can assign a value to the field in an Inspector window. You can restrict the assets that can be assigned to a field by type and by label.
An Inspector window displaying several AssetReference fields
To assign a value, drag an asset to the field or select the object picker icon to open a dialog that lets you choose an Addressable asset.
If you drag a non-Addressable asset to an AssetReference
field, the system automatically makes the asset Addressable and adds it to your default Addressables group. Sprite and SpriteAtlas assets can have sub-objects. AssetReferences assigned these types of asset display an additional object picker that you can use to specify which sub-object to reference.
For examples of using AssetReference
types in a project refer to the Basic AssetReference, Component Reference, and Sprite Land projects in the Addressables-Sample repository.
Important
To be able to assign assets from a group to an AssetReference field, you must enable the Include GUIDs in Catalog property in the group’s Advanced Options. The Include GUIDs in Catalog option is enabled by default. For more information, refer to Content Packing & Loading schema reference.
AssetReference types
The Addressables API provides AssetReference
subclasses for common types of assets. You can use the generic subclass, AssetReferenceT<TObject>
, to restrict an AssetReference field to other asset types.
The types of AssetReference include:
AssetReference type | Description |
---|---|
AssetReference |
Can reference any asset type |
AssetReferenceT<TObject> |
Can reference assets that are the same type as TObject |
AssetReferenceTexture |
Can reference a Texture asset. |
AssetReferenceTexture2D |
Can reference a Texture2D asset. |
AssetReferenceTexture3D |
Can reference a Texture3D asset. |
AssetReferenceGameObject |
Can reference a Prefab asset. |
AssetReferenceAtlasedSprite |
Can reference a SpriteAtlas asset. |
AssetReferenceSprite |
Can reference a single Sprite asset. |
Note
If you want to use a CustomPropertyDrawer
with a generic AssetReferenceT
, or are using a version of Unity earlier than 2020.1, you must make a concrete subclass to support custom AssetReference
types.