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.