unity平台的预处理

 
 

在开发中,特别是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.
 

以后我们可以根据如上宏定义就可以去轻而易举的很容易去在我们代码中加入判断了。我举个简单例子,如下:

using UnityEngine;
using System.Collections; public class Recompile : MonoBehaviour
{ private string platform = string.Empty;
// Use this for initialization
void Start()
{
DebugPlatformMesaage();
} void DebugPlatformMesaage()
{ #if UNITY_EDITOR
platform = "hi,大家好,我是在unity编辑模式下";
#elif UNITY_XBOX360
platform="hi,大家好,我在XBOX360平台";
#elif UNITY_IPHONE
platform="hi,大家好,我是IPHONE平台";
#elif UNITY_ANDROID
platform="hi,大家好,我是ANDROID平台";
#elif UNITY_STANDALONE_OSX
platform="hi,大家好,我是OSX平台";
#elif UNITY_STANDALONE_WIN
platform="hi,大家好,我是Windows平台";
#endif
Debug.Log("Current Platform:" + platform);
}
}

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

我们也可以自己定义宏定义,在PlayerSetting中定义:

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

//新添加的内容
#if CUSTOM_ITF
customMsg = "我自定义了预编译";
#endif
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.
 
    其实预编译在我们对unity开发的时候还有一个很好的作用,我们Debug的时候是IO,其实会消耗CPU的,从而影响了我们的性能,我们想在开发的时候进行Debug,但不想在到处的时候打印出,在这个时候我们就可以用预编译来判断他是否是在Editor状态下,从而做出相应的操作!
    预处理命令从来不会被翻译为可执行中的命令,但会影响编译过程的各个方面。例如:使用预处理器指令可以禁止编译器编译代码的某一部分,如果计划发布两个版本的代码,即基本版本和有更多功能的企业版本,即可以使用这些预处理指令。在编译软件的基本版本时,使用预处理器指令还可以禁止编译器编译于额外相关的代码。另外,在编写提供调试信息的代码时,也可以使用预处理器指令。在销售软件时,一般不希望编译这部分代码。预处理器指令开头都有符号#。我们unity的不是分专业版和免费版吗,其实有可能就是用到了预编译,从而编译不同的版本。

  如果对预编译不熟的同学可以去百度,或者谷歌下,我也是在查阅后才明白的!

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

  1. [unity3d]unity平台的预处理

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

  2. Unity 平台依赖编译

    位置:unity文档-Manual-Scripting-Platform dependent compilation Property: Function: UNITY_EDITOR #define ...

  3. Unity 平台宏定义

    官方文档: https://docs.unity3d.com/Manual/PlatformDependentCompilation.html

  4. Unity 3D游戏开发引擎:最火的插件推荐

    摘要:为了帮助使用Unity引擎的开发人员制作更完美的游戏.我们精心挑选了十款相关开发插件和工具.它们是:2D Toolkit.NGUI.Playmaker.EasyTouch & EasyJ ...

  5. Unity开发之实现更换鼠标图片

    在玩游戏的时候,感觉游戏里的鼠标图片特酷炫,23333,今天我就总结了两种方法! 我是做Unity开发的,所以方法仅针对于Unity平台........ 方法如下: 1.Unity客户端直接更改,步骤 ...

  6. Unity摄像机围绕物体旋转两种实现方式

    第一种,使用Transform 函数 RotateAround. 代码如下: public Transform target;//获取旋转目标 private void camerarotate() ...

  7. Unity 下集成第三方原生 SDK,以极光厂商通道为例

    Unity中集成三方SDK有两种方式: Unity 项目开发中时常有集成 Android 第三方 SDK 的需求,比如接入第三方推送,分享等功能.而第三方 SDK 的集成文档提到的往往是基于原生 An ...

  8. Unity Editor Console Pro 扩展点击定位到外部工程

    链接 http://blog.csdn.net/akof1314/article/details/53232981 http://forum.china.unity3d.com/thread-2689 ...

  9. Unity插件

    1.2D Toolkit 如果过去你一直从事3D游戏开发,最近想转做2D游戏,最好尝试一下2D Toolkit.2D Toolkit是一款2D开发组件,它具有很强的灵活性和适应性,能够让开发者在Uni ...

随机推荐

  1. Caffe框架下的图像回归测试

    Caffe框架下的图像回归测试 参考资料: 1. http://stackoverflow.com/questions/33766689/caffe-hdf5-pre-processing 2. ht ...

  2. C/C++二维数组的用法

    二维数组在存储时按行优先连续存储,数组名是一个二维指针,如 int a[3][2] 中,a 是一个二维指针,而a[0],a[1],a[2]都相当于普通的一位数组的数组名,是一个固定值的指针. 二维数组 ...

  3. Foundation框架

    1.框架是由许多类.方法.函数.文档按照一定的逻辑组织起来的集合,以便使研发程序变的更容易 清除缓存,删除这个文件夹下的所有文件/Users/fanyafang/Library/Developer/X ...

  4. Oracle新表使用序列(sequence)作为插入值,初始值不是第一个,oraclesequence

    Oracle新表使用序列(sequence)作为插入值,初始值不是第一个,oraclesequence 使用oracle11g插入数据时遇到这样一个问题: 1 --创建测试表-- 2 CREATE T ...

  5. vs打开项目出现“尚未配置为Web项目XXXX指定的本地IIS URL HTTP://localhost:…… .要打开此项目,需要配置虚拟目录……”提示

    今天打开网上下载的一个源码,出现如标题的这个问题,这是从未遇见的提示.尝试点击是,但是网站还是运行不起来.于是网上搜索,就有了这篇. 解决的方案如下: 注意:也可以用记事本把工程文件(.vcxproj ...

  6. Spring之ClassPathResource加载资源文件

    先看Demo: 1 @Test 2 public void testClassPathResource() throws IOException { 3 Resource res = new Clas ...

  7. maven项目和普通项目转换

     

  8. Java8闭包

    闭包在很多语言中都存在,例如C++,C#.闭包允许我们创建函数指针,并把它们作为参数传递,Java编程语言提供了接口的概念,接口中可以定义抽象方法,接口定义了API,并希望用户或者供应商来实现这些方法 ...

  9. NOIP2014 day2 T2寻找道路

    #include<iostream> #include<cmath> #include<cstdlib> #include<cstdio> #inclu ...

  10. 关于小黄车(ofo共享单车)使用的问题

    小黄车即ofo共享单车,号称是全球创立最早.成长最快.规模最大的无桩共享单车创业公司,缔造了"共享单车"概念,致力于解决城市出行问题.它的出现给大家带来了方便,作为一个商业运行的公 ...