Documentation

Unity Engine


User Manual

Script Reference

Unity Engine


Conditional compilation behavior for native iOS plug-ins

Learn how to use conditional compilation to support different project types in your native source code plug-in.
Read time 2 minutesLast updated 13 days ago

Learn about the conditions you can check at different stages of build and runtime to create a generic plug-in that works across different targets.
Use conditional logic to create a generic plug-in that works across different targets. You can add checks in both native and C# code at different stages of the build and runtime process.
The following table outlines the conditions you can check at each stage:

Build stage

Conditions and usage

Xcode project generationAutomatic plug-in integration:

When using the plug-in Inspector, configure platform-specific settings to check for the Platform, SDK version, Architecture, and Project type, and use Define Constraints to check for the Unity version.

Manual plug-in file injection at post process with the PBXProject API:

Use PostProcessBuildAttribute with the PBXProject API to add plug-ins based on Unity versions. You can check for:
Xcode project compilationIn native code (Swift or Objective-C), you can check for:In native Swift code, you can check for:
  • Unity version, using the
    UnityAPI
    module version with
    #if canImport(UnityAPI, _version: <UnityVersion>)
    . Write
    <UnityVersion>
    without the release type or build number, for example
    6000.7.0
    for Unity 6000.7.0a5. The check matches that version and later. An Objective-C project type has no
    UnityAPI
    module, so the check is always false.
Application runtimeIn native code, Swift or Objective-C, you can check for:In a C# managed script, you can check for:
You can use preprocessor directives, such as
#if
, in both Swift and Objective-C to adjust behavior based on the target platform, architecture, or SDK version.
Unity's generated Xcode project can be one of two types: Swift or Objective-C. To distinguish between these types in your native code, use the
UNITY_XCODE_PROJECT_TYPE_SWIFT
preprocessor macro.

Additional resources