Published on

Summary of how to identify platforms in Unity

Authors
  • Evelyn

Please share this article if you like!

Hi! How are you doing?

I'm developing a game in Unity, and I want to do individual processing for each platform.

To help you out, I have put together a method to determine the platform on which the game is running.

In this article, we will cover the following three methods:

  1. Using Platform Dependent Compilation
  2. Using Application.platform
  3. Using Debug.isDebugBuild

Using Platform Dependent Compilation

By using Platform Dependent Compilation, you can separate the code according to the platform at compile time. By using Platform Dependent Compilation, we can separate the code according to the platform.

The actual code is written as follows. you can use if, else if, else

#if UNITY_EDITOR
    Debug.Log("Unity Editor");

#elif UNITY_IPHONE
    Debug.Log("iOS");
#elif UNITY_ANDROID
    Debug.Log("ANDROID");
#else
    Debug.Log("Any other platform");
#endif

Pleaes read the official documentation for details.

#define directives
This way is useful because it can also be used to define the namespace to be used and the serialization field.

Using Application.platform

Also, by using Application.platform, you can determine the platform at runtime.

if (Application.platform == RuntimePlatform.IPhonePlayer)
{
    Debug.Log("iOS");
}
else if (Application.platform == RuntimePlatform.Android)
{
    Debug.Log("ANDROID");
}
else if (Application.platform == RuntimePlatform.WindowsPlayer)
{
    Debug.Log("Windows");
}
else  {
    Debug.Log("Any other player");
}

Pleaes read the official documentation for details.

RuntimePlatform

Using Debug.isDebugBuild

Debug.isDebugBuild can be used to determine if the program was built in debug state.

if (Debug.isDebugBuild)
    Debug.Log("Now debug build");

Summary of this article

This is how to judge each platform.

You might often use this for scripts that run only in the Unity Editor.

Please try to use the contents of this article to define the behavior for each platform.

I hope this article will be useful to you in your game development.

Please share this article if you like!