参考文档:https://docs.unity3d.com/Manual/PlatformDependentCompilation.htmlhtml
在Unity开发中,可使用预编译条件,宏定义。好比在一个cs文件中android
#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。app
除了这种咱们定义的局部宏,还有一种是全局宏,在整个工程中都生效。好比编辑器
#if UNITY_EDITOR
DoSomething();
#elif UNITY_ANDROID
DoSomething2();
#endif
表示分别在编辑器环境下和android环境下作某些处理。函数
这个宏是Unity提供的全局宏。注意:本文如下的宏都是指全局宏。ui
Unity中的宏
这类的宏定义有如下几种。spa
平台类宏
因为各个平台的实现和接口都不一样,因此不少时候须要根据平台来作。Unity提供了覆盖了大部分设备的宏定义。3d
- 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。这时候就须要根据版本分别实现。code
Unity提供了2.6.0以后的大部分宏定义,形如UNITY_X或者UNITY_X_Y或者UNITY_X_Y_Z,好比UNITY_2017_1_0表示Unity2017.1.0版本。注意Unity并无提供patch版本的宏定义,后面会说明如何实现自定义的宏。orm
代码编译类宏
这类宏有
- 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/