zoukankan      html  css  js  c++  java
  • [unity3d]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.
     

     

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

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

    上面假设我是在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>
  • 相关阅读:
    Android开发-API指南-Manifest介绍
    MSP430G2333下位机乘法运算需要注意的一个问题
    VC++ 6.0使用定时器SetTimer;
    QT编写上位机程序一定要初始化变量以及谨慎操作指针
    QT点击"X"按钮,调用closeEvent()函数来实现调用特定事件(附:粗略介绍QT的信号与槽的使用方法)
    部分LINUX系统由图形界面启动变更为命令行界面启动的方法
    示波器trigger的使用方法
    QT共享库的创建与调用(初级)(附:UI界面不能被改变的其中一个原因)
    随笔:开篇——加入博客园的第N天,变量N无法用char来装载
    c语言课程设计之贪吃蛇代码及思路 c语言课程设计报告之贪吃蛇
  • 原文地址:https://www.cnblogs.com/gcczhongduan/p/4026181.html
Copyright © 2011-2022 走看看