在开发中,特别是unity的跨平台中,我们常常会在各个平台游走,如安卓版,苹果版,PC版......。在此不同的平台上,有可能我们须要做不同的操作。然而我们就能够用unity的自带的平台宏定义方式来做平台的推断。Unity帮我们定义了例如以下平台预处理:

 名称  描写叙述
UNITY_EDITOR Define for calling Unity Editor scripts from your game code.
UNITY_STANDALONE_OSX Platform define for compiling/executing code specifically for Mac OS (This includes Universal, PPC and Intel architectures).
UNITY_DASHBOARD_WIDGET Platform define when creating code for Mac OS dashboard widgets.
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_WEBPLAYER Platform define for web player content (this includes Windows and Mac Web player executables).
UNITY_WII Platform define for compiling/executing code for the Wii console.
UNITY_IPHONE Platform define for compiling/executing code for the iPhone platform.
UNITY_ANDROID Platform define for the Android platform.
UNITY_PS3 Platform define for running PlayStation 3 code.
UNITY_XBOX360 Platform define for executing Xbox 360 code.
UNITY_NACL Platform define when compiling code for Google native client (this will be set additionally to UNITY_WEBPLAYER).
UNITY_FLASH Platform define when compiling code for Adobe Flash.
 

以后我们能够依据如上宏定义就能够去轻而易举的非常easy去在我们代码中增加推断了。我举个简单样例,例如以下:

  1. using UnityEngine;
  2. using System.Collections;
  3. public class Recompile : MonoBehaviour
  4. {
  5. private string platform = string.Empty;
  6. // Use this for initialization
  7. void Start()
  8. {
  9. DebugPlatformMesaage();
  10. }
  11. void DebugPlatformMesaage()
  12. {
  13. #if UNITY_EDITOR
  14. platform = "hi,大家好,我是在unity编辑模式下";
  15. #elif UNITY_XBOX360
  16. platform="hi,大家好,我在XBOX360平台";
  17. #elif UNITY_IPHONE
  18. platform="hi,大家好,我是IPHONE平台";
  19. #elif UNITY_ANDROID
  20. platform="hi,大家好,我是ANDROID平台";
  21. #elif UNITY_STANDALONE_OSX
  22. platform="hi,大家好,我是OSX平台";
  23. #elif UNITY_STANDALONE_WIN
  24. platform="hi,大家好,我是Windows平台";
  25. #endif
  26. Debug.Log("Current Platform:" + platform);
  27. }
  28. }

上面假设我是在Editor状态下的话,就能看见打印出:     

我们也能够自定义宏定义,在PlayerSetting中定义:

比如我在上面圈起来的地方填写一个CUSTOM_ITF这个预编译指令。然后在DebugPlatformMesaage函数尾部增加这句话

  1. //新加入的内容
  2. #if CUSTOM_ITF
  3. customMsg = "我自己定义了预编译";
  4. #endif
  5. Debug.Log(customMsg);

然后我们执行看下,你就能在控制台看见我们输出的信息了:

当我们把我们自己定义的宏定义给去掉的时候,我们打印的信息就不会出来。

除了这些,unity中还有各个版本号的宏定义,一般开发插件的大牛都会用到。

UNITY_2_6 Platform define for the major version of Unity 2.6.
UNITY_2_6_1 Platform define for specific version 1 from the major release 2.6.
UNITY_3_0 Platform define for the major version of Unity 3.0.
UNITY_3_0_0 Platform define for the specific version 0 of Unity 3.0.
UNITY_3_1 Platform define for major version of Unity 3.1.
UNITY_3_2 Platform define for major version of Unity 3.2.
UNITY_3_3 Platform define for major version of Unity 3.3.
UNITY_3_4 Platform define for major version of Unity 3.4.
UNITY_3_5 Platform define for major version of Unity 3.5.
UNITY_4_0 Platform define for major version of Unity 4.0.
UNITY_4_0_1 Platform define for major version of Unity 4.0.1.
UNITY_4_1 Platform define for major version of Unity 4.1.
UNITY_4_2 Platform define for major version of Unity 4.2.
 
<span style="margin: 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; font-size: 14px;">    事实上预编译在我们对unity开发的时候另一个非常好的作用,我们Debug的时候是IO,事实上会消耗CPU的,从而影响了我们的性能,我们想在开发的时候进行Debug,但不想在到处的时候打印出,在这个时候我们就能够用预编译来推断他是否是在Editor状态下,从而做出对应的操作!</span><br style="margin: 0px; padding: 0px;" /></span>
<span style="margin: 0px; padding: 0px; font-size: 14px;">    预处理命令从来不会被翻译为可运行中的命令,但会影响编译过程的各个方面。比如:使用预处理器指令能够禁止编译器编译代码的某一部分,假设计划公布两个版本号的代码,即基本版本号和有很多其它功能的企业版本号,即能够使用这些预处理指令。在编译软件的基本版本号时,使用预处理器指令还能够禁止编译器编译于额外相关的代码。另外,在编写提供调试信息的代码时,也能够使用预处理器指令。在销售软件时,一般不希望编译这部分代码。预处理器指令开头都有符号#。我们unity的不是分专业版和免费版吗,事实上有可能就是用到了预编译,从而编译不同的版本号。<br style="margin: 0px; padding: 0px;" /><br style="margin: 0px; padding: 0px;" />  <br style="margin: 0px; padding: 0px;" />  假设对预编译不熟的同学能够去百度,或者谷歌下,我也是在查阅后才明确的!</span>

[unity3d]unity平台的预处理的更多相关文章

  1. unity平台的预处理

    unity平台的预处理     在开发中,特别是unity的跨平台中,我们经常会在各个平台游走,如安卓版,苹果版,PC版.......在此不同的平台上,有可能我们需要做不同的操作.然而我们就可以用un ...

  2. [Unity3D]Unity资料大全免费分享

     都是网上找的连七八糟的资料了,整理好分享的,有学习资料,视频,源码,插件……等等 东西比较多,不是所有的都是你需要的,可以按  ctrl+F 来搜索你要的东西,如果有广告,不用理会,关掉就可以了,如 ...

  3. (转)Unity3D移动平台动态读取外部文件全解析

    Unity3D移动平台动态读取外部文件全解析 c#语言规范 阅读目录 前言: 假如我想在editor里动态读取文件 移动平台的资源路径问题 移动平台读取外部文件的方法 补充: 回到目录 前言: 一直有 ...

  4. 慕容小匹夫 Unity3D移动平台动态读取外部文件全解析

    Unity3D移动平台动态读取外部文件全解析   c#语言规范 阅读目录 前言: 假如我想在editor里动态读取文件 移动平台的资源路径问题 移动平台读取外部文件的方法 补充: 回到目录 前言: 一 ...

  5. Unity3D 多平台 预编译 宏定义

    平台定义 UNITY_EDITOR 编辑器调用. UNITY_STANDALONE_OSX 专门为Mac OS(包括Universal,PPC和Intelarchitectures)平台的定义. UN ...

  6. Unity3d各平台资源路径文件夹

    之前一直是PC项目,公司终于考虑移动平台了,但是试验了几把,感觉移动平台资源管理路径还是有很多隐藏的注意事项. 比如在PC上可以做到随便读写,但是在移动平台就涉及到权限问题. 看到小伙伴的总结,还是要 ...

  7. Unity3D 多平台_预编译相关宏定义

    http://www.cnblogs.com/zhaoqingqing/p/3510332.html API地址:http://docs.unity3d.com/Documentation/Manua ...

  8. unity3d各平台通讯原生的平台API的说明

    注意:unity3d与原生代码的调用需要pro版本,此点注意了. 一.IOS平台,由于IOS平台的原生应该是objectC,所以通讯起来非常的简单, 1.原生代码调用u3d代码: 1.1.在Xcode ...

  9. Unity3D移动平台动态读取外部文件全解析

    前言: 一直有个想法,就是把工作中遇到的坑通过自己的深挖,总结成一套相同问题的解决方案供各位同行拍砖探讨.眼瞅着2015年第一个工作日就要来到了,小匹夫也休息的差不多了,寻思着也该写点东西活动活动大脑 ...

随机推荐

  1. grep理解

    http://www.cnblogs.com/ggjucheng/archive/2013/01/13/2856896.html部分摘录于此 grep与正规表达式  字符类 字符类的搜索:如果我想要搜 ...

  2. Codeforces Round #439 (Div. 2) C. The Intriguing Obsession

    C. The Intriguing Obsession 题目链接http://codeforces.com/contest/869/problem/C 解题心得:     1.由于题目中限制了两个相同 ...

  3. PHP 和 AJAX MySQL

    AJAX 可用来与数据库进行交互式通信. AJAX 数据库实例 在下面的 AJAX 实例中,我们将演示网页如何使用 AJAX 技术从 MySQL 数据库中读取信息. 在下拉列表中选择一个名字 (测试说 ...

  4. Python基础之字符编码,文件操作流与函数

    一.字符编码 1.字符编码的发展史 阶段一:现代计算机起源于美国,最早诞生也是基于英文考虑的ASCII ASCII:一个Bytes代表一个字符(英文字符/键盘上的所有其他字符),1Bytes=8bit ...

  5. 10,*args **kwargs 函数的命名空间。

    用户传入到函数中的实参数量不定时,或者是为了以后拓展, 此时要用到动态参数*args,**kwargs(万能参数.) *args(接受的是所有的位置参数) 以元组的形式返回给函数调用者. **kwar ...

  6. sql自动审核工具-inception

    [inception使用规范及说明文档](http://mysql-inception.github.io/inception-document/usage/)[代码仓库](https://githu ...

  7. BZOJ 3569 DZY Loves Chinese II ——线性基

    [题目分析] 腊鸡题目卡题面. 大概的意思就是给一张无向图,每次删掉其中一些边,问是否联通. 首先想到的是Bitset,可以做到n^2/64.显然过不了. 然而这是lyd在给我们讲线性基的时候的一道题 ...

  8. Uva5009 Error Curves

    已知n条二次曲线si(x) = ai*x^2 + bi*x + ci(ai ≥ 0),定义F(x) = max{si(x)},求出F(x)在[0,1000]上的最小值. 链接:传送门 分析:最大值最小 ...

  9. [HDU-4825] Xor-Sum (01字典树)

    Problem Description Zeus 和 Prometheus 做了一个游戏,Prometheus 给 Zeus 一个集合,集合中包含了N个正整数,随后 Prometheus 将向 Zeu ...

  10. 程序自动分析(codevs 4600)

    题目描述 Description 在实现程序自动分析的过程中,常常需要判定一些约束条件是否能被同时满足. 考虑一个约束满足问题的简化版本:假设x1,x2,x3,…代表程序中出现的变量,给定n个形如xi ...