LoginSignup
122
105

More than 5 years have passed since last update.

Unity プラットフォーム判別

Last updated at Posted at 2014-11-16

プラットフォーム判別

Unityでプラットフォームごとに、コードを変えたい場合は多々あると思います
このページではUnityでのプラットフォーム判別方法をまとめています

1.Platform Dependent Compilationを使う

Platform Dependent Compilation(プラットフォーム依存コンパイル)を使う事で、コンパイル時に
プラットフォームに応じてコードを分けることが出来ます

実際のコードは以下のように書きます if,else if,elseを使う事が出来ます

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

#elif UNITY_IPHONE
    Debug.Log("Unity iPhone");
#else
    Debug.Log("Any other platform");
#endif

条件は、プラットフォームやUnityのバージョンを指定する事が出来ます

よく使われる例を挙げておきます

プラットフォーム 条件文
Unityエディタ UNITY_EDITOR
MacOS UNITY_STANDALONE_OSX
Windows UNITY_STANDALONE_WIN
LINUX UNITY_STANDALONE_LINUX
WebPlayer UNITY_WEBPLAYER
iPhone UNITY_IPHONE
Android UNITY_ANDROID
Unity 4.5 UNITY_4_5

詳細は公式ページをご覧下さい

また、Player Settings の Other Settingsからカスタム定義を加える事も出来ます
2_1.png

詳細は公式ページをご覧下さい

2.Application.platformを使う

Application.platformを使う事で、実行時のプラットフォームを判別することが出来ます

Unity.cs
if (Application.platform == RuntimePlatform.Android) {
  // Android
}
else if (Application.platform == RuntimePlatform.IPhonePlayer) {
  // iOS
}
else if (Application.platform == RuntimePlatform.WindowsPlayer) {
  // Windows
}
else  {

}

指定出来るプラットフォームは公式サイトを参照してください

3.Debug.isDebugBuildを使う

2_2.png

Debug.isDebugBuildによってデバッグ状態でビルドされたかどうかを判別することが出来ます

Unity.cs
 if (Debug.isDebugBuild)
            Debug.Log("This is a debug build!");
122
105
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
122
105