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. To determine if the reference is set, use RuntimeKeyIsValid().
Inheritance
Namespace: UnityEngine.AddressableAssets
Syntax
[Serializable]
public class AssetReference : IKeyEvaluator
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 AsyncOperationHandle returned from LoadAssetAsync completes. It will not be set if only InstantiateAsync is called. It will be set to null if release is called.
Declaration
public object Asset { get; }
Property Value
| Type | Description |
|---|---|
| System.Object |
RuntimeKey
The actual key used to request the asset at runtime. RuntimeKeyIsValid() can be used to determine if this reference was set.
Declaration
public object RuntimeKey { get; }
Property Value
| Type | Description |
|---|---|
| System.Object |
Implements
Methods
Instantiate(Transform, Boolean)
InstantiateAsync the referenced asset as type TObject.
Declaration
[Obsolete]
public AsyncOperationHandle<GameObject> Instantiate(Transform parent = null, bool instantiateInWorldSpace = false)
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 |
|---|---|
| AsyncOperationHandle<GameObject> |
Instantiate(Vector3, Quaternion, Transform)
InstantiateAsync the referenced asset as type TObject.
Declaration
[Obsolete]
public AsyncOperationHandle<GameObject> Instantiate(Vector3 position, Quaternion rotation, Transform parent = null)
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 |
|---|---|
| AsyncOperationHandle<GameObject> |
InstantiateAsync(Transform, Boolean)
InstantiateAsync the referenced asset as type TObject.
Declaration
public AsyncOperationHandle<GameObject> InstantiateAsync(Transform parent = null, bool instantiateInWorldSpace = false)
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 |
|---|---|
| AsyncOperationHandle<GameObject> |
InstantiateAsync(Vector3, Quaternion, Transform)
InstantiateAsync the referenced asset as type TObject.
Declaration
public AsyncOperationHandle<GameObject> InstantiateAsync(Vector3 position, Quaternion rotation, Transform parent = null)
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 |
|---|---|
| AsyncOperationHandle<GameObject> |
LoadAsset<TObject>()
Load the referenced asset as type TObject.
Declaration
[Obsolete]
public AsyncOperationHandle<TObject> LoadAsset<TObject>()
Returns
| Type | Description |
|---|---|
| AsyncOperationHandle<TObject> | The load operation. |
Type Parameters
| Name | Description |
|---|---|
| TObject | The object type. |
LoadAssetAsync<TObject>()
Load the referenced asset as type TObject.
Declaration
public AsyncOperationHandle<TObject> LoadAssetAsync<TObject>()
Returns
| Type | Description |
|---|---|
| AsyncOperationHandle<TObject> | The load operation. |
Type Parameters
| Name | Description |
|---|---|
| TObject | The object type. |
LoadScene()
Loads the reference as a scene.
Declaration
[Obsolete]
public AsyncOperationHandle<SceneInstance> LoadScene()
Returns
| Type | Description |
|---|---|
| AsyncOperationHandle<SceneInstance> | The operation handle for the scene load. |
LoadSceneAsync()
Loads the reference as a scene.
Declaration
public AsyncOperationHandle<SceneInstance> LoadSceneAsync()
Returns
| Type | Description |
|---|---|
| AsyncOperationHandle<SceneInstance> | The operation handle for the scene load. |
ReleaseAsset()
Release the internal operation handle.
Declaration
public void ReleaseAsset()
ReleaseInstance(GameObject)
Release an instantiated object.
Declaration
public void ReleaseInstance(GameObject obj)
Parameters
| Type | Name | Description |
|---|---|---|
| GameObject | obj | The object to release. |
RuntimeKeyIsValid()
Checks if the current RuntimeKey is valid.
Declaration
public bool RuntimeKeyIsValid()
Returns
| Type | Description |
|---|---|
| System.Boolean | Whether the RuntimeKey is valid or not. |
Implements
ToString()
String representation of asset reference.
Declaration
public override string ToString()
Returns
| Type | Description |
|---|---|
| System.String | The asset guid as a string. |
Overrides
ValidateAsset(Object)
Validates that the referenced asset allowable for this asset reference.
Declaration
public virtual bool ValidateAsset(object obj)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Object | obj | The Object to validate. |
Returns
| Type | Description |
|---|---|
| System.Boolean | Whether the referenced asset is valid. |
ValidateAsset(String)
Validates that the referenced asset allowable for this asset reference.
Declaration
public virtual bool ValidateAsset(string path)
Parameters
| Type | Name | Description |
|---|---|---|
| System.String | path | The path to the asset in question. |
Returns
| Type | Description |
|---|---|
| System.Boolean | Whether the referenced asset is valid. |