Unity平台的預處理
在開發中,特別是unity的跨平台中,我們常常會在各個平台游走,如安卓版,蘋果版,PC版......。在此不同的平台上,有可能我們須要做不同的操作。然而我們就能夠用unity的自帶的平台宏定義方式來做平台的推斷。
Unity幫我們定義了例如以下平台預處理:
名稱 | 描寫敘述 |
UNITY_EDITOR | Unity編輯器 |
UNITY_STANDALONE_OSX | 專門為Mac OS(包括Universal,PPC和Intelarchitectures)平台的定義 |
UNITY_DASHBOARD_WIDGET | Mac OS Dashboard widget (Mac OS儀表板小部件)。 |
UNITY_STANDALONE_WIN | Windows系統 |
UNITY_STANDALONE_LINUX | LINUX的獨立的應用程序 |
UNITY_STANDALONE | 獨立的平台 (Mac, Windows or Linux). |
UNITY_WEBPLAYER | 網頁播放器(包括Windows和Mac Web播放器可執行文件)。 |
UNITY_WII | Wii游戲機平台。 |
UNITY_IPHONE | 蘋果系統 |
UNITY_ANDROID | 安卓系統 |
UNITY_PS3 | PlayStation 3 |
UNITY_XBOX360 | VBOX360系統 |
UNITY_NACL | 谷歌原生客戶端(使用這個必須另外使用UNITY_WEBPLAYER) |
UNITY_FLASH | Adobe Flash |
1 using UnityEngine; 2 using System.Collections; 3 publicclassRecompile:MonoBehaviour 4 { 5 privatestring platform =string.Empty; 6 // Use this for initialization 7 voidStart() 8 { 9 DebugPlatformMesaage(); 10 } 11 voidDebugPlatformMesaage() 12 { 13 #if UNITY_EDITOR 14 platform ="hi,大家好,我是在unity編輯模式下"; 15 #elif UNITY_XBOX360 16 platform="hi,大家好,我在XBOX360平台"; 17 #elif UNITY_IPHONE 18 platform="hi,大家好,我是IPHONE平台"; 19 #elif UNITY_ANDROID 20 platform="hi,大家好,我是ANDROID平台"; 21 #elif UNITY_STANDALONE_OSX 22 platform="hi,大家好,我是OSX平台"; 23 #elif UNITY_STANDALONE_WIN 24 platform="hi,大家好,我是Windows平台"; 25 #endif 26 Debug.Log("Current Platform:"+ platform); 27 } 28 }
上面假設我是在Editor狀態下的話,就能看見打印出:

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

比如我在上面圈起來的地方填寫一個CUSTOM_ITF這個預編譯指令。然后在DebugPlatformMesaage函數尾部增加這句話:
//新加入的內容 #if CUSTOM_ITF customMsg ="我自己定義了預編譯"; #endif 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. |
補充:
1/ 獲得當前運行平台
//獲得當前運行平台 Debug.Log("plat = "+Application.platform);
//可以獲取到的平台類型 publicenumRuntimePlatform { OSXEditor=0, OSXPlayer=1, WindowsPlayer=2, OSXWebPlayer=3, OSXDashboardPlayer=4, WindowsWebPlayer=5, WiiPlayer=6, WindowsEditor=7, IPhonePlayer=8, PS3 =9, XBOX360 =10, Android=11, NaCl=12, LinuxPlayer=13, FlashPlayer=15, }
From:http://blog.csdn.net/oskytonight/article/details/40111401