首页 >头条 > 正文

unity_addressable_入门指南

2023-03-28 11:06:12来源:哔哩哔哩

Once you have installed the Addressables package in your Unity Project, you can get started.

译:一旦你在Unity项目中安装了Addressables包,你就可以开始了。


(资料图片)

The basic steps to using Addressables include:

译:使用Addressables的基本步骤包括:

Make your assets Addressable 译:让你的资产具有可寻址性

Reference and load those assets in code using the Addressables API 译:使用Addressables API在代码中引用和加载这些资产

Build your Addressable assets 译:构建你的Addressable 

See the Space Shooter project in the Addressables-Sample repository for an example of a project set up to use Addressable assets.

译:请参阅Addressable - sample存储库中的Space Shooter项目,以获得设置为使用Addressable资产的项目示例。

NOTE

This Getting Started topic doesn't discuss the various ways you can organize your Addressable content. For information on that topic, see Organizing Addressable Assets.

译:本入门主题不讨论组织可寻址内容的各种方法。有关该主题的信息,请参见组织可寻址资产。

Installation

To install the Addressables package in your project, use the Unity Package Manager

译:要在项目中安装Addressables包,请使用Unity包管理器

Open the Package Manager (menu: Window > Package Manager).译:Window > Package Manager

Set the package list to display packages from the Unity Registry.译:设置包列表来显示来自Unity Registry的包。

3 .Select the Addressables package in the list.译:在列表中选择Addressables包

4.Click Install(at the bottom, right-hand side of the Package Manager window).译:单击Install(在Package Manager窗口的底部,右边)。

To set up the Addressables system in your Project after installation, open the Addressables Groupswindow and click Create Addressables Settings.

译:要在安装后在项目中设置Addressables系统,请打开Addressables组窗口并单击Create Addressables Settings。

When you run the Create Addressables Settingscommand, the Addressables system creates a folder called, AddressableAssetsData, in which it stores settings files and other assets it uses to keep track of your Addressables setup. You should add the files in this folder to your source control system. Note that Addressables can create additional files as you change your Addressables configuration. See Addressables Settings for more information about the settings themselves.

译:当您运行Create Addressables Settings命令时,Addressables系统创建了一个名为AddressableAssetsData的文件夹,其中存储了用于跟踪Addressables设置的设置文件和其他资产。您应该将此文件夹中的文件添加到您的源代码控制系统中。注意,Addressables可以在您更改Addressables配置时创建额外的文件。有关设置本身的更多信息,请参阅Addressables设置。

NOTE

For instructions on installing a specific version of Addressables or for general information about managing the packages in a Project, see Packages.

译:有关安装Addressables的特定版本的说明或有关在项目中管理包的一般信息,请参阅包。

Making an asset Addressable

You can mark an asset as Addressable in the following ways:

译:您可以通过以下方式将资产标记为可寻址:

Check the Addressablebox in the asset's Inspector:译:勾选Addressable选项在Inspector面板中

Drag or assign the asset to an AssetReference field in an Inspector:译:将资产拖拽或分配到检查器中的AssetReference字段

Drag the asset into a group on the Addressables Groupswindow:译:在“Addressables Groups”窗口上将资产拖到一个组中

Put the asset in a Project folder that's marked as Addressable:译:将放入Project文件夹中的资产标记为Addressable

Once you make an asset Addressable, the Addressables system adds it to a default group (unless you place it in a specific group). Addressables packs assets in a group into AssetBundles according to your group settings when you make a content build. You can load these assets using the Addressables API.

译:一旦你将一个资产设置为Addressable, Addressables系统就会将它添加到一个默认组中(除非你将它放在一个特定的组中)。在构建内容时,Addressables根据组设置将组中的资产打包到AssetBundles中。您可以使用Addressables API加载这些资产。

NOTE

If you make an asset in a Resources folder Addressable, Unity moves the asset out of the Resources folder. You can move the asset to a different folder in your Project, but you cannot store Addressable assets in a Resources folder.

译:如果你让资源文件夹中的资产可寻址,Unity将资产移出资源文件夹。您可以将资产移动到Project中的另一个文件夹中,但不能将可寻址资产存储在Resources文件夹中。

Using an Addressable Asset

To load an Addressable Asset, you can:

译:加载一个可寻址资产,你可以:

Use an AssetReference referencing the asset译:使用一个AssetReference引用资产

Use its address string译:使用它的地址字符串

Use a label assigned to the asset译:使用标签打包资产

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

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

Loading Addressable assets uses asynchronous operations. See Operations for information about the different ways to tackle asynchronous programming in Unity scripts.

译:加载可寻址资产使用异步操作。关于在Unity脚本中处理异步编程的不同方法,请参阅操作。

TIP

You can find more involved examples of how to use Addressable assets in the Addressables-Sample repo.

译:您可以在Addressable - sample涉及更多有关如何使用Addressable资产的示例。

Using AssetReferences

To use an AssetReference, add an AssetReference field to a MonoBehaviour or ScriptableObject. After you create an object of that type, you can assign an asset to the field in your object's Inspector window.

译:为了使用一个AssetReference,在MonoBehaviour或ScriptableObject中添加一个AssetReference字段。创建该类型的对象后,可以将资产分配给对象的Inspector窗口中的字段。

NOTE

If you assign a non-Addressable asset to an AssetReference field, Unity automatically makes that asset Addressable and adds it to your default Addressables group. AssetReferences also let you use Addressable assets in a Scene that isn't itself Addressable.

译:如果你将一个非可寻址资产分配给一个AssetReference字段,Unity会自动使该资产可寻址,并将其添加到你的默认Addressables组中。资产引用还允许你在一个场景中使用本身不是可寻址的可寻址资产。

Unity does not load or release the referenced asset automatically; you must load and release the asset using the Addressables API:

译:Unity不会自动加载或释放引用的资产;你必须使用Addressables API加载和释放资产:

See Loading an AssetReference for additional information about loading AssetReferences.

译:有关加载资产引用的其他信息,请参见加载资产引用。

Loading by address

You can use the address string to load an Asset:

译:你可以使用地址字符串来加载资产:

Remember that every time you load an Asset, you must also release it.

译:请记住,每当您加载一个资产时,您也必须释放它。

See Loading a single asset for more information.

译:有关更多信息,请参见加载单个资产。

Loading by label

You can load sets of assets that have the same label in one operation:

译:您可以在一个操作中加载具有相同标签的资产集:

See Loading multiple assets for more information.

译:有关更多信息,请参见加载多个资产。

Managing Addressable assets

To manage your Addressable assets, use the Addressables Groupswindow. Use this window to create Addressables groups, move assets between groups, and assign addresses and labels to assets.

译:要管理你的Addressable 资源,请使用“Addressables Groups”窗口。使用此窗口可创建Addressables组,在组之间移动资产,并为资产分配地址和标签。

When you first install and set up the Addressables package, it creates a default group for Addressable assets. The Addressables system assigns any assets you mark as Addressable to this group by default. In the early stages of a Project, you might find it acceptable to keep your assets in this single group, but as you add more content, you should consider creating additional groups so that you have better control over which resources your application loads and keeps in memory at any given time.

译:当您第一次安装和设置Addressables包时,它会为Addressable资产创建一个默认组。Addressables系统默认将您标记为Addressable的任何资产分配给该组。在项目的早期阶段,您可能会发现将资产保留在这个单独的组中是可以接受的,但是随着您添加更多的内容,您应该考虑创建额外的组,以便您可以更好地控制应用程序在任何给定时间加载和保存在内存中的资源。

Key group settings include:译:关键组组设置包括

Build path: where to save your content after a content build.译:构建路径:内容构建后保存内容的位置。

Load path: where your app or game looks for built content at runtime.译:你的应用或游戏在运行时在哪里寻找构建内容

NOTE

You can (and usually should) use Profile variables to set these paths. See Profiles for more information.

译:您可以(通常应该)使用Profile变量来设置这些路径。有关更多信息,请参阅概要文件。

Bundle mode: how to package the content in the group into a bundle. You can choose the following options:译:Bundle模式:如何将组中的内容打包成一个Bundle。您可以选择以下选项

One bundle containing all group assets译:一个包含所有组资产的包

A bundle for each entry in the group (particularly useful if you mark entire folders as Addressable and want their contents built together)译:组中每个条目的捆绑包(如果您将整个文件夹标记为Addressable并希望将其内容构建在一起,则特别有用)

A bundle for each unique combination of labels assigned to group assets译:分配给组资产的每个唯一标签组合的包

Content update restriction: Setting this value appropriately allows you to publish smaller content updates. See Content update builds for more information. If you always publish full builds to update your app and don't download content from a remote source, you can ignore this setting.译:内容更新限制:适当地设置这个值可以让您发布较小的内容更新。有关更多信息,请参阅内容更新构建。如果你总是发布完整的版本来更新你的应用程序,并且不从远程源下载内容,你可以忽略这个设置。

For more information on strategies to consider when deciding how to organize your assets, see Organizing Addressable assets.

译:有关在决定如何组织资产时要考虑的策略的更多信息,请参阅组织可寻址资产。

For more information on using the Addressables Groups window, see Groups.

译:有关使用“可寻址组”窗口的详细信息,请参见组。

Building Addressable assets

The Addressables content build step converts the assets in your Addressables groups into AssetBundles based on the group settings and the current platform set in the Editor.

译:Addressables内容构建步骤将Addressables组中的资产转换为基于组设置和编辑器中设置的当前平台的资产包。

In Unity 2021.2+, you can configure the Addressables system to build your Addressables content as part of every Player build or you can build your content separately before making a Player build. See Building Addressables content with Player builds for more information about configuring these options.

译:在Unity 2021.2以上版本,你可以配置Addressables系统来构建你的Addressables内容作为每个播放器构建的一部分,或者你可以在制作播放器构建之前单独构建你的内容。有关配置这些选项的更多信息,请参见与播放器构建构建可寻址内容。

If you configure Unity to build your content as pa    build, use the normal Buildor Build and Runbuttons on the Editor Build Settings window to start a build. Unity builds your Addressables content as a pre-build step before it builds the Player.

译:如果你将Unity配置为将内容构建为播放器构建的一部分,请使用编辑器构建设置窗口中的正常构建或构建并运行按钮来开始构建。Unity在构建播放器之前构建你的Addressables内容作为预构建步骤。

In earlier versions of Unity, or if you configure Unity to build your content separately, you must make an Addressables build using the Buildmenu on the Addressables Groupswindow as described in [Making builds]. The next time you build the Player for your project, it uses the artifacts produced by the last Addressables content build run for the current platform. See [Build scripting] for information about automating your Addressables build process.

译:在早期的Unity版本中,或者如果你配置Unity来单独构建你的内容,你必须使用[制作构建]中描述的Addressables Groups上的Build菜单进行可寻址构建。下次为项目构建Player时,它将使用上次为当前平台运行的Addressables内容构建所生成的构件。有关自动化Addressables构建过程的信息,请参阅[构建脚本]。

To initiate a content build from the Addressables Groups window:

译:要从Addressables Groups窗口启动内容构建:

Open the Addressables Groups window (menu: Windows > Asset Management > Addressables > Groups).译:打开 Addressables Groups窗口,Windows > Asset Management > Addressables > Groups

Choose an option from the Buildmenu:译:从“构建”菜单中选择一个选项:

New Build: perform a build with a specific build script. Use the Default Build Scriptif you don't have your own custom one.译:新构建:使用特定的构建脚本执行构建。如果您没有自己的自定义构建脚本,请使用(Default Build Script)默认构建脚本

Update a Previous Build: builds an update based on an existing build. To update a previous build, the Addressables system needs译:更新以前的构建:基于现有的构建构建一个更新。要更新以前的版本,Addressables系统需要 the addressables_content_state.binfile produced by the earlier build. You can find this file in the Assets/AddressableAssetsData/Platformfolder of your Unity Project. See Content Updates for more information about updating content.译:由早期构建生成的addressables_content_state.bin文件。你可以在Unity项目的Assets/AddressableAssetsData/Platform文件夹中找到这个文件。有关更新内容的更多信息,请参阅内容更新。

Clean Build: deletes cached build files.译:Clean Build:删除缓存的构建文件

By default, the build creates files in the locations defined in your Profile settings for the LocalBuildPathand RemoteBuildPathvariables. The files that Unity uses for your player builds include AssetBundles (.bundle), catalog JSON and hash files, and settings files.

译:默认情况下,构建在配置文件设置中为LocalBuildPath和RemoteBuildPath变量定义的位置中创建文件。Unity用于你的用户构建的文件包括AssetBundles (.bundle),目录JSON和哈希文件,以及设置文件。

WARNING

In most cases, you should not change the local build or load paths from their default values. If you do, you must copy the local build artifacts from your custom build location to the project's StreamingAssets folder before making a Player build. Altering these paths also precludes building your Addressables as part of the Player build.

译:在大多数情况下,不应更改本地构建或加载路径的默认值。如果你这样做,你必须复制本地构建工件从您的自定义构建位置到项目的StreamingAssets文件夹,然后再进行用户构建。改变这些路径也排除了将你的Addressables构建为Player构建的一部分。

If you have groups that you build to the RemoteBuildPath, it is your responsibility to upload those AssetBundles, catalog, and hash files to your hosting server. (If your Project doesn't use remote content, set all groups to use the local build and load paths.)

译:如果你有组,你建立到RemoteBuildPath,这是你的责任,上传这些资产包,目录,和哈希文件到你的主机服务器。(如果项目不使用远程内容,请将所有组设置为使用本地构建和加载路径。)

A content build also creates the following files that Addressables doesn't use directly in a player build:

译:内容构建也会创建以下文件,Addressables不会直接在用户构建中使用:

addressables_content_state.bin: used to make a content update build. If you support dynamic content updates, you must save this file after each content release. Otherwise, you can ignore this file.译:addressables_content_state.bin:用于制作内容更新构建。如果支持动态内容更新,则必须在每次内容发布后保存此文件。否则,您可以忽略该文件

AddressablesBuildTEP.json: logs build performance data. See Build Profiling.译:AddressablesBuildTEP.json:日志构建性能数据。参见构建剖析

See Building Addressable content for more information about how to set up and perform a content build.

译:有关如何设置和执行内容构建的详细信息,请参见构建可寻址内容。

Starting a full content build

To make a full content build:

译:要制作完整的内容构建:

Set the desired Platform Targeton the Build Settingswindow.译:在Build Settings窗口设置目标平台

Open the Addressables Groupswindow (menu: Asset Management > Addressables > Groups).译:打开Addressables Groups窗口,Asset Management > Addressables > Groups

Choose the__ New Build > Default Build Script__ command from the Build menu of the Groupswindow.译:在Groups中选择New Build Default Build Script

The build process starts.

译:构建过程开始。

After the build is complete, you can perform a player build and upload any remote files from your RemoteBuildPathto your hosting server.

译:构建完成后,您可以执行用户构建,并将任何远程文件从您的RemoteBuildPath上传到您的托管服务器。

IMPORTANT

If you plan to publish remote content updates without rebuilding your application, you must preserve the addressables_content_state.binfile for each published build. Without this file, you can only create a full content build and player build, not an update. See Content update builds for more information.

译:如果您计划在不重新构建应用程序的情况下发布远程内容更新,则必须为每个发布的构建保留addressables_content_state.bin文件。没有这个文件,你只能创建一个完整的内容构建和用户构建,而不是更新。有关更多信息,请参阅内容更新构建。

Remote content distribution

You can use Addressables to support remote distribution of content through a Content Delivery Network (CDN) or other hosting service. Unity provides the Unity Cloud Content Delivery (CCD) service for this purpose, but you can use any CDN or host you prefer.

译:您可以使用Addressables通过内容分发网络(CDN)或其他托管服务来支持内容的远程分发。Unity为此提供了Unity云内容交付(CCD)服务,但你可以使用任何你喜欢的CDN或主机。

Before building content for remote distribution, you must:

译:在构建用于远程分发的内容之前,您必须:

Enable the Build Remote Catalogoption in your AddressableAssetSettings (access using menu: Windows > Asset Management > Addressables > Settings).译:在AddressableAssetSettings(使用菜单访问)中启用“生成远程目录”选项, Windows > Asset Management > Addressables > Settings

Configure the RemoteLoadPathin the Profile you use to publish content to reflect the remote URL at which you plan to access the content.译:在概要文件中配置用于发布内容的Remotelloadpath,以反映计划访问内容的远程URL

For each Addressables group containing assets you want to deliver remotely, set the Build Pathto RemoteBuildPathand the Load Pathto RemoteLoadPath.译:对于每个包含您想要远程交付的资产的Addressables组,将构建路径设置为RemoteBuildPath,将加载路径设置为RemoteLoadPath。

Set desired Platform Targeton the Unity Build Settingswindow.译:在Unity Build Settings窗口中设置所需的平台目标。

After you make a content build (using the Addressables Groupswindow) and a player build (using the Build Settingswindow), you must upload the files created in the folder designated by your profile's RemoteBuildPathto your hosting service. The files to upload include:

译:在创建内容构建(使用Addressables Groups窗口)和用户构建(使用构建设置窗口)后,必须将在配置文件的RemoteBuildPath指定的文件夹中创建的文件上传到托管服务。需要上传的文件包括:

AssetBundles (name.bundle)译:

Catalog (catalog_timestamp.json)译:

Hash (catalog_timestamp.hash)译:

See Distributing remote content for more information.

译:有关更多信息,请参阅分发远程内容。

Incremental content updates

When you distribute content remotely, you can reduce the amount of data your users need to download for an update by publishing incremental content update builds. An incremental update build allows you to publish remote bundles which contain only the assets that have changed since you last published an update rather than republishing everything. The assets in these smaller, updated bundles override the existing assets.

译:当您远程分发内容时,您可以通过发布增量内容更新构建来减少用户为更新需要下载的数据量。增量更新构建允许您发布远程包,其中只包含自上次发布更新以来发生变化的资产,而不是重新发布所有内容。这些较小的更新包中的资产覆盖现有资产。

IMPORTANT

You must turn on the Build Remote Catalog option before you publish a player build if you want to have the option to publish incremental updates. Without a remote catalog, an installed application doesn't check for updates.

译:如果您希望有发布增量更新的选项,则必须在发布用户构建之前打开生成远程目录选项。如果没有远程编目,已安装的应用程序将不会检查更新。

For more detailed information about content updates, including examples, see Content update builds.

译:有关内容更新的详细信息(包括示例),请参见内容更新构建。

Starting a content update build

To make a content update, rather than a full build:

译:要进行内容更新,而不是完整构建:

On the Build Settingswindow, set the Platform Targetto match the target of the previous content build that you are now updating.译:在“生成设置”窗口中,将平台目标设置为与您现在正在更新的上一个内容生成的目标相匹配。

Open the Addressables Groupswindow (menu: Asset Management > Addressables > Groups).译:打开Addressables Groups窗口 通过Asset Management > Addressables > Groups

From the Toolsmenu, run the Check for Content Update Restrictionscommand. The Build Data Filebrowser window opens.译:通过Tools菜单,运行Check for Content Update Restrictions命令Build Data File会在文件夹中打开

Locate the addressables_content_state.binfile produced by the previous build. This file is in a subfolder of Assets/AddressableAssestsDatanamed for the target platform.译:找到上一个版本生成的addressables_content_state.bin文件。该文件位于以目标平台命名的Assets/AddressableAssestsData的子文件夹中。

Click Open. The Content Update Previewwindow searches for changes and identifies assets that must be moved to a new group for the update. If you have not changed any assets in groups set to "Cannot Change Post Release," then no changes will be listed in the preview. (When you change an asset in a group set to "Can Change Post Release," then Addressables rebuilds all the AssetBundles for the group; Addressables does not move the changed assets to a new group in this case.)译:单击Open。“Content Update Preview”窗口搜索更改并识别必须移动到新组以进行更新的资产。如果您没有更改组中的任何资产,设置为“不能更改发布后”,则预览中不会列出任何更改。(当你将一个组中的资产设置为“Can change Post Release”时,Addressables会为这个组重新构建所有的资产包;在这种情况下,Addressables不会将更改的资产移动到新的组中。)

Click Apply Changesto accept any changes.译:点击Apply Changes来改变所有更改

From the Buildmenu, run the__ Update a Previous Build__ command.译:从Build菜单中运行Update a Previous Build命令

Open the addressables_content_state.binfile produced by the previous build.译:打开上一个构建生成的addressables_content_state.bin文件.

The build process starts.

译:

After the build is complete, you can upload the files from your RemoteBuildPathto your hosting server.

译:构建过程开始。

IMPORTANT

Addressables uses the addressables_content_state.binfile to identify which assets you changed. You must preserve a copy of this file for each published build. Without the file, you can only create a full content build, not an update.

译:Addressables使用addressables_content_state.bin文件来识别您更改了哪些资产。您必须为每个发布的版本保留该文件的副本。如果没有该文件,您只能创建完整的内容构建,而不能创建更新。

责任编辑:

标签:

免责声明

头条新闻