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

Unity_Addressable_升级到Addressables系统

2023-03-27 11:43 作者:unity_某某师_高锦锦  | 我要投稿

This article covers how to modify your existing project to take advantage of Addressable assets.

译:本文介绍了如何修改现有项目以使用Addressable 。

Outside of the Addressables system, Unity provides a few "traditional" ways to reference and load assets:

译:在Addressables系统之外,Unity提供了一些“传统的”方法来引用和加载资产:

  • Scene data: Assets you add directly to a Scene or to a component in a Scene, which the application loads automatically. Unity packages serialized scene data and the assets directly referenced by a scene into a single archive that it includes in your built player application. See Converting Scenes and Using Addressable assets in non-Addressable Scenes.

  • 译:场景数据:直接添加到场景或场景中的组件的资产,应用程序会自动加载这些资产。Unity将序列化的场景数据和直接由场景引用的资产打包到一个单独的存档中,包括在你构建的播放器应用程序中。参见转换场景和在非可寻址场景中使用可寻址资产。

  • Prefabs: Assets you create using GameObjects and components, and save outside a Scene. See Converting Prefabs.

  • 译:预设物:你使用游戏对象和组件创建的资产,并保存在场景之外。参见转换预设物。

  • Resources folders: Assets you place in your project’s Resources folders and load using the Resources API. Unity packages assets in Resources files into a single archive that it includes in your built player application. The Resources archive is separate from the Scene data archive. See Converting Resources folders.

  • 译:Resources文件夹:您放置在项目的资源文件夹中的资产,并使用资源API加载。Unity将资源文件中的资产打包成一个单一的存档,它包括在您构建的播放器应用程序中。资源归档与场景数据归档是分开的。参见Resources文件夹

  • AssetBundles: Assets you package in AssetBundles and load with the AssetBundle API. See Converting AssetBundles.

  • 译:AssetBundles:你在AssetBundle中打包的资产,用AssetBundle API加载。参见AssetBundles

  • StreamingAssets: Files you place in the StreamingAssets folder. Unity includes any files in the StreamingAssets folder in your built player application as is. See Files in StreamingAssets

  • 译:StreamingAssets:你放在StreamingAssets文件夹中的文件。Unity包括任何文件在StreamingAssets文件夹在你构建的播放器应用程序。参见StreamingAssets中的文件

Converting to Addressables

Content built using Addressables only reference other assets built in that Addressables build. Content that is used or referenced to that is included within both Addressables, and the Player build through the Scene data and Resource folders is duplicated on disk and in memory if they are both loaded. Due to this limitation the recommended best practice is to convert all Scene data and Resource folders to the Addressables build system. Reducing the memory overhead due to duplication and allowing all content to be managed using Addressables. Allowing for the content to be either local or remote as well as updatable through Content Update builds.

译:使用Addressables构建的内容只引用在该Addressables构建中构建的其他资产。使用或引用的内容包含在两个Addressables中,通过场景数据和资源文件夹构建的播放器在磁盘和内存中复制(如果它们都被加载)。由于这种限制,推荐的最佳实践是将所有场景数据和资源文件夹转换为Addressables构建系统。减少由于复制造成的内存开销,并允许使用Addressables管理所有内容。允许内容是本地或远程的,并且可以通过内容更新构建进行更新。

Converting Scenes

The easiest way to integrate Addressables into a project is to move your Scenes out of the Build Settings list and make those scenes Addressable. You do need to have one Scene in the list, which is the Scene Unity loads at application startup. You can make a new Scene for this that does nothing else than load your first Addressable Scene.

译:将可寻址场景集成到项目中最简单的方法是将场景移出构建设置列表,并使这些场景可寻址。你需要在列表中有一个场景,这是Unity在应用启动时加载的场景。你可以为此创建一个新的场景,除了加载你的第一个可寻址场景外什么都不做。

To convert your Scenes:

译:转换场景:

  1. Make a new "initialization" Scene.译:创建一个新的初始化场景。

  2. Open the Build Settings window (menu: File > Build Settings).译: File > Build Settings

  3. Add the initialization Scene to the Scene list.译: 添加初始化场景到场景列表

  4. Remove the other Scenes from the list.译:从列表中移除其他场景

  5. Click on each Scene in the project list and check the Addressable option in its Inspector window. Alternatively, you can drag Scene assets to a group in the Addressables Groups window. (Don't make your new initialization Scene Addressable.)译:

  6. Update the code you use to load Scenes to use the Addressables class Scene loading methods rather than the SceneManager methods.译:点击项目列表中的每个场景,并在其检查器窗口中检查Addressable选项。或者,您可以将场景资产拖到Addressables Groups窗口中的一个组中。(不要让你的新初始化场景可寻址。)

At this point, you have included all the assets you have in your Scenes in an Addressable group and the Addressables system packages them in an AssetBundle when you build your Addressables content. If you only use one group for all your Scenes, the runtime loading and memory performance should be roughly equivalent to your project’s pre-Addressables state.

译:在这一点上,你已经在一个Addressables组中包含了你在场景中的所有资产,当你构建你的Addressables内容时,Addressables系统将它们打包在一个AssetBundle中。如果你只对所有的场景使用一个组,运行时加载和内存性能应该大致相当于项目的pre-Addressables状态。

You can now split your one, large Addressable Scene group into multiple groups. The best way to do that depends on the project goals. To proceed, you can move your Scenes into their own groups so that you can load and unload each of them independently of each other. As you do this, you can use the Analyze tool to check for duplicated assets that are shared between multiple Scenes. You can avoid duplicating an asset referenced from two different bundles by making the asset itself Addressable. It's often better to move shared assets to their own group as well to reduce interdependencies among your AssetBundles.

译:你现在可以把一个大的可寻址场景组分成多个组。做到这一点的最佳方式取决于项目目标。接下来,你可以将你的场景移动到它们自己的组中,这样你就可以独立地加载和卸载每个场景。当您这样做时,您可以使用Analyze工具来检查在多个场景之间共享的重复资产。通过将资产本身设置为可寻址的,可以避免重复从两个不同的bundle引用的资产。最好将共享资产移动到它们自己的组中,以减少您的AssetBundles之间的相互依赖。


Using Addressable assets in non-Addressable Scenes

For Scenes that you don't want to make Addressable, you can still use Addressable assets as part of the Scene data through AssetReferences.

译:对于你不想使用Addressable的场景,你仍然可以通过AssetReferences使用Addressable作为场景数据的一部分。

When you add an AssetReference field to a custom MonoBehaviour or ScriptableObject class, you can assign an Addressable asset to the field in the Unity Editor in much the same way that you would assign an asset as a direct reference. The main difference is that you need to add code to your class to load and release the asset assigned to the AssetReference field (whereas Unity loads direct references automatically when it instantiates your object in the Scene).

译:当你将一个AssetReference字段添加到一个自定义MonoBehaviour或ScriptableObject类时,你可以在Unity Editor中将一个Addressable资产分配给该字段,就像你将资产分配为直接引用一样。主要的区别是,你需要向你的类添加代码来加载和释放分配给AssetReference字段的资产(而Unity在场景中实例化你的对象时自动加载直接引用)。

NOTE

You cannot use Addressable assets for the fields of any UnityEngine components in a non-Addressable Scene. For example, if you assign an Addressable mesh asset to a MeshFilter component in a non-Addressable Scene, Unity does not use the Addressable version of that mesh data for the Scene. Instead, Unity copies the mesh asset and includes two versions of the mesh in your application, one in the AssetBundle built for the Addressable group containing the mesh and one in the built-in Scene data of the non-Addressable Scene. (When used in an Addressable Scene, Unity does not copy the mesh data and always loads it from the AssetBundle.)

译:在非可寻址场景中,您不能为任何UnityEngine组件的字段使用可寻址资产。例如,如果你将可寻址网格资产分配给一个非可寻址场景中的MeshFilter组件,Unity不会为场景使用该网格数据的可寻址版本。相反,Unity复制了网格资产,并在你的应用程序中包含了两个版本的网格,一个在为包含网格的可寻址组构建的AssetBundle中,一个在非可寻址场景的内置场景数据中。(当在可寻址场景中使用时,Unity不会复制网格数据,总是从AssetBundle中加载它。)

To replace direct references with AssetReferences in your custom classes, follow these steps:

译:要在自定义类中使用AssetReferences替换直接引用,请遵循以下步骤:

  1. Replace your direct references to objects with asset references (for example, public GameObject directRefMember; becomes public AssetReference assetRefMember;).译:将对对象的直接引用替换为资产引用


  1. Drag assets onto the appropriate component’s Inspector, as you would for a direct reference.译:将资产拖放到适当组件的检查器上,就像直接引用一样

  2. Add runtime code to load the assigned asset using the Addressables API.译:添加运行时代码,使用Addressables API加载分配的资产

  3. Add code to release the loaded asset when no longer needed.译:添加代码以在不再需要时释放加载的资产。

See Asset References for more information about using AssetReference fields.

译:有关使用AssetReference字段的更多信息,请参阅AssetReference 。

See Loading Addressable assets for more information about loading Addressable assets.

译:

Converting Prefabs

To convert a Prefab into an Addressable asset, check the Addressables option in its Inspector window or drag it to a group in the Addressables Groups window.

译:有关加载可寻址资产的更多信息,请参见加载可寻址资产。

You don't always need to make Prefabs Addressable when used in an Addressable Scene; Addressables automatically includes Prefabs that you add to the Scene hierarchy as part of the data contained in the Scene’s AssetBundle. If you use a Prefab in more than one Scene, however, you should make the Prefab into an Addressable asset so that the Prefab data isn't duplicated in each Scene that uses it. You must also make a Prefab Addressable if you want to load and instantiate it dynamically at runtime.

译:当在可寻址场景中使用时,你并不总是需要使预置可寻址;Addressables自动包含你添加到场景层次结构中的prefab,作为包含在场景的AssetBundle中的数据的一部分。但是,如果你在多个场景中使用了一个预制件,你应该将该预制件设置为可寻址资产,这样预制件数据就不会在每个使用它的场景中重复。如果你想在运行时动态加载和实例化它,你还必须创建一个Prefab Addressable。

NOTE

If you use a Prefab in a non-Addressable Scene, Unity copies the Prefab data into the built-in Scene data whether the Prefab is Addressable or not. You can identify assets duplicated between your Addressable asset groups and your non-Addressable Scene data using the Check Scene to Addressable Duplicate Dependencies rule in the Analyze tool.

译:如果你在不可寻址场景中使用预制,Unity将预制数据复制到内置场景数据中,无论预制是否可寻址。您可以使用Analyze工具中的Check Scene to Addressable Duplicate Dependencies规则来识别可寻址资产组和非可寻址场景数据之间的重复资产。

Converting Resources folders

If your project loads assets in Resources folders, you can migrate those assets to the Addressables system:

译:如果你的项目在Resources文件夹中加载资产,你可以将这些资产迁移到Addressables系统:

  1. Make the assets Addressable. To do this, either enable the Addressable option in each asset's Inspector window or drag the assets to groups in the Addressables Groups window.译:使资产具有可寻址性。要做到这一点,要么在每个资产的检查器窗口中启用可寻址选项,要么将资产拖到可寻址组窗口中的组中。

  2. Change any runtime code that loads assets using the Resources API to load them with the Addressables API.译:更改任何使用Resources API加载资产的运行时代码,以使用Addressables API加载它们。

  3. Add code to release loaded assets when no longer needed.译:添加代码以在不再需要时释放加载的资产。

As with Scenes, if you keep all the former Resources assets in one group, the loading and memory performance should be equivalent. Depending on your project, you can improve performance and flexibility by dividing your assets into separate groups. Use the Analyze tool to check for unwanted duplication across AssetBundles.

译:与scene一样,如果您将所有以前的Resources资产放在一个组中,那么加载和内存性能应该是相等的。根据您的项目,您可以通过将资产划分为单独的组来提高性能和灵活性。使用Analyze工具检查资产包之间不需要的重复。

When you mark an asset in a Resources folder as Addressable, the system automatically moves the asset to a new folder in your project named Resources_moved. The default address for a moved asset is the old path, omitting the folder name. For example, your loading code might change from:

译:当您将资源文件夹中的资产标记为Addressable时,系统会自动将该资产移动到项目中名为Resources_moved的新文件夹中。移动资产的默认地址是旧路径,省略文件夹名称。例如,您的加载代码可能从以下更改:

to:

You might have to implement some functionality of the Resources class differently after modifying your project to use the Addressables system.

译:在修改项目以使用Addressables系统之后,您可能必须以不同的方式实现Resources类的某些功能。

For example, consider the Resources.LoadAll function. Previously, if you had assets in a folder Resources/MyPrefabs/, and ran Resources.LoadAll<SampleType>("MyPrefabs");, it would have loaded all the assets in Resources/MyPrefabs/ matching type SampleType. The Addressables system doesn't support this exact functionality, but you can achieve similar results using Addressable labels.

译:例如,考虑参考资料。负荷函数。以前,如果你在Resources/MyPrefabs/文件夹中有资产,并运行Resources. loadall <SampleType>("MyPrefabs");,它将加载Resources/MyPrefabs/匹配类型SampleType中的所有资产。Addressables系统并不支持这种确切的功能,但是您可以使用Addressable标签实现类似的结果。

Converting AssetBundles

When you first open the Addressables Groups window, Unity offers to convert all AssetBundles into Addressables groups. This is the easiest way to migrate your AssetBundle setup to the Addressables system. You must still update your runtime code to load and release assets using the Addressables API.

译:当你第一次打开Addressables Groups窗口时,Unity提供了将所有资产包转换为Addressables组的功能。这是将您的AssetBundle设置迁移到Addressables系统的最简单方法。您仍然必须更新运行时代码以使用Addressables API加载和释放资产。

If you want to convert your AssetBundle setup manually, click the Ignore button. The process for manually migrating your AssetBundles to Addressables is similar to that described for Scenes and the Resources folder:

译:如果您想手动转换您的AssetBundle设置,请单击Ignore按钮。手动迁移你的资产包到Addressables的过程类似于场景和资源文件夹的描述:

  1. Make the assets Addressable by enabling the Addressable option on each asset’s Inspector window or by dragging the asset to a group in the Addressables Groups window. The Addressables system ignores existing AssetBundle and Label settings for an asset.译:通过在每个资产的检查器窗口上启用可寻址选项或将资产拖到可寻址组窗口中的组中,使资产可寻址。Addressables系统忽略资产的现有AssetBundle和Label设置。

  2. Change any runtime code that loads assets using the AssetBundle or UnityWebRequestAssetBundle APIs to load them with the Addressables API. You don't need to explicitly load AssetBundle objects themselves or the dependencies of an asset; the Addressables system handles those aspects automatically.译:更改任何使用AssetBundle或UnityWebRequestAssetBundle API加载资产的运行时代码,以使用Addressables API加载它们。您不需要显式地加载AssetBundle对象本身或资产的依赖项;Addressables系统自动处理这些方面。

  3. Add code to release loaded assets when no longer needed.译:添加代码以在不再需要时释放加载的资产。

NOTE

The default path for the address of an asset is its file path. If you use the path as the asset's address, you'd load the asset in the same manner as you would load from a bundle. The Addressable Asset System handles the loading of the bundle and all its dependencies.

译:资产地址的默认路径为资产的文件路径。如果您使用路径作为资产的地址,您将以与从bundle加载相同的方式加载资产。可寻址资产系统(Addressable Asset System)处理bundle及其所有依赖项的加载。

If you chose the automatic conversion option or manually added your assets to equivalent Addressables groups, then, depending on your group settings, you end up with the same set of bundles containing the same assets. (The bundle files themselves won't be identical.) You can check for unwanted duplication and other potential issues using the Analyze tool. You can make sure that asset loading and unloading behaves as you expect using the [Event viewer] window.

译:如果您选择了自动转换选项,或者手动将资产添加到等效的Addressables组中,那么,根据您的组设置,您最终将得到包含相同资产的相同包集。(bundle文件本身不会完全相同。)您可以使用Analyze工具检查不必要的重复和其他潜在问题。您可以使用[Event viewer]窗口确保资产的加载和卸载行为与您期望的一样。

Files in StreamingAssets

You can continue to load files from the StreamingAssets folder when you use the Addressables system. However, files in this folder cannot be Addressable nor can files reference other assets in your project.

译:当您使用Addressables系统时,您可以继续从StreamingAssets文件夹加载文件。但是,此文件夹中的文件不能为可寻址文件,也不能引用项目中的其他资产。

The Addressables system does place its runtime configuration files and local AssetBundles in the StreamingAssets folder during a build. (Addressables removes these files at the conclusion of the build process; you won’t see them in the Editor.)

译:Addressables系统在构建过程中将其运行时配置文件和本地资产包放在StreamingAssets文件夹中。(Addressables在构建过程结束时删除这些文件;你不会在编辑器中看到它们。)



Unity_Addressable_升级到Addressables系统的评论 (共 条)

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