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); } }
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.