欢迎光临散文网 会员登陆 & 注册

Unity_Addressable_Asset References(资源参考)

2023-04-01 20:37 作者:unity_某某师_高锦锦  | 我要投稿

An AssetReference is a type that can reference an Addressable asset.

译:AssetReference是一种可以引用Addressable资源的类型。

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.

译:在MonoBehaviour或ScriptableObject中使用AssetReference类。当您在这些类中添加可序列化的AssetReference字段时,您可以在检查器窗口中为该字段分配一个值。您可以通过类型和标签限制可以分配给字段的资源。

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.

译:为了分配一个值,拖拽一个资源到字段中或者点击对象选择器图标打开一个对话框,让您选择一个Addressable资源。

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.

译:如果您将一个非Addressable资源拖放到AssetReference字段中,系统会自动将该资源设置为Addressable并将其添加到您的默认Addressable组中。Sprite和SpriteAtlas资源可以有子对象。分配这些类型的AssetReference会显示一个额外的对象选择器,允许您指定要引用哪个子对象。

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

译:请参见Addressables-Sample存储库中的Basic AssetReference、Component Reference和Sprite Land项目,以了解在项目中使用AssetReferences的示例。

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 group’s Advanced Settings. The Include GUID in Catalog option is enabled by default.

译:为了能够将组中的资源分配给AssetReference字段,必须在组的高级设置中启用“在目录中包含GUID”选项。默认情况下,“在目录中包含GUID”选项已启用。

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.

译:AssetReference类型:Addressables API提供了常见类型资源的AssetReference子类。您可以使用通用子类AssetReferenceT<TObject>将AssetReference字段限制为其他类型的资源。

The types of AssetReference include:

译:AssetReference类型包括:

  • AssetReference: can reference any asset type译:AssetReference:可以引用任何类型的资源。

  • AssetReferenceT<TObject>: can reference assets that are the same type as TObject译:AssetReferenceT:可以引用与TObject类型相同的资源。

  • AssetReferenceTexture: can reference a Texture asset译:AssetReferenceTexture:可以引用纹理资源

  • AssetReferenceTexture2D: can reference a Texture2D asset译:AssetReferenceTexture2D:可以引用纹理2D资源

  • AssetReferenceTexture3D: can reference a Texture3D asset译:AssetReferenceTexture3D:可以引用纹理3D资源

  • AssetReferenceGameObject: can reference a Prefab asset译:AssetReferenceGameObject:可以引用预制件资源

  • AssetReferenceAtlasedSprite: can reference a SpriteAtlas asset译:AssetReferenceAtlasedSprite:可以引用SpriteAtlas资源

  • AssetReferenceSprite: can reference a single Sprite asset译:AssetReferenceSprite:可以引用单个Sprite资源

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.

译:如果您想要使用自定义属性绘制器与通用的AssetReferenceT(或使用早于2020.1版本的Unity),则必须创建一个具体的子类来支持自定义AssetReference类型

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:

译:在 MonoBehaviour 或 ScriptableObject 中添加 AssetReference 或其子类,可以通过将其声明为该类的可序列化字段来实现:

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.

译:在 Unity 2020.1 之前,检查器窗口默认无法显示通用字段。在早期版本的 Unity 中,您必须创建自己的非通用 AssetReferenceT 子类。请参阅创建具体子类。

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.

译:AssetReference 类提供了自己的方法来加载、实例化和释放引用的资源。您还可以使用 AssetReference 实例作为任何 Addressables 类方法加载资源的键。

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

译:以下示例将 AssetReference 实例实例化为当前 GameObject 的子对象,并在父对象被销毁时释放它:

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

译:有关使用 AssetReferences 加载资源的更多信息和示例,请参阅加载 AssetReference。

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.

译:使用 AssetReferenceUILabelRestriction 属性将可以分配给 AssetReference 字段的资源限制为具有特定标签的资源。您可以将此属性引用与 AssetReference 子类一起使用,以通过类型和标签限制分配。

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":

译:以下示例防止将 Addressable 资源分配给没有标签“animals”或标签“characters”的引用:

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.译:该属性仅使用 Unity 编辑器中的检查器防止分配未指定标签的资产。某些人仍然可以使用脚本将没有标签的资产分配给该字段。

  • You cannot drag non-Addressable assets to a field with the AssetReferenceUILabelRestriction attribute.译:您无法将非 Addressable 资产拖动到具有 AssetReferenceUILabelRestriction 属性的字段中。

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.

译:在无法直接使用 AssetReference 类的通用形式(在 Unity 版本低于 Unity 202.1 或使用 CustomPropertyDrawer 属性时)的情况下,您可以创建具体子类。

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:

译:要创建具体子类,请继承 AssetReferenceT 类并指定资产类型。您还必须将 GUID 字符串传递给基类构造函数:

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

译:您可以像其他 AssetReference 类型一样在另一个脚本中使用自定义 AssetReference 子类:




Unity_Addressable_Asset References(资源参考)的评论 (共 条)

分享到微博请遵守国家法律