參考文檔:https://docs.unity3d.com/Manual/PlatformDependentCompilation.html
在Unity開發中,可以使用預編譯條件,宏定義。比如在一個cs文件中
#define DevTest using UnityEngine; namespace Test { public class Dev : MonoBehaviour { public void Start() { #if DevTest DoSomeThing(); #else DoSomeThing2(); #endif } private void DoSomeThing() { } private void DoSomeThing2() { } } }
在文件的首行定義一個局部宏,僅限在本文件中有效。然后使用#if的方式判斷是否存在這個宏,如果存在則運行DoSomeThing1,否則運行DoSomeThing2。注意編譯時,會把不需要運行的代碼移除掉。所以編譯出來的代碼中Start函數中不會出現DoSomeThing2。
除了這種我們定義的局部宏,還有一種是全局宏,在整個工程中都生效。比如
#if UNITY_EDITOR DoSomething(); #elif UNITY_ANDROID DoSomething2(); #endif
表示分別在編輯器環境下和android環境下做某些處理。
這個宏是Unity提供的全局宏。注意:本文以下的宏都是指全局宏。
Unity中的宏
這類的宏定義有以下幾種。
平台類宏
由於各個平台的實現和接口都不同,所以很多時候需要根據平台來做。Unity提供了覆蓋了大部分設備的宏定義。
- UNITY_EDITOR #define directive for calling Unity Editor scripts from your game code.
- UNITY_EDITOR_WIN #define directive for Editor code on Windows.
- UNITY_EDITOR_OSX #define directive for Editor code on Mac OS X.
- UNITY_STANDALONE_OSX #define directive for compiling/executing code specifically for Mac OS X (including Universal, PPC and Intel architectures).
- UNITY_STANDALONE_WIN #define directive for compiling/executing code specifically for Windows standalone applications.
- UNITY_STANDALONE_LINUX #define directive for compiling/executing code specifically for Linux standalone applications.
- UNITY_STANDALONE #define directive for compiling/executing code for any standalone platform (Mac OS X, Windows or Linux).
- UNITY_WII #define directive for compiling/executing code for the Wii console.
- UNITY_IOS #define directive for compiling/executing code for the iOS platform.
- UNITY_IPHONE Deprecated. Use UNITY_IOS instead.
- UNITY_ANDROID #define directive for the Android platform.
- UNITY_PS4 #define directive for running PlayStation 4 code.
- UNITY_SAMSUNGTV #define directive for executing Samsung TV code.
- UNITY_XBOXONE #define directive for executing Xbox One code.
- UNITY_TIZEN #define directive for the Tizen platform.
- UNITY_TVOS #define directive for the Apple TV platform.
- UNITY_WSA #define directive for Universal Windows Platform. Additionally, NETFX_CORE is defined when compiling C# files against .NET Core and using .NET scripting backend.
- UNITY_WSA_10_0 #define directive for Universal Windows Platform. Additionally WINDOWS_UWP is defined when compiling C# files against .NET Core.
- UNITY_WINRT Same as UNITY_WSA.
- UNITY_WINRT_10_0 Equivalent to UNITY_WSA_10_0
- UNITY_WEBGL #define directive for WebGL.
- UNITY_FACEBOOK #define directive for the Facebook platform (WebGL or Windows standalone).
- UNITY_ADS #define directive for calling Unity Ads methods from your game code. Version 5.2 and above.
- UNITY_ANALYTICS #define directive for calling Unity Analytics methods from your game code. Version 5.2 and above.
- UNITY_ASSERTIONS #define directive for assertions control process.
Unity版本類宏
由於Unity每個版本的API可能不同,而項目成員使用的是不同版本的Unity。這時候就需要根據版本分別實現。
Unity提供了2.6.0之后的大部分宏定義,形如UNITY_X或者UNITY_X_Y或者UNITY_X_Y_Z,比如UNITY_2017_1_0表示Unity2017.1.0版本。注意Unity並沒有提供patch版本的宏定義,后面會說明如何實現自定義的宏。
代碼編譯類宏
這類宏有
- ENABLE_MONO Scripting backend #define for Mono.
- ENABLE_IL2CPP Scripting backend #define for IL2CPP.
- ENABLE_DOTNET Scripting backend #define for .NET.
- NETFX_CORE Defined when building scripts against .NET Core class libraries on .NET.
- NET_2_0 Defined when building scripts against .NET 2.0 API compatibility level on Mono and IL2CPP.
- NET_2_0_SUBSET Defined when building scripts against .NET 2.0 Subset API compatibility level on Mono and IL2CPP.
- NET_4_6 Defined when building scripts against .NET 4.6 API compatibility level on Mono and IL2CPP.
- ENABLE_WINMD_SUPPORT Defined when Windows Runtime support is enabled on IL2CPP and .NET. See Windows Runtime Support for more details.
自定義宏
在C#工程中,我們是可以通過Project上的設置類定義自己的宏。如下圖,通過在Project的屬性頁面中可以配置宏。
但是在Unity生成的工程中是無法打開Project的屬性配置界面。因此無法在C#工程中進行配置。
使用PlayerSetting定義宏
要進行自定義的宏,需要在Unity的PlayerSetting中進行設置,多個宏定義間用分號“;”隔開。設置的數據會被存儲在項目的ProjectSettings/ProjectSettings.asset文件中。
由於ProjectSettings/ProjectSettings.asset一般會被納入版本管理,所以項目的全體成員一般都會同步宏定義。但是有時候存在單獨的某些成員,需要自己單獨的自定義宏的情況。
特殊案例
在Unity2017.2.0f3(f3版本)和Unity2017.2.0p2(p2版本)這2個版本中,由於某些API接口的變更,p2版本書寫的代碼就在f3版本中報錯。f3版本是項目大多數成員使用的版本,p2版本為少數人使用的版本。但是由於Unity版本的版本宏定義中只有UNITY_2017_2_0,無法區分是f3版本和p2版本,所以只能使用自定義宏。但是由於PlayerSetting會被同步,所以又會造成全部開啟宏的情況,f3版本和p2版本依然無法區分。
使用RSP文件定義宏
Unity提供一個方法,可以用一個rsp文件來定義宏。然后將rsp文件不要納入版本庫。這樣就可以解決特殊案例中問題。
一般來說開發Unity用的是C#,那么這個rsp文件必須命名為mcs.rsp並且放在Assets目錄下。使用rsp文件定義宏,只需要每一行輸入一個“-define:<宏名稱>”即可,比如這里隨意增加2個宏定義:
-define:DevTest
-define:UNITY_2017_2_0_P2
輸入完成后保存退出,在Unity中對任意一個代碼reimort一次,使Unity重新編譯。關閉並重新打開代碼編輯器就會看到這個DevTest的全局宏已經生效。
特殊案例的建議
大部分時候還是建議使用PlayerSetting定義宏,統一使用同一個版本的Unity。
轉載注明:http://www.codegize.com http://www.cnblogs.com/CodeGize/