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

Unity_Addressable_Build scripting(构建脚本)

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

There are a few ways in which you can use the Addressables API to customize your project build:

译:有几种方法可以使用Addressables API自定义项目构建:

  • Start a build from a script译:从脚本开始构建

  • Override an existing script译:覆盖现有脚本

  • Extend BuildScriptBase or implement IDataBuilder译:扩展BuildScriptBase或实现IDataBuilder

When you customize a build script to handle different asset types or handle assets in a different way, you might also need to customize the [Play Mode Scripts] so that the Editor can handle those assets in the same way during Play mode.

译:当您自定义构建脚本以处理不同类型的资产或以不同方式处理资产时,您可能还需要自定义[Play Mode Scripts],以便在Play模式下编辑器可以以同样的方式处理这些资产。

Starting an Addressables build from a script

To start an Addressables build from another script, call the AddressableAssetSettings.BuildPlayerContent method.

译:要从另一个脚本开始Addressables构建,请调用AddressableAssetSettings.BuildPlayerContent方法。

Before starting the build, you should set the active Profile and the active build script. You can also set a different AddressableAssetSettings object than the default, if desired.

译:在开始构建之前,您应该设置活动配置文件和活动构建脚本。如果需要,您还可以设置一个与默认值不同的AddressableAssetSettings对象。

There are a few pieces of information that BuildPlayerContent takes into consideration when performing the build: the AddressableAssetSettingsDefaultObject, ActivePlayerDataBuilder, and the addressables_content_state.bin file.

译:BuildPlayerContent在执行构建时考虑了一些信息:AddressableAssetSettingsDefaultObject,ActivePlayerDataBuilder和addressablescontentstate.bin文件。

Set the AddressableAssetSettings

The settings defined by AddressableAssetSettings include the list of groups and the profile to use.

译:AddressableAssetSettings定义的设置包括要使用的组和配置文件列表。

To access the settings that you see in the Editor (menu: Window > Asset Management > Addressables > Settings), use the static AddressableAssetSettingsDefaultObject.Settings property. However, if desired, you can use a different settings object for a build.

译:要访问您在编辑器中看到的设置(菜单:Window> Asset Management> Addressables> Settings),请使用静态AddressableAssetSettingsDefaultObject.Settings属性。但是,如果需要,您可以为构建使用不同的设置对象。

To load a custom settings object in a build:

译:要在构建中加载自定义设置对象:

Set the active Profile

A build started with BuildContent uses the variable settings of the active Profile. To set the active Profile as part of your customized build script, assign the ID of the desired profile to the activeProfileId field of the AddressableAssetSettingsDefaultObject.Settings object.

译:要设置活动配置文件,请使用BuildContent启动的构建使用活动配置文件的变量设置。为了在自定义构建脚本中设置活动配置文件,将所需配置文件的ID分配给AddressableAssetSettingsDefaultObject.Settings对象的activeProfileId字段。

The AddressableAssetSettings object contains the list of profiles. Use the name of the desired profile to look up its ID value and then assign the ID to the activeProfileId variable:

译:AddressableAssetSettings对象包含配置文件列表。使用所需配置文件的名称查找其ID值,然后将ID分配给activeProfileId变量:

Set the active build script

The BuildContent method launches the build based on the current ActivePlayerDataBuilder setting. To use a specific build script, assign the index of the IDataBuilder object in the AddressableAssetSetting.DataBuilders list to the [ActivePlayerDataBuilderIndex] property.

译:BuildContent方法根据当前的ActivePlayerDataBuilder设置启动构建。要使用特定的构建脚本,请将AddressableAssetSetting.DataBuilders列表中的IDataBuilder对象的索引分配给[ActivePlayerDataBuilderIndex]属性。

The build script must be a ScriptableObject that implements IDataBuilder and you must add it to the DataBuilders list in the AddressableAssetSettings instance. Once added to the list, use the standard List.IndexOf method to get the index of the object.

译:构建脚本必须是实现IDataBuilder的ScriptableObject,并且必须将其添加到AddressableAssetSettings实例的DataBuilders列表中。添加到列表后,请使用标准的List.IndexOf方法获取对象的索引。

Launch a build

After setting the profile and builder to use (if desired), you can launch the build:

译:在将概要文件和构建器设置为使用之后(如果需要的话),您可以启动构建。

To check for success, use BuildPlayerContent(out AddressablesPlayerBuildResult result). result.Error contains any error message returned if the Addressables build failed. If string.IsNullOrEmpty(result.Error) is true, the build was successful.

译:要检查是否成功,请使用BuildPlayerContent(out AddressablesPlayerBuildResult result)。如果Addressables构建失败,则result.Error包含任何错误消息。如果string.IsNullOrEmpty(result.Error)为true,则构建成功。

Example script to launch build

The following example adds a couple of menu commands to the Asset Management > Addressables menu in the Editor. The first command builds the Addressable content using the preset profile and build script. The second command builds the Addressable content, and, if it succeeds, builds the Player, too.

译:以下示例将一些菜单命令添加到编辑器中的Asset Management>Addressables菜单中。第一个命令使用预设配置文件和构建脚本构建Addressable内容。第二个命令构建Addressable内容,并且如果成功,则也构建Player。

Note that if your build script makes setting changes that require a domain reload, you should run the build script using Unity command line options, instead of running it interactively in the Editor. See Domain reloads and Addressable builds for more information.

译:请注意,如果您的构建脚本进行了需要重新加载域的设置更改,则应使用Unity命令行选项运行构建脚本,而不是在编辑器中进行交互式运行。有关更多信息,请参见重新加载域和Addressable构建。

Domain reloads and Addressables builds

If your scripted build process involves changing settings that trigger a domain reload before it makes an Addressables build, then you should script such builds to use Unity's command line arguments rather than interactively running a script in the Editor. These types of settings include:

译:如果您的脚本化构建过程涉及更改设置,会在进行Addressables构建之前触发重新加载域,则应该编写脚本以使用Unity的命令行参数,而不是在编辑器中交互式运行脚本。这些类型的设置包括:

  • Changing the defined compiler symbols译:更改定义的编译器符号

  • Changing platform target or target group译:更改平台目标或目标组

When you run a script that triggers a domain reload interactively in the Editor (using a menu command, for example), your Editor script finishes executing before the domain reload occurs. Thus, if you immediately start an Addressables build, both your code and imported assets are still in their original state. You must wait for the domain reload to complete before you start the content build.

译:当您交互式在编辑器中运行触发重新加载域的脚本(例如使用菜单命令)时,您的编辑器脚本在重新加载域发生之前就完成执行了。因此,如果您立即启动Addressables构建,则您的代码和导入的资产仍处于其原始状态。在开始内容构建之前,您必须等待重新加载域完成。

Waiting for the domain reload to finish is relatively straightforward when you run the build from the command line, but can be difficult or impossible to accomplish reliably in an interactive script (for a variety of reasons).

译:当您从命令行运行构建时,等待重新加载域完成相对简单,但在交互式脚本中可靠地完成可能很难或不可能(由于各种原因)。

The following example script defines two functions that can be invoked when running Unity on the command line. The ChangeSettings example sets the specified define symbols. The BuildContentAndPlayer function runs the Addressables build and the Player build.

译:以下示例脚本定义了两个函数,可以在运行Unity时调用。ChangeSettings示例设置指定的定义符号。 BuildContentAndPlayer函数运行Addressables构建和Player构建。

To call these functions, use Unity's command line arguments in a terminal or command prompt or in a shell script:

译:以下示例脚本定义了两个函数,可以在运行Unity时调用。ChangeSettings示例设置指定的定义符号。 BuildContentAndPlayer函数运行Addressables构建和Player构建。

NOTE

If you specify the platform target as a command line parameter, you can perform an Addressables build in the same command. However, if you wanted to change the platform in a script, you should do it in a separate command, such as the ChangeSettings function in this example.

译:如果将平台目标指定为命令行参数,则可以在同一命令中执行Addressables构建。但是,如果要在脚本中更改平台,则应在单独的命令中执行此操作,例如此示例中的ChangeSettings函数。

Custom Build Scripting

To configure a new custom script, add it to the Build and Play Mode Scripts list.

译:要配置新的自定义脚本,请将其添加到Build and Play Mode Scripts列表中。

Custom scripts extend the BuildScriptBase class or implement the IDataBuilder interface. There are several overridable methods, such as ClearCachedData and CanBuildData<T>. If extending the BuildScriptBase class, the most notable method to override is BuildDataImplementation<TResult>. This is the method that is used to setup or build content.

译:自定义脚本扩展BuildScriptBase类或实现IDataBuilder接口。有几种可重写的方法,例如ClearCachedData和CanBuildData。如果扩展BuildScriptBase类,则要重写的最重要的方法是BuildDataImplementation。这是用于设置或构建内容的方法。

A custom script is either a Build Script or a Play Mode Script. This is determined by how the CanBuildData<T> method is implemented. Build scripts can only build data of the type AddressablesPlayerBuildResult, so the method is implemented in this way:

译:自定义脚本是构建脚本或播放模式脚本。这取决于CanBuildData方法的实现方式。构建脚本只能构建AddressablesPlayerBuildResult类型的数据,因此该方法是以这种方式实现的:

This allows the script to be listed in the Build/New Build/ menu.

译:这允许在Build / New Build /菜单中列出脚本。

Play Mode Scripts can only build data of the type AddressablesPlayModeBuildResult, so the method is implemented in this way:

译:播放模式脚本只能构建AddressablesPlayModeBuildResult类型的数据,因此该方法是以下方式实现的:

This allows the script to be listed in the Play Mode Scripts menu.

译:这允许在播放模式脚本菜单中列出脚本。

See the Custom Build and Playmode Scripts Sample for an example.

译:有关示例,请参见Custom Build and Playmode Scripts示例。

Extend the default build script

If you want to use the same basic build as the default build script BuildScriptPackedMode, but want to treat specific groups or types of assets differently, you can extend the default build script and override the functions within it. If the group or asset the build script is processing is one that you want to treat differently, you can run your own code, otherwise you can call the base class version of the function to use the default algorithm.

译:如果要使用与默认构建脚本BuildScriptPackedMode相同的基本构建,但要对特定组或类型的资产进行不同的处理,则可以扩展默认构建脚本并覆盖其中的函数。如果构建脚本正在处理的组或资产是您想要不同处理的组或资产,则可以运行自己的代码,否则可以调用该函数的基类版本以使用默认算法。

See the Addressable variants project for an example.

译:有关示例,请参见Addressable variants项目。

Save the content state

If you support remote content distribution and update your content between player releases, you must record the state of your Addressables groups at the time of the build. Recording the state allows you to perform a differential build using the Update a Previous Build script.

译:如果支持远程内容分发并在播放器发布之间更新内容,则必须记录构建时Addressables组的状态。记录状态允许您使用Update a Previous Build脚本执行差分构建。

See the implementation of BuildScriptPackedMode and ContentUpdateScript, for details.

译:有关详细信息,请参见BuildScriptPackedMode和ContentUpdateScript的实现。

Can I build Addressables when recompiling scripts?

If you have a pre-build step that triggers a domain reload, then you must take special care that the Addressables build itself does not start until after the domain reload is finished.

译:

如果有预构建步骤触发域重新加载,那么必须特别注意在域重新加载完成之后才启动Addressables构建本身。

Using methods such as setting scripting define symbols (PlayerSettings.SetScriptingDefineSymbolsForGroup) or switching active build target (EditorUserBuildSettings.SwitchActiveBuildTarget), triggers scripts to recompile and reload.

译:使用设置脚本定义符号(PlayerSettings.SetScriptingDefineSymbolsForGroup)或切换活动构建目标(EditorUserBuildSettings.SwitchActiveBuildTarget)等方法会触发脚本重新编译和重新加载。

The execution of the Editor code will continue with the currently loaded domain until the domain reloads and execution stops. Any platform dependent compilation or custom defines will not be set until after the domain reloads. This can lead to unexpected issues where code relies on these defines to build correctly, and can be easily missed.

译:编辑器代码的执行将继续使用当前加载的域,直到重新加载域并停止执行为止。任何平台相关的编译或自定义定义都不会在域重新加载之前设置。这可能会导致意外问题,其中代码依赖于这些定义来正确构建,并且很容易忽略。

Is there a safe way to change scripts before building?

To switch Platform, or modify Editor scripts in code and then continue with the defines set, a domain reload must be performed. Note in this case, -quit argument should not be used or the Editor will exit immediately after execution of the invoked method.

译:要切换平台或在代码中修改编辑器脚本,然后继续使用已设置的定义,必须执行域重新加载。请注意,在这种情况下,不应使用-quit参数,否则编辑器将在调用的方法执行后立即退出。

When the domain reloads, InitializeOnLoad is invoked. The code below demonstrates how to set scripting define symbols and react to those in the Editor code, building Addressables after the domain reload completes. The same process can be done for switching platforms and platform dependent compilation.

译:重新加载域时,将调用InitializeOnLoad。下面的代码演示了如何设置脚本定义符号并在域重新加载完成后构建Addressables。可以使用相同的过程来切换平台和平台相关的编译。


Unity_Addressable_Build scripting(构建脚本)的评论 (共 条)

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