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 RuntimeKey.isValid.
Inheritance
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 |
---|---|
System.Object |
RuntimeKey
The actual key used to request the asset at runtime. RuntimeKey.isValid can be used to determine if this reference was set.
Declaration
public Hash128 RuntimeKey { get; }
Property Value
Type | Description |
---|---|
Hash128 |
Methods
Instantiate(Transform, Boolean)
Instantiate the referenced asset as type TObject.
Declaration
public IAsyncOperation<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 |
---|---|
IAsyncOperation<GameObject> |
Instantiate(Vector3, Quaternion, Transform)
Instantiate the referenced asset as type TObject.
Declaration
public IAsyncOperation<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 |
---|---|
IAsyncOperation<GameObject> |
LoadAsset<TObject>()
Load the referenced asset as type TObject.
Declaration
public IAsyncOperation<TObject> LoadAsset<TObject>()
Returns
Type | Description |
---|---|
IAsyncOperation<TObject> | The load operation. |
Type Parameters
Name | Description |
---|---|
TObject | The object type. |
ReleaseAsset()
Release the referenced asset.
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. |
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. |