Version: 2021.3
特殊フォルダーとスクリプトのコンパイル順
Custom scripting symbols

Conditional Compilation

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”.

Platform scripting symbols

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.

Editor version Scripting symbols

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.

Other scripting symbols

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 Standard 2.0 API 互換性レベル向けにスクリプトをビルドするときに定義されます。
NET_STANDARD_2_1 Defined when building scripts against .NET Standard 2.1 API compatibility level on Mono and IL2CPP.
NET_STANDARD Defined when building scripts against .NET Standard 2.1 API compatibility level on Mono and IL2CPP.
NETSTANDARD2_1 Defined when building scripts against .NET Standard 2.1 API compatibility level on Mono and IL2CPP.
NETSTANDARD Defined when building scripts against .NET Standard 2.1 API compatibility level on Mono and IL2CPP.
ENABLE_WINMD_SUPPORT Windows Runtime のサポートが IL2CPP で使用可能なときに定義されます。詳しくは Windows Runtime Support を参照してください。
ENABLE_INPUT_SYSTEM Player Settings で Input System パッケージが有効になっている場合に定義されます。
ENABLE_LEGACY_INPUT_MANAGER Player Settings で古い Input Manager が有効になっている場合に定義されます。
UNITY_SERVER Build Settings で Server Build 設定が有効になっている場合に定義されます。
DEVELOPMENT_BUILD Defined when your script is running in a player which was built with the “Development Build” option enabled.

Testing precompiled code

以下は、プリコンパイルコードの使用例です。選択したビルドターゲットのプラットフォームにもとづいてメッセージを出力します。

まず初めに、コードをテストしたいプラットフォームを選択するために File > Build Settings をクリックします。これによりターゲットプラットフォームを選択する Build Settings ウィンドウが表示されます。

Build Settings window with Windows, Mac, Linux selected as the target platform
Build Settings window with Windows, Mac, Linux selected as the target platform

プリコンパイルコードをテストしたいプラットフォームを選択して、Switch Platform をクリックし、どのプラットフォームをターゲットにするのか設定します。

スクリプトを作成し、次のコードをコピーアンドペーストします。

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 Class を参照してください。一般的な 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
特殊フォルダーとスクリプトのコンパイル順
Custom scripting symbols