Platform-specific settings let you change how your game works on different devices. This helps your game run well everywhere.
Platform-specific settings in Unity
using UnityEngine; #if UNITY_ANDROID // Android-specific code here #elif UNITY_IOS // iOS-specific code here #else // Code for other platforms #endif
Use #if directives to include code only for certain platforms.
Unity defines many platform symbols like UNITY_ANDROID, UNITY_IOS, UNITY_STANDALONE_WIN, etc.
using UnityEngine;
void Start() {
#if UNITY_ANDROID
Debug.Log("Running on Android");
#elif UNITY_IOS
Debug.Log("Running on iOS");
#else
Debug.Log("Running on another platform");
#endif
}using UnityEngine;
void SetupGraphics() {
#if UNITY_STANDALONE_WIN
QualitySettings.SetQualityLevel(5); // High quality on Windows
#elif UNITY_ANDROID
QualitySettings.SetQualityLevel(2); // Lower quality on Android
#endif
}This script prints a message when the game starts, telling you which platform it is running on. It helps you test platform-specific code easily.
using UnityEngine; public class PlatformChecker : MonoBehaviour { void Start() { #if UNITY_EDITOR Debug.Log("Running inside Unity Editor"); #elif UNITY_STANDALONE_WIN Debug.Log("Running on Windows standalone"); #elif UNITY_ANDROID Debug.Log("Running on Android device"); #else Debug.Log("Running on some other platform"); #endif } }
Platform symbols are set automatically by Unity based on the build target.
You can also set platform-specific settings in Unity's Build Settings and Player Settings windows.
Use platform-specific code carefully to avoid errors on unsupported platforms.
Platform-specific settings help your game work well on different devices.
Use #if directives with Unity platform symbols to write platform-specific code.
Test your game on each platform to make sure the settings work as expected.
Practice
Solution
Step 1: Understand platform-specific settings
Platform-specific settings allow customizing game behavior for different devices.Step 2: Identify the main goal
The goal is to ensure the game works well on all targeted platforms.Final Answer:
To make the game work well on different devices -> Option CQuick Check:
Platform-specific settings = device compatibility [OK]
- Confusing platform-specific settings with increasing file size
- Assuming settings limit the game to one platform
- Believing settings disable features globally
Solution
Step 1: Recall Unity platform symbols
Unity uses symbols like UNITY_ANDROID for Android platform checks.Step 2: Match the correct syntax
#if UNITY_ANDROID is the correct directive for Android-specific code.Final Answer:
#if UNITY_ANDROID // Android code #endif -> Option AQuick Check:
Use UNITY_ANDROID for Android platform [OK]
- Using incorrect symbols like ANDROID or PLATFORM_ANDROID
- Assuming UNITY_MOBILE covers Android specifically
- Missing #endif to close the directive
#if UNITY_IOS
Console.WriteLine("Running on iOS");
#else
Console.WriteLine("Running on other platform");
#endifSolution
Step 1: Check platform directive
The code uses #if UNITY_IOS, which is true on iOS platform.Step 2: Determine executed code
Since UNITY_IOS is defined, it prints "Running on iOS".Final Answer:
Running on iOS -> Option DQuick Check:
UNITY_IOS true prints iOS message [OK]
- Confusing UNITY_IOS with UNITY_IPHONE
- Expecting else block to run on iOS
- Thinking code causes compilation error
#if UNITY_STANDALONE
Debug.Log("Standalone platform");
#else
Debug.Log("Other platform");
Solution
Step 1: Check #if directive syntax
The code starts with #if but does not have a matching #endif.Step 2: Understand preprocessor rules
Every #if must be closed with #endif to avoid compilation errors.Final Answer:
Missing #endif to close the #if directive -> Option AQuick Check:
#if must end with #endif [OK]
- Assuming UNITY_STANDALONE is invalid
- Thinking Debug.Log is disallowed inside #if
- Ignoring missing #endif causes errors
Solution
Step 1: Identify platforms to include
Windows and macOS correspond to UNITY_STANDALONE_WIN and UNITY_STANDALONE_OSX.Step 2: Combine platform symbols correctly
Use logical OR (||) to include both platforms in one directive.Final Answer:
#if UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX // code #endif -> Option BQuick Check:
Use || to combine Windows and macOS platforms [OK]
- Using UNITY_STANDALONE alone includes all standalone platforms
- Confusing mobile symbols with desktop platforms
- Using && instead of || for platform combination
Ready to prove it?
Pass the test and this pattern moves unknown
