1. 程式人生 > >Unity執行平臺判斷

Unity執行平臺判斷

unity是跨平臺的,一般都是在PC ,Andriod 和IOS用的比較多。

平臺判斷程式碼:

 

 
  1.  
  2. #if UNITY_ANDROID

  3. Debug.Log("這裡是安卓裝置");

  4. #endif

  5.  
  6. #if UNITY_IPHONE

  7. Debug.Log("這裡是蘋果裝置");

  8. #endif

  9.  
  10. #if UNITY_STANDALONE_WIN

  11. Debug.Log("我是從Windows的電腦上執行的");

  12. #endif

或者是寫這種:

 
  1.  
  2. if (Application.platform == RuntimePlatform.Android ||

  3. Application.platform == RuntimePlatform.IPhonePlayer)

  4. {

  5.  
  6.  
  7. }

轉自http://blog.csdn.net/alayeshi/article/details/52872962

 

 名稱  描寫敘述
UNITY_EDITOR Define for calling Unity Editor scripts from your game code.
UNITY_STANDALONE_OSX Platform define for compiling/executing code specifically for Mac OS (This includes Universal, PPC and Intel architectures).
UNITY_DASHBOARD_WIDGET Platform define when creating code for Mac OS dashboard widgets.
UNITY_STANDALONE_WIN Use this when you want to compile/execute code for Windows stand alone applications.
UNITY_STANDALONE_LINUX Use this when you want to compile/execute code for Linux stand alone applications.
UNITY_STANDALONE Use this to compile/execute code for any standalone platform (Mac, Windows or Linux).
UNITY_WEBPLAYER Platform define for web player content (this includes Windows and Mac Web player executables).
UNITY_WII Platform define for compiling/executing code for the Wii console.
UNITY_IPHONE Platform define for compiling/executing code for the iPhone platform.
UNITY_ANDROID Platform define for the Android platform.
UNITY_PS3 Platform define for running PlayStation 3 code.
UNITY_XBOX360 Platform define for executing Xbox 360 code.
UNITY_NACL Platform define when compiling code for Google native client (this will be set additionally to UNITY_WEBPLAYER).
UNITY_FLASH Platform define when compiling code for Adobe Flash.
 

 

 

   以後我們能夠依據如上巨集定義就能夠去輕而易舉的非常easy去在我們程式碼中增加推斷了。我舉個簡單樣例,例如以下:

  1. using UnityEngine;  
  2. using System.Collections;  
  3.   
  4. public class Recompile : MonoBehaviour  
  5. {  
  6.   
  7.     private string platform = string.Empty;  
  8.     // Use this for initialization  
  9.     void Start()  
  10.     {  
  11.         DebugPlatformMesaage();  
  12.     }  
  13.   
  14.     void DebugPlatformMesaage()  
  15.     {  
  16.  
  17. #if UNITY_EDITOR  
  18.         platform = "hi,大家好,我是在unity編輯模式下";  
  19. #elif UNITY_XBOX360  
  20.        platform="hi,大家好,我在XBOX360平臺";  
  21. #elif UNITY_IPHONE  
  22.        platform="hi,大家好,我是IPHONE平臺";  
  23. #elif UNITY_ANDROID  
  24.        platform="hi,大家好,我是ANDROID平臺";  
  25. #elif UNITY_STANDALONE_OSX  
  26.        platform="hi,大家好,我是OSX平臺";  
  27. #elif UNITY_STANDALONE_WIN  
  28.        platform="hi,大家好,我是Windows平臺";  
  29. #endif  
  30.         Debug.Log("Current Platform:" + platform);  
  31.     }  
  32. }  

 

上面假設我是在Editor狀態下的話,就能看見打印出:    

 

 

我們也能夠自定義巨集定義,在PlayerSetting中定義:

 

比如我在上面圈起來的地方填寫一個CUSTOM_ITF這個預編譯指令。然後在DebugPlatformMesaage函式尾部增加這句話

  1. //新加入的內容  
  2. #if CUSTOM_ITF  
  3.         customMsg = "我自己定義了預編譯";  
  4. #endif  
  5.         Debug.Log(customMsg);  

 

然後我們執行看下,你就能在控制檯看見我們輸出的資訊了:

 

    當我們把我們自己定義的巨集定義給去掉的時候,我們列印的資訊就不會出來。

 

除了這些,unity中還有各個版本號的巨集定義,一般開發外掛的大牛都會用到。

 

UNITY_2_6 Platform define for the major version of Unity 2.6.
UNITY_2_6_1 Platform define for specific version 1 from the major release 2.6.
UNITY_3_0 Platform define for the major version of Unity 3.0.
UNITY_3_0_0 Platform define for the specific version 0 of Unity 3.0.
UNITY_3_1 Platform define for major version of Unity 3.1.
UNITY_3_2 Platform define for major version of Unity 3.2.
UNITY_3_3 Platform define for major version of Unity 3.3.
UNITY_3_4 Platform define for major version of Unity 3.4.
UNITY_3_5 Platform define for major version of Unity 3.5.
UNITY_4_0 Platform define for major version of Unity 4.0.
UNITY_4_0_1 Platform define for major version of Unity 4.0.1.
UNITY_4_1 Platform define for major version of Unity 4.1.
UNITY_4_2 Platform define for major version of Unity 4.2.