target | 目标构建平台。 |
targetGroup | 构建目标组。 |
namedBuildTarget | 目标命名构建平台。 |
bool 如果构建目标成功切换,则返回 true,否则返回 false(例如,如果许可证检查失败、文件丢失或用户通过 UI 取消了操作)。
选择一个新的构建目标作为活动目标。
更改活动构建目标时,此函数会重新导入受当前平台设置影响的资源,然后如果操作成功完成则返回 true。所有脚本文件将在下一个编辑器更新时进行编译。若要在重新导入资源之前编译脚本,请参阅 SwitchActiveBuildTargetAsync。
如果给定的目标是独立目标,则调用 SwitchActiveBuildTarget 也会影响 EditorUserBuildSettings.selectedStandaloneTarget。
注意:在以 批处理模式 运行编辑器时,此方法不可用。更改构建目标需要为给定目标重新编译脚本代码,这在脚本代码正在执行时无法完成。这在编辑器中不是问题,因为操作被延迟到下一个编辑器更新。但是,在批处理模式下,编辑器将在执行指定的脚本代码后停止,因此延迟操作是不可能的。要设置在批处理模式下使用的构建目标,请使用 buildtarget 命令行参数。
using UnityEditor;
public class SwitchPlatformExample { [MenuItem("Example/Switch Platform")] public static void PerformSwitch() { // Switch to Windows standalone build. EditorUserBuildSettings.SwitchActiveBuildTarget(BuildTargetGroup.Standalone, BuildTarget.StandaloneWindows); } }
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.