您的位置:首页 > 移动开发 > Unity3D

Platform Dependent Compilation

2016-07-04 10:13 453 查看

平台相关的编译

团结包括一个称为“平台依赖编译”的特性。 这包括一些预处理器指令,让你分区脚本编译和执行一段代码专门为一个受支持的平台。

你可以统一编辑器中运行这段代码,你可以专门为你的目标平台和编译代码编辑器中测试它!

平台#定义指令

平台#定义指令,统一支持脚本如下:

属性:功能:
UNITY_EDITOR#定义指令调用统一从游戏代码编辑器脚本。
UNITY_EDITOR_WIN#定义指令代码编辑窗口。
UNITY_EDITOR_OSX#定义Mac OSX上编辑代码的指令。
UNITY_STANDALONE_OSX#定义指令编译/执行代码专门为OS X(包括通用、PPC和英特尔架构)。
UNITY_STANDALONE_WIN#定义指令编译/专为Windows独立应用程序执行代码。
UNITY_STANDALONE_LINUX#定义指令编译/专为Linux独立应用程序执行代码。
UNITY_STANDALONE#定义指令编译/执行代码的任何独立的平台(OS X Windows或Linux)。
UNITY_WII#定义指令编译/执行代码Wii控制台。
UNITY_IOS#定义指令编译/ iOS平台的执行代码。
UNITY_IPHONE弃用。 使用UNITY_IOS代替。
UNITY_ANDROID#定义Android平台的指令。
UNITY_PS3#定义运行PlayStation 3代码的指令。
UNITY_PS4#定义运行PlayStation 4代码的指令。
UNITY_SAMSUNGTV#定义执行三星电视代码的指令。
UNITY_XBOX360#定义执行Xbox 360代码的指令。
UNITY_XBOXONE#为Xbox执行一个代码定义指令。
UNITY_TIZEN#定义指令Tizen平台。
UNITY_TVOS#定义苹果电视平台的指令。
UNITY_WP_8#为Windows Phone 8定义指令。
UNITY_WP_8_1#为Windows Phone 8.1定义指令。
UNITY_WSA#定义指令为Windows存储应用。 此外,NETFX_CORE当c#文件编译与定义。
净的核心。
UNITY_WSA_8_0#定义指令为Windows存储应用程序时针对SDK 8.0。
UNITY_WSA_8_1#定义指令为Windows存储应用程序时针对SDK 8.1。
UNITY_WSA_10_0#定义指令Windows存储应用程序时针对通用10应用。 另外WINDOWS_UWPNETFX_CORE当c#编译文件中定义。
净的核心。
UNITY_WINRT相当于UNITY_WP_8|UNITY_WSA
UNITY_WINRT_8_0相当于UNITY_WP_8|UNITY_WSA_8_0
UNITY_WINRT_8_1相当于UNITY_WP_8_1|UNITY_WSA_8_1
这也是定义当编译与普遍的SDK 8.1。
UNITY_WINRT_10_0相当于UNITY_WSA_10_0
UNITY_WEBGL#定义WebGL的指令。
UNITY_ADS#定义指令从你的游戏代码调用统一的广告方法。 5.2及以上版本。
UNITY_ANALYTICS#定义指令从你的游戏代码调用统一的分析方法。 5.2及以上版本。
UNITY_ASSERTIONS#定义断言控制指令的过程。
从统一2.6.0开始,您可以选择性地编译代码。 选择取决于版本的编辑工作。给定一个版本号X.Y.Z(例如,2.6.0),统一公开三个全球#定义指令格式如下:UNITY_X,UNITY_X_YUNITY_X_Y_Z

这是一个#在统一定义指令暴露5.0.1的例子:

  
UNITY_5#定义指令的发布版本统一,暴露在每5. x。 Y释放。
UNITY_5_0#定义指令统一的主要版本5.0,暴露在每5.0。 Z释放。
UNITY_5_0_1#定义统一的小版本5.0.1的指令。
你可以选择性地编译代码基于统一的最早版本需要编译或执行一个给定的部分代码。 考虑到相同版本的格式如上(X.Y.Z公开一个全球#),统一定义指令可用于这个目的,在格式UNITY_X_Y_OR_NEWER

支持#定义指令:

  
UNITY_5_3_OR_NEWER5.3全球#定义指令暴露从统一。
你也可以选择性地编译代码根据脚本后端。

  
ENABLE_MONO脚本后端#定义Mono的指令。
ENABLE_IL2CPP脚本后端#为IL2CPP定义指令。
ENABLE_DOTNET脚本后端#为。net定义指令。
您还可以使用
DEVELOPMENT_BUILD
#定义指令来确定您的脚本运行在一个球员,是建立“发展构建”选项启用。

测试预编译代码

下面是如何使用预编译的代码的一个例子。 它打印一条消息,取决于你选择为目标构建平台。

首先,选择你想要的平台对去测试你的代码文件[b]>建立设置[/b]。
这显示了建立设置窗口中,选择你的目标平台。


构建与电脑设置窗口,选择Mac和Linux作为目标平台
选择您想要测试的平台预编译代码并点击转换平台告诉统一平台的目标。

创建一个脚本并复制/粘贴以下代码:

// JS
function Awake() {
#if UNITY_EDITOR
Debug.Log("Unity Editor");
#endif

#if UNITY_IPHONE
Debug.Log("Iphone");
#endif

#if UNITY_STANDALONE_OSX
Debug.Log("Stand Alone OSX");
#endif

#if UNITY_STANDALONE_WIN
Debug.Log("Stand Alone Windows");
#endif
}

// C#
using UnityEngine;
using System.Collections;

public class PlatformDefines : MonoBehaviour {
void Start () {

#if UNITY_EDITOR
Debug.Log("Unity Editor");
#endif

#if UNITY_IOS
Debug.Log("Iphone");
#endif

#if UNITY_STANDALONE_OSX
Debug.Log("Stand Alone OSX");
#endif

#if UNITY_STANDALONE_WIN
Debug.Log("Stand Alone Windows");
#endif

}
}

要测试代码,点击播放模式
确认代码是通过检查相关信息在统一控制台中,取决于你选择哪个平台——例如,如果您选择iOS“Iphone”,消息将出现在控制台。

注意,在c#中可以使用
有条件的
属性是一个更干净、更少出错的方法剔除功能。
看到http://msdn.microsoft.com/en-us/library/4xssyw96(v =应用程序). aspx为更多的信息。

除了基本的
#如果
编译器指令,您还可以使用c#和JavaScript的多路测试:

#if UNITY_EDITOR
Debug.Log("Unity Editor");

#elif UNITY_IOS
Debug.Log("Unity iPhone");

#else
Debug.Log("Any other platform");

#endif

平台自定义定义了

也可以添加到内置的#定义指令通过提供你自己的选择。 在其他设置面板的播放器设置,你将会看到脚本定义符号文本框中。


输入符号的名称你想定义特定平台,用分号分隔。 这些符号可以作为条件
#如果
指令,就像内置的。

全球定制的定义

您可以定义自己的预处理器指令来控制哪些代码编译时被包括。 要做到这一点,你必须添加一个文本文件与额外的指令资产文件夹中。
文件的名称取决于您所使用的语言。 扩展是.rsp:

  
c#<项目路径> /资产/ smcs.rsp
c# -编辑脚本<项目路径> /资产/ gmcs.rsp
UnityScript<项目路径> /资产/ us.rsp
作为一个例子,如果你包含一行
——:UNITY_DEBUG
在你的smcs.rsp文件,#定义指令
UNITY_DEBUG
会存在作为全球#定义c#脚本,除了编辑脚本。

每次你修改.rsp文件,您将需要重新编译为了他们是有效的。
你可以通过更新(或再输入一个脚本。 js或cs)文件。

请注意

如果你想修改只有全球#定义指令,使用脚本定义符号播放器设置,因为这涉及所有的编译器。
如果你选择.rsp文件,您需要为每个编译器联合使用,提供一个文件,你不知道当一个或另一个编译器使用。

的使用.rsp文件中所描述的“帮助”部分smc应用程序,它包含在编辑器中安装文件夹。
你可以得到更多的信息通过运行
smc -


请注意,.rsp文件需要被调用编译器相匹配。
例如:

当针对web播放器,smc是使用
smcs.rsp
,
当针对独立的球员,gmc是使用
gmcs.rsp
,
当目标编译器,女士csc是使用
csc.rsp
等。

Unity includes a feature called “Platform Dependent Compilation”. This consists of some preprocessor directives that let you partition your scripts to compile and execute a section of code exclusively for one of the supported platforms.

You can run this code within the Unity Editor, so you can compile the code specifically for your target platform and test it in the Editor!

Platform #define directives

The platform #define directives that Unity supports for your scripts are as follows:

Property:Function:
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 OSX.
UNITY_STANDALONE_OSX#define directive for compiling/executing code specifically for 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 (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_IPHONEDeprecated. Use UNITY_IOS instead.
UNITY_ANDROID#define directive for the Android platform.
UNITY_PS3#define directive for running PlayStation 3 code.
UNITY_PS4#define directive for running PlayStation 4 code.
UNITY_SAMSUNGTV#define directive for executing Samsung TV code.
UNITY_XBOX360#define directive for executing Xbox 360 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_WP_8#define directive for Windows Phone 8.
UNITY_WP_8_1#define directive for Windows Phone 8.1.
UNITY_WSA#define directive for Windows Store Apps. Additionally,
NETFX_CORE is defined when compiling C# files against .NET Core.
UNITY_WSA_8_0#define directive for Windows Store Apps when targeting SDK 8.0.
UNITY_WSA_8_1#define directive for Windows Store Apps when targeting SDK 8.1.
UNITY_WSA_10_0#define directive for Windows Store Apps when targeting Universal Windows 10 Apps. Additionally
WINDOWS_UWP and NETFX_CORE are defined when compiling C# files against .NET Core.
UNITY_WINRTEquivalent to UNITY_WP_8 | UNITY_WSA.
UNITY_WINRT_8_0Equivalent to UNITY_WP_8 | UNITY_WSA_8_0.
UNITY_WINRT_8_1Equivalent to UNITY_WP_8_1 |
UNITY_WSA_8_1
. This is also defined when compiling against Universal SDK 8.1.
UNITY_WINRT_10_0Equivalent to UNITY_WSA_10_0
UNITY_WEBGL#define directive for WebGL.
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.
From Unity 2.6.0 onwards, you can compile code selectively. The options available depend on the version of the Editor that you are working on.Given a version number
X.Y.Z (for example, 2.6.0), Unity exposes three global #define directives in the following formats:
UNITY_X, UNITY_X_Y and UNITY_X_Y_Z.

Here is an example of #define directives exposed in Unity 5.0.1:

  
UNITY_5#define directive for the release version of Unity 5, exposed in every 5.X.Y release.
UNITY_5_0#define directive for the major version of Unity 5.0, exposed in every 5.0.Z release.
UNITY_5_0_1#define directive for the minor version of Unity 5.0.1.
You can compile code selectively based on the earliest version of Unity required to compile or execute a given portion of code. Given the same version format as above (X.Y.Z), Unity exposes one global #define directive that can be used for
this purpose, in the format UNITY_X_Y_OR_NEWER.

The supported #define directives are:

  
UNITY_5_3_OR_NEWERGlobal #define directive exposed starting from Unity 5.3.
You can also compile code selectively depending on the scripting back-end.

  
ENABLE_MONOScripting back-end #define directive for Mono.
ENABLE_IL2CPPScripting back-end #define directive for IL2CPP.
ENABLE_DOTNETScripting back-end #define directive for .NET.
You can also use the
DEVELOPMENT_BUILD
#define directive to identify whether your script is running in a player which was built with the “Development Build” option enabled.

Testing precompiled code

Below is an example of how to use the precompiled code. It prints a message that depends on the platform you have selected for your target build.

First of all, select the platform you want to test your code against by going to
File [b]> Build Settings[/b]. This displays the
Build Settings
window; select your target platform from here.


Build Settings window with PC, Mac & Linux selected as the target platforms
Select the platform you want to test your precompiled code against and click
Switch Platform
to tell Unity which platform you are targeting.

Create a script and copy/paste the following code:

// JS
function Awake() {
#if UNITY_EDITOR
Debug.Log("Unity Editor");
#endif

#if UNITY_IPHONE
Debug.Log("Iphone");
#endif

#if UNITY_STANDALONE_OSX
Debug.Log("Stand Alone OSX");
#endif

#if UNITY_STANDALONE_WIN
Debug.Log("Stand Alone Windows");
#endif
}

// C#
using UnityEngine;
using System.Collections;

public class PlatformDefines : MonoBehaviour {
void Start () {

#if UNITY_EDITOR
Debug.Log("Unity Editor");
#endif

#if UNITY_IOS
Debug.Log("Iphone");
#endif

#if UNITY_STANDALONE_OSX
Debug.Log("Stand Alone OSX");
#endif

#if UNITY_STANDALONE_WIN
Debug.Log("Stand Alone Windows");
#endif

}
}

To test the code, click Play Mode. Confirm that the code works by checking for the relevant message in the Unity console, depending on which platform you selected - for example, if you choose
iOS, the message “Iphone” is set to appear in the console.

Note that in C# you can use a
CONDITIONAL
attribute which is a more clean, less error-prone way of stripping out functions. See
http://msdn.microsoft.com/en-us/library/4xssyw96(v=vs.90).aspx for more information.

In addition to the basic
#if
compiler directive, you can also use a multiway test in C# and JavaScript:

#if UNITY_EDITOR
Debug.Log("Unity Editor");

#elif UNITY_IOS
Debug.Log("Unity iPhone");

#else
Debug.Log("Any other platform");

#endif

Platform custom defines

It is also possible to add to the built-in selection of #define directives by supplying your own. In the
Other Settings panel of the
Player Settings, you will see the Scripting Define Symbols text box.


Enter the names of the symbols you want to define for that particular platform, separated by semicolons. These symbols can then be used as the conditions for
#if
directives, just like the built-in ones.

Global custom defines

You can define your own preprocessor directives to control which code gets included when compiling. To do this you must add a text file with the extra directives to the
Assets folder. The name of the file depends on the language you are using. The extension is
.rsp:

  
C#<Project Path>/Assets/smcs.rsp
C# - Editor Scripts<Project Path>/Assets/gmcs.rsp
UnityScript<Project Path>/Assets/us.rsp
As an example, if you include the single line
-define:UNITY_DEBUG
in your
smcs.rsp file, the #define directive
UNITY_DEBUG
will exist as a global #define for C# scripts, except for Editor scripts.

Every time you make changes to .rsp files, you will need to recompile in order for them to be effective. You can do this by updating or reimporting a single script (.js or .cs) file.

NOTE

If you want to modify only global #define directives, use Scripting Define Symbols in

Player Settings, as this covers all the compilers. If you choose the
.rsp
files instead, you need to provide one file for every compiler Unity uses, and you won’t know when one or another compiler is used.

The use of .rsp files is described in the ‘Help’ section of the
smcs application which is included in the Editor installation folder. You can get more information by running
smcs -help
.

Note that the .rsp file needs to match the compiler being invoked. For example:

when targeting the web player, smcs is used with
smcs.rsp
,
when targeting standalone players, gmcs is used with
gmcs.rsp
, and
when targeting MS compiler, csc is used with
csc.rsp
, etc.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  UNITY3D 编译条件