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

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

2015-07-21 10:23 597 查看
Unity(团结) includes a
feature(特色) named “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.

Furthermore(此外), you can run this code within the Editor, so you can compile the code
specifically(特别地) 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(实行) code
specifically(特别地) for Mac OS (This includes Universal, PPC and Intel
architectures(建筑学)).
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 Wii
console(控制台).
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(定义) when
compiling(编译) 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(定义) when
compiling(编译) against Universal SDK 8.1.
UNITY_WINRT_10_0Same as UNITY_WSA_10_0
UNITY_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 number
X.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 and
UNITY_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(编译) 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 in the format
UNITY_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(编译) code
selectively(有选择地) 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 on
File -> 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.
Select the platform you want to test your precompiled code against and press the
Switch Platform button to tell Unity which platform you are targeting.

Create a script and copy/paste(面团) this 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_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 Unity
console(安慰) when you press play.

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.
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(选择) of
defines(定义) by supplying your own. In the
Other 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 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, and 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
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(重新编译) for them to be
effective(有效的). You can do this by updating or
reimporting(再进口) a single script (.js or .cs) file.

If you want to modify(修改) only global defines, you should use
Scripting 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 Editor
installation(安装) 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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: