Firstborn/Library/PackageCache/com.unity.addressables@1.19.19/Documentation~/AssetReferences.md
Schaken-Mods 7502018d20 Adding Mod Support
There is an asset in the store I grabbed. the coding is WAY above my head, I got about half of it and integrated and adapted what I can to it. im going as far as I can with it and ill come back in a few month when I understand t better.
2023-05-13 22:01:48 -05:00

8.9 KiB
Raw Blame History

uid
addressables-asset-references

Asset References

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.

image alt text
An Inspector window showing several AssetReference fields

To assign a value, drag an asset to the field or click on 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 subobjects. AssetReferences assigned these types of asset display an additional object picker that allows you to specify which subobject to reference.

See the Basic AssetReference, Component Reference, and Sprite Land projects in the Addressables-Sample repository for examples of using AssetReferences in a project.

Important

To be able to assign assets from a group to an AssetReference field, the Include GUID in Catalog option must be enabled in the groups Advanced Settings. The Include GUID in Catalog option is enabled by default.

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:

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.

Adding AssetReference fields to a class

Add an AssetReference, or one of its subclasses, to a MonoBehaviour or ScriptableObject by declaring it as a serializable field to the class:

[!code-cssample]

Note

Before Unity 2020.1, the Inspector window couldn't display generic fields by default. In earlier versions of Unity, you must make your own non-generic subclass of AssetReferenceT instead. See Creating a concrete subclass.

Loading and releasing AssetReferences

The AssetReference class provides its own methods to load, instantiate, and release a referenced asset. You can also use an AssetReference instance as a key to any Addressables class method that loads assets.

The following example instantiates an AssetReference as a child of the current GameObject and releases it when the parent is destroyed:

[!code-cssample]

See Loading an AssetReference for more information and examples about loading assets using AssetReferences.

Restricting AssetReference assignment to assets with specific labels

Use the AssetReferenceUILabelRestriction attribute to restrict the assets you can assign to an AssetReference field to those with specific labels. You can use this attribute reference in addition to AssetReference subclasses to restrict assignment by both type and label.

The following example prevents someone from assigning an Addressable asset to a reference that does not have either the label, "animals", or the label, "characters":

[!code-cssample]

[!Note]

  • The attribute only prevents assigning assets without the specified label using an Inspector in the Unity Editor. Someone could still assign an asset without the label to the field using a script.
  • You cannot drag non-Addressable assets to a field with the AssetReferenceUILabelRestriction attribute.

Creating a concrete subclass

For those cases in which you cannot use the generic form of the AssetReference class directly (in versions of Unity prior to Unity 202.1 or when using the CustomPropertyDrawer attribute), you can create a concrete subclass.

To create a concrete subclass, inherit from the AssetReferenceT class and specify the asset type. You must also pass the GUID string to the base class constructor:

[!code-cssample]

You can use your custom AssetReference subclass in another script the same way as other AssetReference types:

[!code-cssample]