0
0
Unityframework~5 mins

Platform-specific settings in Unity

Choose your learning style9 modes available
Introduction

Platform-specific settings let you change how your game works on different devices. This helps your game run well everywhere.

You want different screen resolutions for mobile and desktop.
You need to use special controls on a console but not on PC.
You want to enable or disable features depending on the device.
You want to set different graphics quality for phones and computers.
You want to handle file paths differently on Windows and Android.
Syntax
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.

Examples
This example prints a message depending on the platform the game runs on.
Unity
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
}
This example sets different graphics quality levels for Windows and Android.
Unity
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
}
Sample Program

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.

Unity
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
    }
}
OutputSuccess
Important Notes

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.

Summary

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.