Unity3d平台脚本预定义编译条件 Platform Dependent Compilation

来源:互联网 发布:淘宝充值平台官网登录 编辑:程序博客网 时间:2024/05/16 14:58

Unity(团结) includes afeature(特色) named “Platform Dependent Compilation”. This consists of somepreprocessor(预处理器)directives(指示) that let youpartition your scripts to compile(编译) andexecute(实行) a section of codeexclusively(唯一地) for one of the supported platforms.

Furthermore(此外), you can run this code within the Editor, so you can compile the codespecifically(特别地) for your mobile/console(控制台) and test it in the Editor!

Platform Defines

The platform defines(定义) that Unity supports for your scripts are:

Property:Function:UNITY_EDITORDefine for calling Unity Editor scripts from your game code.UNITY_EDITOR_WINPlatform define for editor code on Windows.UNITY_EDITOR_OSXPlatform define(定义) for editor code on Mac OSX.UNITY_STANDALONE_OSXPlatform define for compiling(编译)/executing(实行) codespecifically(特别地) for Mac OS (This includes Universal, PPC and Intelarchitectures(建筑学)).UNITY_STANDALONE_WINUse this when you want to compile/execute code for Windows stand alone applications.UNITY_STANDALONE_LINUXUse this when you want to compile/execute code for Linux stand alone applications.UNITY_STANDALONEUse this to compile/execute code for any standalone(单独的) platform (Mac, Windows or Linux).UNITY_WIIPlatform define for compiling/executing code for the Wiiconsole(控制台).UNITY_IOSPlatform define for compiling/executing code for the iOS platform.UNITY_IPHONEDeprecated. Use UNITY_IOS instead.UNITY_ANDROIDPlatform define for the Android platform.UNITY_PS3Platform define(定义) for running PlayStation 3 code.UNITY_PS4Platform define for running PlayStation 4 code.UNITY_SAMSUNGTVPlatform define for executing(实行) Samsung TV code.UNITY_XBOX360Platform define for executing Xbox 360 code.UNITY_XBOXONEPlatform define for executing Xbox One code.UNITY_TIZENPlatform define for the Tizen platform.UNITY_TVOSPlatform define for the Apple TV platform.UNITY_WP8Platform define for Windows Phone 8.UNITY_WP_8_1Platform define for Windows Phone 8.1.UNITY_WSAPlatform define for Windows Store Apps (additionally(附加的)NETFX_CORE is defined(定义) whencompiling(编译) C# files against .NET Core).UNITY_WSA_8_0Platform define for Windows Store Apps when targeting SDK 8.0.UNITY_WSA_8_1Platform define for Windows Store Apps when targeting SDK 8.1.UNITY_WSA_10_0Platform define for Windows Store Apps when targeting Universal Windows 10 Apps (additionally(附加的) WINDOWS_UWP and NETFX_CORE is defined when compiling C# files against .NET Core).UNITY_WINRTEquivalent to UNITY_WP8 |UNITY_WSA.UNITY_WINRT_8_0Equivalent to UNITY_WP8 |UNITY_WSA_8_0.UNITY_WINRT_8_1Equivalent to UNITY_WP_8_1 |UNITY_WSA_8_1. It’s also defined(定义) whencompiling(编译) against Universal SDK 8.1.UNITY_WINRT_10_0Same as UNITY_WSA_10_0UNITY_WEBGLPlatform define for WebGL.UNITY_ADSDefine for calling Unity Ads methods from your game code. Version 5.2 and above.UNITY_ANALYTICSDefine for calling Unity Analytics methods from your game code. Version 5.2 and above.UNITY_ASSERTIONSControl process for assertions(断言).

Starting from Unity 2.6.0, you can compile code selectively(有选择地) depending on the version of the engine you are working on. Given a version numberX.Y.Z (such as 2.6.0, 5.0.0 or 4.6.3 to give a few examples), Unity exposes three global defines in the following formats:UNITY_X, UNITY_X_Y andUNITY_X_Y_Z.

Here is an example of defines(定义) exposed in Unity 5.0.1:

  UNITY_5Platform define for the release version of Unity 5, exposed in every 5.X.Y release.UNITY_5_0Platform define for the major version of Unity 5.0, exposed in every 5.0.Z release.UNITY_5_0_1Platform define for the minor(未成年的) version of Unity 5.0.1.

Starting from Unity 5.3.4, you can compile(编译) codeselectively(有选择地) based on the earliest version of Unity required to compile orexecute(实行) a givenportion(部分) of code. Given the same version format as above (X.Y.Z),Unity(团结) exposes one global define in the formatUNITY_X_Y_OR_NEWER, that can be used for this purpose.

The supported defines are:

  UNITY_5_3_OR_NEWERGlobal define exposed starting from Unity 5.3.

You can also compile(编译) codeselectively(有选择地) depending on the scripting backend.

  ENABLE_MONOScripting backend define(定义) for Mono.ENABLE_IL2CPPScripting backend define for IL2CPP.ENABLE_DOTNETScripting backend define for .NET.

You can also use the DEVELOPMENT_BUILD define to identify(确定) whether your script is running in a player which was built with the “Development Build” option enabled.

Testing precompiled code.

We are going to show a small example of how to use the precompiled(预编译) code. This will simply print a message that depends on the platform you have selected to build your target.

First of all, select the platform you want to test your code against by clicking onFile -> Build Settings. This will bring the build settings window to select your target platform.

Build Settings window with the PC, Mac & Linux Selected as Target platform.Build Settings window with the PC, Mac & Linux Selected as Target platform.

Select the platform you want to test your precompiled code against and press theSwitch Platform button to tell Unity which platform you are targeting.

Create a script and copy/paste(面团) this code:-

// JSfunction 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_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  }          }

Then, depending on which platform you selected, one of the messages will get printed on the Unityconsole(安慰) when you press play.

Note that in C# you can use a CONDITIONAL attribute(属性) which is a more clean, lesserror-prone(易于出错的) way ofstripping(剥夺) out functions, seehttp://msdn.microsoft.com/en-us/library/4xssyw96(v=vs.90).aspx.

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_IPHONE    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(选择) ofdefines(定义) by supplying your own. In theOther Settings panel of the Player Settings, you will see the Scripting Define Symbols(象征) textbox.

Here, you can enter the names of the symbols you want to define for that particular platform, separated bysemicolons(分号). 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, and theextension(延长) is.rsp:

  C#<Project Path>/Assets/smcs.rspC# - Editor Scripts<Project Path>/Assets/gmcs.rspUnityScript<Project Path>/Assets/us.rsp

As an example, if you include the single line “-define:UNITY_DEBUG” in yoursmcs.rsp file the define UNITY_DEBUG will exist as a globaldefine(定义) for C# scripts, except for Editor scripts.

Every time you make changes to .rsp files you will need to recompile(重新编译) for them to beeffective(有效的). You can do this by updating orreimporting(再进口) a single script (.js or .cs) file.

If you want to modify(修改) only global defines, you should useScripting Define Symbols in Player Settings, because this will cover all the compilers(编译). If you choose the .rsp files instead, you’ll have to provide one file for every compiler Unity uses, and you won’t know when one or another compiler is used.

The use of the .rsp files is described in the help section of the smcs application which is included in the Editorinstallation(安装) folder. You can get more information by running “smcs -help”. Also, bear in mind 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; when targeting MS compiler, csc is used with csc.rsp; and so on.


0 0