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().
Namespace: UnityEngine.AddressableAssets
Syntax
public class AssetReference : object, 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 |
|---|---|---|
| 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 virtual Object Asset { get; }
Property Value
| Type | Description |
|---|---|
| Object |
AssetGUID
Stores the guid of the asset.
Declaration
public virtual string AssetGUID { get; }
Property Value
| Type | Description |
|---|---|
| String |
IsDone
Get the loading status of the internal operation.
Declaration
public bool IsDone { get; }
Property Value
| Type | Description |
|---|---|
| Boolean |
OperationHandle
The AsyncOperationHandle currently being used by the AssetReference. For example, if you call AssetReference.LoadAssetAsync, this property will return a handle to that operation.
Declaration
public AsyncOperationHandle OperationHandle { get; }
Property Value
| Type | Description |
|---|---|
| AsyncOperationHandle |
RuntimeKey
The actual key used to request the asset at runtime. RuntimeKeyIsValid() can be used to determine if this reference was set.
Declaration
public virtual object RuntimeKey { get; }
Property Value
| Type | Description |
|---|---|
| Object |
Implements
SubObjectName
Stores the name of the sub object.
Declaration
public virtual string SubObjectName { get; set; }
Property Value
| Type | Description |
|---|---|
| String |
Methods
Instantiate(Transform, Boolean)
InstantiateAsync the referenced asset as type TObject. This cannot be used a second time until the first load is released. If you wish to call load multiple times on an AssetReference, use Addressables.InstantiateAsync() and pass your AssetReference in as the key. See the Loading Addressable Assets documentation for more details.
Declaration
public AsyncOperationHandle<GameObject> Instantiate(Transform parent = null, bool instantiateInWorldSpace = false)
Parameters
| Type | Name | Description |
|---|---|---|
| Transform | parent | The parent of the instantiated object. |
| Boolean | instantiateInWorldSpace | Option to retain world space when instantiated with a parent. |
Returns
| Type | Description |
|---|---|
| AsyncOperationHandle<GameObject> | Returns the instantiation operation. |
Instantiate(Vector3, Quaternion, Transform)
InstantiateAsync the referenced asset as type TObject. This cannot be used a second time until the first load is released. If you wish to call load multiple times on an AssetReference, use Addressables.InstantiateAsync() and pass your AssetReference in as the key. See the Loading Addressable Assets documentation for more details.
Declaration
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> | Returns the instantiation operation. |
InstantiateAsync(Transform, Boolean)
InstantiateAsync the referenced asset as type TObject. This cannot be used a second time until the first load is released. If you wish to call load multiple times on an AssetReference, use Addressables.InstantiateAsync() and pass your AssetReference in as the key. See the Loading Addressable Assets documentation for more details.
Declaration
public virtual AsyncOperationHandle<GameObject> InstantiateAsync(Transform parent = null, bool instantiateInWorldSpace = false)
Parameters
| Type | Name | Description |
|---|---|---|
| Transform | parent | The parent of the instantiated object. |
| 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. This cannot be used a second time until the first load is released. If you wish to call load multiple times on an AssetReference, use Addressables.InstantiateAsync() and pass your AssetReference in as the key. See the Loading Addressable Assets documentation for more details.
Declaration
public virtual 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> |
IsValid()
Returns the state of the internal operation.
Declaration
public bool IsValid()
Returns
| Type | Description |
|---|---|
| Boolean | True if the operation is valid. |
LoadAsset<TObject>()
Declaration
public AsyncOperationHandle<TObject> LoadAsset<TObject>()
Returns
| Type | Description |
|---|---|
| AsyncOperationHandle<TObject> |
Type Parameters
| Name | Description |
|---|---|
| TObject |
LoadAssetAsync<TObject>()
Declaration
public virtual AsyncOperationHandle<TObject> LoadAssetAsync<TObject>()
Returns
| Type | Description |
|---|---|
| AsyncOperationHandle<TObject> |
Type Parameters
| Name | Description |
|---|---|
| TObject |
LoadScene()
Loads the reference as a scene. This cannot be used a second time until the first load is released. If you wish to call load multiple times on an AssetReference, use Addressables.LoadSceneAsync() and pass your AssetReference in as the key. See the Loading Addressable Assets documentation for more details.
Declaration
public AsyncOperationHandle<SceneInstance> LoadScene()
Returns
| Type | Description |
|---|---|
| AsyncOperationHandle<SceneInstance> | The operation handle for the scene load. |
LoadSceneAsync(LoadSceneMode, Boolean, Int32)
Loads the reference as a scene. This cannot be used a second time until the first load is unloaded. If you wish to call load multiple times on an AssetReference, use Addressables.LoadSceneAsync() and pass your AssetReference in as the key. See the Loading Addressable Assets documentation for more details.
Declaration
public virtual AsyncOperationHandle<SceneInstance> LoadSceneAsync(LoadSceneMode loadMode = null, bool activateOnLoad = true, int priority = 100)
Parameters
| Type | Name | Description |
|---|---|---|
| LoadSceneMode | loadMode | Scene load mode. |
| Boolean | activateOnLoad | If false, the scene will load but not activate (for background loading). The SceneInstance returned has an Activate() method that can be called to do this at a later point. |
| Int32 | priority | Async operation priority for scene loading. |
Returns
| Type | Description |
|---|---|
| AsyncOperationHandle<SceneInstance> | The operation handle for the request if there is not a valid cached operation, otherwise return default operation |
ReleaseAsset()
Release the internal operation handle.
Declaration
public virtual void ReleaseAsset()
ReleaseInstance(GameObject)
Release an instantiated object.
Declaration
public virtual void ReleaseInstance(GameObject obj)
Parameters
| Type | Name | Description |
|---|---|---|
| GameObject | obj | The object to release. |
RuntimeKeyIsValid()
Checks if the current RuntimeKey is valid.
Declaration
public virtual bool RuntimeKeyIsValid()
Returns
| Type | Description |
|---|---|
| Boolean | Whether the RuntimeKey is valid or not. |
Implements
ToString()
String representation of asset reference.
Declaration
public override string ToString()
Returns
| Type | Description |
|---|---|
| String | The asset guid as a string. |
UnLoadScene()
Unloads the reference as a scene.
Declaration
public virtual AsyncOperationHandle<SceneInstance> UnLoadScene()
Returns
| Type | Description |
|---|---|
| AsyncOperationHandle<SceneInstance> | The operation handle for the scene load. |
ValidateAsset(Object)
Validates that the referenced asset allowable for this asset reference.
Declaration
public virtual bool ValidateAsset(Object obj)
Parameters
| Type | Name | Description |
|---|---|---|
| Object | obj | The Object to validate. |
Returns
| Type | Description |
|---|---|
| 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 |
|---|---|---|
| String | path | The path to the asset in question. |
Returns
| Type | Description |
|---|---|
| Boolean | Whether the referenced asset is valid. |