Class AssetReference
Reference to an addressable asset. This can be used in script to provide fields that can be easily set in the editor and loaded dynamically at runtime.
Inheritance
Inherited Members
Namespace: UnityEngine.AddressableAssets
Syntax
[Serializable]
public class AssetReference
Constructors
AssetReference()
Construct a new AssetReference object.
Declaration
public AssetReference()
AssetReference(String)
Construct a new AssetReference object.
Declaration
public AssetReference(string guid)
Parameters
| Type | Name | Description |
|---|---|---|
| System.String | guid | The guid of the asset. |
Properties
Asset
The loaded asset. This value is only set after the IAsyncOperation returned from LoadAsset completes. It will not be set if only Instantiate is called. It will be set to null if release is called.
Declaration
public Object Asset { get; }
Property Value
| Type | Description |
|---|---|
| Object |
RuntimeKey
The actual key used to request the asset at runtime.
Declaration
public Hash128 RuntimeKey { get; }
Property Value
| Type | Description |
|---|---|
| Hash128 |
Methods
Instantiate<TObject>(Transform, Boolean)
Instantiate the referenced asset as type TObject.
Declaration
public IAsyncOperation<TObject> Instantiate<TObject>(Transform parent = null, bool instantiateInWorldSpace = false)where TObject : Object
Parameters
| Type | Name | Description |
|---|---|---|
| Transform | parent | The parent of the instantiated object. |
| System.Boolean | instantiateInWorldSpace | Option to retain world space when instantiated with a parent. |
Returns
| Type | Description |
|---|---|
| IAsyncOperation<TObject> |
Type Parameters
| Name | Description |
|---|---|
| TObject | The object type. |
Instantiate<TObject>(Vector3, Quaternion, Transform)
Instantiate the referenced asset as type TObject.
Declaration
public IAsyncOperation<TObject> Instantiate<TObject>(Vector3 position, Quaternion rotation, Transform parent = null)where TObject : Object
Parameters
| Type | Name | Description |
|---|---|---|
| Vector3 | position | Position of the instantiated object. |
| Quaternion | rotation | Rotation of the instantiated object. |
| Transform | parent | The parent of the instantiated object. |
Returns
| Type | Description |
|---|---|
| IAsyncOperation<TObject> |
Type Parameters
| Name | Description |
|---|---|
| TObject | The object type. |
LoadAsset<TObject>()
Load the referenced asset as type TObject.
Declaration
public IAsyncOperation<TObject> LoadAsset<TObject>()where TObject : Object
Returns
| Type | Description |
|---|---|
| IAsyncOperation<TObject> | The load operation. |
Type Parameters
| Name | Description |
|---|---|
| TObject | The object type. |
ReleaseAsset<TObject>()
Release the refrerenced asset.
Declaration
public void ReleaseAsset<TObject>()where TObject : Object
Type Parameters
| Name | Description |
|---|---|
| TObject | The object type. |
ReleaseAsset<TObject>(Object)
Release the refrerenced asset.
Declaration
[Obsolete("Use ReleaseAsset without parameters. It will release the asset loaded via LoadAsset.")]
public void ReleaseAsset<TObject>(Object asset)where TObject : Object
Parameters
| Type | Name | Description |
|---|---|---|
| Object | asset |
Type Parameters
| Name | Description |
|---|---|
| TObject | The object type. |
ReleaseInstance<TObject>(TObject)
Release an instantiated object.
Declaration
public void ReleaseInstance<TObject>(TObject obj)where TObject : Object
Parameters
| Type | Name | Description |
|---|---|---|
| TObject | obj | The object to release. |
Type Parameters
| Name | Description |
|---|---|
| TObject | The object type. |
ToString()
String representation of asset reference.
Declaration
public override string ToString()
Returns
| Type | Description |
|---|---|
| System.String | The asset guid as a string. |
Overrides
ValidateType(Type)
Validates that the referenced asset is the correct type.
Declaration
public virtual bool ValidateType(Type type)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Type | type | The type to validate. |
Returns
| Type | Description |
|---|---|
| System.Boolean | Whether the referenced asset is the specified type. |