EditorUserBuildSettings.SwitchActiveBuildTargetAsync

切换到手册
public static bool SwitchActiveBuildTargetAsync (BuildTargetGroup targetGroup, BuildTarget target);

参数

targetGroup目标构建平台。
target构建目标组。

返回

bool 如果已成功切换构建目标,则为 true,否则为 false(例如许可证检查失败、文件丢失或用户通过 UI 取消了操作)。

描述

选择要在下一次编辑器更新期间激活的新构建目标。

在下一次编辑器更新期间,此函数将切换为作为活动构建进行传递的构建目标。效果等同于通过编辑器中的 Build Player 对话框更改构建目标。

在更改此函数返回的当前活动构建目标后,受到当前平台设置影响的所有脚本文件和资源都会在下一次编辑器更新时重新编译或重新导入。请参阅 SwitchActiveBuildTarget 了解如何立即导入资源。

如果给定目标是一个独立平台目标,则调用 SwitchActiveBuildTarget 还会影响 EditorUserBuildSettings.selectedStandaloneTarget

“注意”:在batch mode下运行编辑器时,此函数不可用,因为更改构建目标需要重新编译给定目标的脚本代码,而这在执行脚本代码时无法完成。这在编辑器中不是问题,因为操作会延迟。但是,批量模式会在执行过指定的脚本代码后立即退出。使用 buildTarget 命令行切换从批量模式中指定构建目标。

using UnityEngine;
using UnityEditor;
using UnityEditor.Build;

public class SwitchPlatformExample : IActiveBuildTargetChanged { [MenuItem("Example/Switch Platform")] public static void PerformSwitch() { // Switch to Windows standalone build. EditorUserBuildSettings.SwitchActiveBuildTargetAsync(BuildTargetGroup.Standalone, BuildTarget.StandaloneWindows); }

public int callbackOrder { get { return 0; } } public void OnActiveBuildTargetChanged(BuildTarget previousTarget, BuildTarget newTarget) { Debug.Log("Active platform is now " + newTarget); } }