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:
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:
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:
You can also compile(编译) codeselectively(有选择地) 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.
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
Build Settings window with the PC, Mac & Linux Selected as Target platform. . This will bring the build settings window to select your target platform.Select the platform you want to test your precompiled code against and press the
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:
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.
- Unity3d平台脚本预定义编译条件 Platform Dependent Compilation
- 【转】Unity3D Platform Dependent Compilation
- unity Platform Dependent Compilation(平台依赖编译)
- Platform Dependent Compilation
- Platform Dependent Compilation
- Platform Dependent Compilation 多平台兼容性使用注意事项
- Platform Dependent Compilation 多平台兼容性使用注意事项
- Platform Dependent Compilation 多平台兼容性使用注意事项
- 预定义,条件编译,数组
- Object-c:预定义,条件编译,数组
- 预定义宏与条件编译
- IE 条件编译 conditional compilation
- Java 条件编译 Conditional Compilation
- 如何通过预定义确定编译平台
- java 条件编译(Conditional Compilation)
- 【Object-c基础】预定义,条件编译,数组
- 平台预定义
- {Unity} 平台预定义
- RSA算法理解
- 素数线性筛选
- 解决apache无法启动:undefined symbol: apr_ldap_ssl_init
- 归并排序
- RabbitMQ第一篇——初识RabbitMQ,简单的消息发送和接收
- Unity3d平台脚本预定义编译条件 Platform Dependent Compilation
- hibernate现有项目说明
- Java-发送邮件(附件、图片)---(三)类-Properties、Session、MimeMessage
- HUST-1010
- java中十进制转二进制转换函数
- 构建流数据平台(stream data platform)实践指南-(part 1)
- 移动混合应用Hybrid App开发实战
- jQuery $符号冲突
- 百炼-2815-城堡问题-DFS