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!
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:
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:
You can also compile(编译) code
selectively(有选择地) depending on the scripting backend.
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.
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:-
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
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:-
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.
the extra directives to the “Assets/” folder. The name of the file depends on the language you are using, and the
extension(延长) is
.rsp:
As an example, if you include the single line “
smcs.rsp file the define
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 “
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.
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_EDITOR | Define for calling Unity Editor scripts from your game code. |
UNITY_EDITOR_WIN | Platform define for editor code on Windows. |
UNITY_EDITOR_OSX | Platform define(定义) for editor code on Mac OSX. |
UNITY_STANDALONE_OSX | Platform define for compiling(编译)/executing(实行) code specifically(特别地) for Mac OS (This includes Universal, PPC and Intel architectures(建筑学)). |
UNITY_STANDALONE_WIN | Use this when you want to compile/execute code for Windows stand alone applications. |
UNITY_STANDALONE_LINUX | Use this when you want to compile/execute code for Linux stand alone applications. |
UNITY_STANDALONE | Use this to compile/execute code for any standalone(单独的) platform (Mac, Windows or Linux). |
UNITY_WII | Platform define for compiling/executing code for the Wii console(控制台). |
UNITY_IOS | Platform define for compiling/executing code for the iOS platform. |
UNITY_IPHONE | Deprecated. Use UNITY_IOS instead. |
UNITY_ANDROID | Platform define for the Android platform. |
UNITY_PS3 | Platform define(定义) for running PlayStation 3 code. |
UNITY_PS4 | Platform define for running PlayStation 4 code. |
UNITY_SAMSUNGTV | Platform define for executing(实行) Samsung TV code. |
UNITY_XBOX360 | Platform define for executing Xbox 360 code. |
UNITY_XBOXONE | Platform define for executing Xbox One code. |
UNITY_TIZEN | Platform define for the Tizen platform. |
UNITY_TVOS | Platform define for the Apple TV platform. |
UNITY_WP8 | Platform define for Windows Phone 8. |
UNITY_WP_8_1 | Platform define for Windows Phone 8.1. |
UNITY_WSA | Platform define for Windows Store Apps (additionally(附加的) NETFX_CORE is defined(定义) when compiling(编译) C# files against .NET Core). |
UNITY_WSA_8_0 | Platform define for Windows Store Apps when targeting SDK 8.0. |
UNITY_WSA_8_1 | Platform define for Windows Store Apps when targeting SDK 8.1. |
UNITY_WSA_10_0 | Platform 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_WINRT | Equivalent to UNITY_WP8 | UNITY_WSA. |
UNITY_WINRT_8_0 | Equivalent to UNITY_WP8 | UNITY_WSA_8_0. |
UNITY_WINRT_8_1 | Equivalent to UNITY_WP_8_1 | UNITY_WSA_8_1. It’s also defined(定义) when compiling(编译) against Universal SDK 8.1. |
UNITY_WINRT_10_0 | Same as UNITY_WSA_10_0 |
UNITY_WEBGL | Platform define for WebGL. |
UNITY_ADS | Define for calling Unity Ads methods from your game code. Version 5.2 and above. |
UNITY_ANALYTICS | Define for calling Unity Analytics methods from your game code. Version 5.2 and above. |
UNITY_ASSERTIONS | Control process for assertions(断言). |
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_5 | Platform define for the release version of Unity 5, exposed in every 5.X.Y release. |
UNITY_5_0 | Platform define for the major version of Unity 5.0, exposed in every 5.0.Z release. |
UNITY_5_0_1 | Platform define for the minor(未成年的) version of Unity 5.0.1. |
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_NEWER | Global define exposed starting from Unity 5.3. |
selectively(有选择地) depending on the scripting backend.
ENABLE_MONO | Scripting backend define(定义) for Mono. |
ENABLE_IL2CPP | Scripting backend define for IL2CPP. |
ENABLE_DOTNET | Scripting backend define for .NET. |
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 theprecompiled(预编译) 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 thebuilt-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 withthe 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 |
-define:UNITY_DEBUG” in your
smcs.rsp file the define
UNITY_DEBUGwill 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.
相关文章推荐
- Unity3d iOS 内购详细流程总汇
- Unity3D脚本-C#
- Unity AssetBundle爬坑手记
- unity3d5.0 SurvivalShooter(三) 实现动画播放代码
- 神马都是浮云,unity中自己写Coroutine协程源码
- 在Unity中使用射线
- UnityShader : 高斯模糊 Gaussian Blur
- [Unity3D]通过SerializedObject访问ParticleSystem中的参数
- Unity退出后保存数据,PlayerPrefs 玩家偏好
- Unity3d中的属性(Attributes)整理
- Unity3D读取XML文档信息
- UNITY3D学习笔记2
- Unity运行时保存prefab的方法一则
- Unity使用C#的多线程
- Unity Web自适应浏览器
- Unity Web自适应浏览器
- Unity3d 扩展自定义类Inspector
- 记一下关于Instantiate时Awake Start的执行
- unity xml序列化与反序列化 多平台
- unity xml序列化与反序列化 多平台