[unity3d]unity平台的預先處理

來源:互聯網
上載者:User

在開發中,特別是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?
  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函數尾部加入這句話

[csharp] view plaincopyprint?
  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.
 

 

<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>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.