溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

[unity3d]unity平臺的預處理

發布時間:2020-05-26 03:53:25 來源:網絡 閱讀:432 作者:蓬萊仙羽 欄目:游戲開發

在開發中,特別是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.
 

 

   以后我們可以根據如上宏定義就可以去輕而易舉的很容易去在我們代碼中加入判斷了。我舉個簡單例子,如下:

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狀態下的話,就能看見打印出:     [unity3d]unity平臺的預處理

 

我們也可以自己定義宏定義,在PlayerSetting中定義:

[unity3d]unity平臺的預處理

 

例如我在上面圈起來的地方填寫一個CUSTOM_ITF這個預編譯指令。然后在DebugPlatformMesaage函數尾部加入這句話

//新添加的內容 #if CUSTOM_ITF         customMsg = "我自定義了預編譯"; #endif         Debug.Log(customMsg);

然后我們運行看下,你就能在控制臺看見我們輸出的信息了:

[unity3d]unity平臺的預處理

 

    當我們把我們自定義的宏定義給去掉的時候,我們打印的信息就不會出來。

 

除了這些,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.
 

 

    其實預編譯在我們對unity開發的時候還有一個很好的作用,我們Debug的時候是IO,其實會消耗CPU的,從而影響了我們的性能,我們想在開發的時候進行Debug,但不想在到處的時候打印出,在這個時候我們就可以用預編譯來判斷他是否是在Editor狀態下,從而做出相應的操作!
    預處理命令從來不會被翻譯為可執行中的命令,但會影響編譯過程的各個方面。例如:使用預處理器指令可以禁止編譯器編譯代碼的某一部分,如果計劃發布兩個版本的代碼,即基本版本和有更多功能的企業版本,即可以使用這些預處理指令。在編譯軟件的基本版本時,使用預處理器指令還可以禁止編譯器編譯于額外相關的代碼。另外,在編寫提供調試信息的代碼時,也可以使用預處理器指令。在銷售軟件時,一般不希望編譯這部分代碼。預處理器指令開頭都有符號#。我們unity的不是分專業版和免費版嗎,其實有可能就是用到了預編譯,從而編譯不同的版本。


如果對預編譯不熟的同學可以去百度,或者谷歌下,我也是在查閱后才明白的!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

un %d
AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女