在開發中,特別是unity的跨平台中,我們經常會在各個平台遊走,如安卓版,蘋果版,PC版......。在此不同的平台上,有可能我們需要做不同的操作。然而我們就可以用unity的內建的平台宏定義方式來做平台的判斷。Unity幫我們定義了如下平台預先處理:
| 名稱 |
描述 |
| 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. |
|
|
以後我們可以根據如上宏定義就可以去輕而易舉很容易去在我們代碼中加入判斷了。我舉個簡單例子,如下:
[csharp] view plaincopyprint?
- using UnityEngine;
- using System.Collections;
-
- public class Recompile : MonoBehaviour
- {
-
- private string platform = string.Empty;
- // Use this for initialization
- void Start()
- {
- DebugPlatformMesaage();
- }
-
- void DebugPlatformMesaage()
- {
-
- #if UNITY_EDITOR
- platform = "hi,大家好,我是在unity編輯模式下";
- #elif UNITY_XBOX360
- platform="hi,大家好,我在XBOX360平台";
- #elif UNITY_IPHONE
- platform="hi,大家好,我是IPHONE平台";
- #elif UNITY_ANDROID
- platform="hi,大家好,我是ANDROID平台";
- #elif UNITY_STANDALONE_OSX
- platform="hi,大家好,我是OSX平台";
- #elif UNITY_STANDALONE_WIN
- platform="hi,大家好,我是Windows平台";
- #endif
- Debug.Log("Current Platform:" + platform);
- }
- }
上面如果我是在Editor狀態下的話,就能看見列印出:
我們也可以自己定義宏定義,在PlayerSetting中定義:
例如我在上面圈起來的地方填寫一個CUSTOM_ITF這個先行編譯指令。然後在DebugPlatformMesaage函數尾部加入這句話
[csharp] view plaincopyprint?
- //新添加的內容
- #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. |
|
|
<span style="margin: 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; font-size: 14px;"> 其實先行編譯在我們對unity開發的時候還有一個很好的作用,我們Debug的時候是IO,其實會消耗CPU的,從而影響了我們的效能,我們想在開發的時候進行Debug,但不想在到處的時候列印出,在這個時候我們就可以用先行編譯來判斷他是否是在Editor狀態下,從而做出相應的操作!</span><br style="margin: 0px; padding: 0px;" /></span>
<span style="margin: 0px; padding: 0px; font-size: 14px;"> 預先處理命令從來不會被翻譯為可執行中的命令,但會影響編譯過程的各個方面。例如:使用前置處理器指令可以禁止編譯器編譯代碼的某一部分,如果計劃發布兩個版本的代碼,即基本版本和有更多功能的企業版本,即可以使用這些預先處理指令。在編譯軟體的基本版本時,使用前置處理器指令還可以禁止編譯器編譯於額外相關的代碼。另外,在編寫提供調試資訊的代碼時,也可以使用前置處理器指令。在銷售軟體時,一般不希望編譯這部分代碼。前置處理器指令開頭都有符號#。我們unity的不是分專業版和免費版嗎,其實有可能就是用到了先行編譯,從而編譯不同的版本。<br style="margin: 0px; padding: 0px;" /><br style="margin: 0px; padding: 0px;" /> <br style="margin: 0px; padding: 0px;" /> 如果對先行編譯不熟的同學可以去百度,或者Google下,我也是在查閱後才明白的!</span>