Unity Platform-Specific Compilation

Unity provides a platform-specific compilation feature that allows developers to write code that will only be included in the build for a specific platform. This feature is useful when needed to write platform-specific code or to optimize the builds by excluding unnecessary code for certain platforms.

How To Use Platform-Specific Compilation

To use platform-specific compilation in Unity, use preprocessor directives. Preprocessor directives are special instructions to the compiler that are executed before the actual compilation process. These directives can be used to conditionally include or exclude code based on the target platform.

Here's an example of how to use platform-specific compilation in Unity:

    // iOS-specific code
    // This code will only be included in the build for iOS
    // Android-specific code
    // This code will only be included in the build for Android
    // Code for other platforms
    // This code will be included in the build for all other platforms

In this example, the 'UNITY_IOS' and 'UNITY_ANDROID' directives are provided by Unity and can be used to conditionally compile code for iOS and Android platforms, respectively. Other available platform-specific directives can be used such as 'UNITY_EDITOR' (for the Unity Editor), 'UNITY_STANDALONE' (for standalone builds), 'UNITY_WEBGL' (for WebGL builds), and more.

    // Editor-specific code
    // This code will only be included when running in the Unity Editor
    using UnityEditor;
    // Standalone build-specific code
    // This code will only be included when building for standalone platforms (Windows, macOS, Linux)
    // WebGL-specific code
    // This code will only be included when building for WebGL
    using UnityEngine.Networking;

// Shared code that will be included in all builds
public class MyScript : MonoBehaviour
    private void Start()
        Debug.Log("Running in Unity Editor");
        Debug.Log("Running in standalone build");
        Debug.Log("Running in WebGL build");


By using platform-specific compilation, developers can write code that takes advantage of the features and capabilities of each platform while keeping the codebase organized and optimized for different target platforms in Unity.

Suggested Articles
Adding Player Entry to a Car in Unity
Using Runtime Animator Controller in Unity
Implementing Object Pooling in Unity
Creating a Turret Controller in Unity
Implementing VR Headset Control in Unity
A Guide to Integrating Nintendo Controller with Unity
Adding Sway Effect to Weapons in Unity