Unity’s support for the C# language includes the use of directives, which allow you to selectively include or exclude code from compilation, based on whether certain scripting symbols are defined or not defined.
You can read more broadly about how these directives work in C# on the Microsoft C# preprocessor directives page.
Unity has a range of built-in scripting symbols which represent options that you can use in your scripts to selectively include or exclude portions of code from compilation.
For example, the built-in scripting symbol that is set when a player is built for Windows standalone platform is UNITY_STANDALONE_WIN
. You can check whether this symbol is defined using a special type of if statement, as follows:
#if UNITY_STANDALONE_WIN
Debug.Log("Standalone Windows");
#endif
The hash (#
) character in front of the if
and endif
indicates that these statements are “directives”, and are handled during the compilation process, rather than at runtime.
Therefore in the above example, the Debug line is only included for compilation in the Windows standalone build of the project. It is omitted entirely when compiled in the Editor, or in other target builds. This is different to using a regular if .. then .. else
structure, which would only bypass the execution of certain portions of code at run time. See further below for full examples.
There are a number of built-in scripting symbols which allow you to selectively compile or omit code based on the selected Platform, the Editor Version, and other miscellaneous system environment scenarios. These built-in scripting symbols are listed below.
In addition, you can define your own scripting symbols either using the Editor UI, via scripting, or via an asset file, which allows you to control compilation of portions of your code based on arbitrary definitions. See Custom scripting symbols for more information.
Note: Scripting symbols are sometimes referred to as “define symbols”, “preprocessor defines”, or just “defines”.
Unity automatically defines certain scripting symbols based on the authoring and build target platform. These are as follows:
Define | 功能 |
---|---|
UNITY_EDITOR | Scripting symbol to call Unity Editor scripts from your game code. |
UNITY_EDITOR_WIN | Scripting symbol for Editor code on Windows. |
UNITY_EDITOR_OSX | Scripting symbol for Editor code on Mac OS X. |
UNITY_EDITOR_LINUX | Scripting symbol for Editor code on Linux. |
UNITY_STANDALONE_OSX | Scripting symbol to compile or execute code specifically for Mac OS X (including Universal, PPC and Intel architectures). |
UNITY_STANDALONE_WIN | Scripting symbol for compiling/executing code specifically for Windows standalone applications. |
UNITY_STANDALONE_LINUX | Scripting symbol for compiling/executing code specifically for Linux standalone applications. |
UNITY_STANDALONE | Scripting symbol for compiling/executing code for any standalone platform (Mac OS X, Windows or Linux). |
UNITY_WII | Scripting symbol for compiling/executing code for the Wii console. |
UNITY_IOS | Scripting symbol for compiling/executing code for the iOS platform. |
UNITY_IPHONE | 已弃用。改用 UNITY_IOS。 |
UNITY_ANDROID | Scripting symbol for the Android platform. |
UNITY_LUMIN | Scripting symbol for the Magic Leap OS platform. You can also use PLATFORM_LUMIN. |
UNITY_TIZEN | Scripting symbol for the Tizen platform. |
UNITY_TVOS | Scripting symbol for the Apple TV platform. |
UNITY_WSA | Scripting symbol for Universal Windows Platform. Additionally, NETFX_CORE is defined when compiling C# files against .NET Core and using .NET scripting backend. |
UNITY_WSA_10_0 | Scripting symbol for Universal Windows Platform. Additionally WINDOWS_UWP is defined when compiling C# files against .NET Core. |
UNITY_WEBGL | Scripting symbol for WebGL. |
UNITY_FACEBOOK | Scripting symbol for the Facebook platform (WebGL or Windows standalone). |
UNITY_ANALYTICS | Scripting symbol for calling Unity Analytics methods from your game code. Version 5.2 and above. |
UNITY_ASSERTIONS | Scripting symbol for assertions control process. |
UNITY_64 | Scripting symbol for 64-bit platforms. |
Unity automatically defines certain scripting symbols based on the version of the Editor that you are currently using.
Given a version number X.Y.Z (for example, 2019.4.14), Unity exposes three global scripting symbols in the following formats: UNITY_X, UNITY_X_Y and UNITY_X_Y_Z.
Here is an example of scripting symbols exposed in Unity 2019.4.14:
Define | 功能 |
---|---|
UNITY_2019 | Scripting symbol for the release version of Unity 2019, exposed in every 2019.Y.Z release. |
UNITY_2019_4 | Scripting symbol for the major version of Unity 2019.4, exposed in every 2019.4.Z release. |
UNITY_2019_4_14 | Scripting symbol for the minor version of Unity 2019.4.14. |
You can also compile code selectively based on the earliest version of Unity required to compile or execute a given portion of code. Given the same version format as above (X.Y), Unity exposes one global #define in the format UNITY_X_Y_OR_NEWER, that you can use for this purpose.
The other scripting symbols Unity defines are:
Define | 功能 |
---|---|
CSHARP_7_3_OR_NEWER | 在构建支持 C# 7.3 或更高版本的脚本时定义。 |
ENABLE_MONO | 用于 Mono 的脚本后端 #define。 |
ENABLE_IL2CPP | 用于 IL2CPP 的脚本后端 #define。 |
NET_2_0 | 在 Mono 和 IL2CPP 上根据 .NET 2.0 API 兼容性级别构建脚本时定义。 |
NET_2_0_SUBSET | 在 Mono 和 IL2CPP 上根据 .NET 2.0 Subset API 兼容性级别构建脚本时定义。 |
NET_LEGACY | 在 Mono 和 IL2CPP 上根据 .NET 2.0 或 .NET 2.0 Subset API 兼容性级别构建脚本时定义。 |
NET_4_6 | 在 Mono 和 IL2CPP 上根据 .NET 4.x API 兼容性级别构建脚本时定义。 |
NET_STANDARD_2_0 | 在 Mono 和 IL2CPP 上根据 .NET 标准 2.0 API 兼容性级别构建脚本时定义。 |
ENABLE_WINMD_SUPPORT | 在 IL2CPP 上启用 Windows 运行时支持时定义。有关更多详细信息,请参阅 Windows 运行时支持。 |
ENABLE_INPUT_SYSTEM | 在 Player Settings 中启用 Input System 包时定义。 |
ENABLE_LEGACY_INPUT_MANAGER | 在 Player Settings 中启用旧版 Input Manager 时定义。 |
UNITY_SERVER | Defined when the Server Build setting is enabled in Build Settings |
DEVELOPMENT_BUILD | Defined when your script is running in a player which was built with the “Development Build” option enabled. |
下面是如何使用预编译代码的示例。该示例根据为目标构建选择的平台打印一条消息。
首先,通过 File > Build Settings 选择要测试代码的平台。随后将显示 Build Settings 窗口;从此处选择目标平台。
选择要测试预编译代码的平台,然后单击 Switch Platform 向 Unity 告知您所需的目标平台。
创建脚本并复制/粘贴以下代码:
using UnityEngine;
using System.Collections;
public class PlatformDefines : MonoBehaviour {
void Start () {
#if UNITY_EDITOR
Debug.Log("Unity Editor");
#endif
#if UNITY_IOS
Debug.Log("iOS");
#endif
#if UNITY_STANDALONE_OSX
Debug.Log("Standalone OSX");
#endif
#if UNITY_STANDALONE_WIN
Debug.Log("Standalone Windows");
#endif
}
}
To test the code, click Play Mode. Confirm that the code works by checking for the relevant message in the Unity console, depending on which platform you selected - for example, if you choose iOS, the message “Unity iOS” is set to appear in the console.
在 C# 中,可使用 CONDITIONAL
属性,这是一种更简洁、更不容易出错的函数剥离方式。请参阅 ConditionalAttribute 类以了解更多信息。
请注意,常见的 Unity 回调(例如:Start()、Update()、LateUpdate()、FixedUpdate()、Awake())不受此属性的影响,因为它们是直接从引擎调用的,并且出于性能原因,此属性不会考虑它们。
除了基本的 #if
编译器指令外,还可在 C# 中使用多路测试:
#if UNITY_EDITOR
Debug.Log("Unity Editor");
#elif UNITY_IOS
Debug.Log("Unity iOS");
#else
Debug.Log("Any other platform");
#endif