`
wgcode
  • 浏览: 593519 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

Unity3D平台依赖编译

 
阅读更多

Platform Dependent Compilation

 

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!

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_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_FLASH Platform define when compiling code for Adobe Flash.
UNITY_BLACKBERRY Platform define for a Blackberry10 device.
UNITY_WP8 Platform define for Windows Phone 8.
UNITY_METRO Platform define for Windows Store Apps (additionally NETFX_CORE is defined when compiling C# files against .NET Core).
UNITY_WINRT Equivalent to UNITY_WP8 |UNITY_METRO

Also you can compile code selectively depending on the version of the engine you are working on. Currently the supported ones are:

 
UNITY_2_6 Platform define for the major version of Unity 2.6.
UNITY_2_6_1 Platform define for specific version 2.6.1.
UNITY_3_0 Platform define for the major version of Unity 3.0.
UNITY_3_0_0 Platform define for specific version 3.0.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 specific version 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_4_3 Platform define for major version of Unity 4.3.
UNITY_4_5 Platform define for major version of Unity 4.5.

Note: For versions before 2.6.0 there are no platform defines as this feature was first introduced in that version.

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 File -> Build Settings. This will bring the build settings window to select your target platform.

Build Settings window with the WebPlayer Selected as Target platform.Build Settings window with the WebPlayer Selected as Target platform.

Select the platform you want to test your precompiled code against and press the Switch Editor 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

  }          
}


// Boo
import UnityEngine

class PlatformDefines (MonoBehaviour): 

    def Start ():
        ifdef UNITY_EDITOR:
            Debug.Log("Unity Editor")

        ifdef UNITY_IPHONE:
            Debug.Log("IPhone")

        ifdef UNITY_STANDALONE_OSX:
            Debug.Log("Stand Alone OSX")

        ifdef not UNITY_IPHONE:
            Debug.Log("not an iPhone")



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


However, Boo currently supports only the ifdef directive.

Platform Custom Defines

It is also possible to add to the 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.

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 the extension is .rsp:

 
C# <Project Path>/Assets/smcs.rsp
C# - Editor Scripts <Project Path>/Assets/gmcs.rsp
UnityScript <Project Path>/Assets/us.rsp
Boo <Project Path>/Assets/boo.rsp

As an example, if you include the single line “-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, .cs or .boo) 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 AssetBundle反编译工具,AssetStudio.v0.15.47

    AssetStudio是一款专门针对Unity3d AssetBundle的反编译工具,它可以帮助开发者分析、查看和提取AssetBundle中的内容,对于调试、优化和学习Unity资源管理有着极大的帮助。 AssetStudio.v0.15.47是该工具的一个版本...

    Unity3D高级手册中文版

    在Unity3D手册中提到了BuildPlayer管道,这是一个用于编译和打包游戏到不同平台的流程。通过掌握这个管道,开发者可以更好地控制游戏的发布流程。 12. 命令行参数(Command Line Arguments) 命令行参数为自动化...

    unity3d解包工具

    在Unity中,所有的资源都会被编译成特定的二进制格式,如`.unity3d`、`.asset`、`.meta`等,这些文件包含了经过Unity引擎处理后的数据,以便于在不同平台上高效运行。然而,这种编译后的格式对于开发者来说并不透明...

    SQLite4Unity3d-master.zip

    《SQLite4Unity3d:在Unity3D中集成SQLite数据库的实践详解》 SQLite4Unity3d是一个专为Unity3D游戏引擎设计的SQLite数据库集成解决方案。它允许开发者在Unity项目中利用SQLite的强大功能,存储和检索游戏数据,如...

    unity.studio反编译工具

    首先,Unity Studio的核心功能是反编译Unity的资源文件(.unity3d格式)。Unity的资源通常被编译成二进制格式,以便在游戏运行时高效加载。Unity Studio能够解析这些二进制文件,将其转换回原始的资源格式,如fbx...

    Unity3D如何导出为APP安装包(苹果版ipa)教程.zip

    Unity3D是一款强大的跨平台游戏开发工具,广泛用于创建2D和3D应用程序,包括游戏、虚拟现实体验和增强现实项目。本教程将详细介绍如何使用Unity3D将项目导出为适用于苹果iOS设备的APP安装包(ipa文件)。在iOS平台上...

    unity3d ios平台,接入第三方sdk

    在Unity3D游戏开发中,将游戏发布到iOS平台时常需要接入第三方SDK,以实现诸如广告展示、社交网络分享、用户登录验证、支付系统等功能。本教程将详细讲解如何在Unity3D中接入iOS的第三方SDK,以实现跨平台的游戏功能...

    [手游] Unity3D《最后一战》全套完整源代码 + 资源

    【手游开发】Unity3D是当今最流行的跨平台游戏开发引擎之一,尤其在移动游戏领域,它的强大功能和易用性使其成为许多开发者的选择。本资源包含的是《最后一战》这款手游的完整源代码和相关资源,对于学习Unity3D游戏...

    disunity -- 反编译Unity3d资源

    Unity3d是一款强大的跨平台游戏开发引擎,被广泛应用于制作2D、3D游戏以及交互式体验。在游戏开发过程中,资源文件(如模型、纹理、脚本等)会被Unity编译成特定的二进制格式,以提高加载速度和运行效率。然而,这种...

    unity3d-websocket-code.zip

    Unity3D是一个强大的跨平台游戏开发引擎,广泛用于创建2D和3D的互动内容,包括游戏、模拟器以及各种可视化应用。WebSocket则是一种在互联网上进行双向通信的协议,它允许客户端和服务器之间建立持久的连接,进行高效...

    unity3d面试题复习资料

    ### Unity3D 面试复习知识点概览 #### 一、Unity3D中的相机模式 Unity3D支持两种相机模式: 1. **透视投影**(Perspective):这种模式下,距离摄像机远的物体看起来更小,适用于模拟真实世界的视觉效果。 2. **...

    unity3d simple game

    Unity3D是一款强大的跨平台游戏开发引擎,它以其易用性、高效性能和广泛支持的平台而备受开发者喜爱。在这个“unity3d simple game”项目中,我们将深入探讨如何利用Unity3D创建一个基础的游戏。该项目是基于一个...

    Unity3D调整屏幕亮度、饱和度、对比度

    Unity3D是一款强大的跨平台游戏开发引擎,广泛用于制作2D和3D游戏以及互动式体验。在开发过程中,有时我们需要根据游戏环境或者用户需求来调整屏幕的视觉效果,如亮度、饱和度和对比度。这些参数对于创建游戏的视觉...

    Unity反编译中心

    Unity是世界上最流行的跨平台游戏引擎之一,使用C#作为主要编程语言。C#源代码是游戏逻辑和系统功能的核心,但通常在发布时被编译为IL(中间语言)或直接打包到最终的可执行文件中,这使得它们对外部不可见。Unity反...

    UNITY3D水果忍者游戏源代码程序

    Unity3D是一款强大的跨平台游戏开发引擎,广泛应用于2D和3D游戏制作。本资源提供的"UNITY3D水果忍者游戏源代码程序"是基于Unity3D开发的一款经典游戏——水果忍者的源代码,非常适合初学者和有经验的开发者用来学习...

    Unity3D 2D小游戏开发框架,内置游戏基本UI功能,音效管理,简单粒子效果,https网络通讯封装等。

    Unity3D是一款强大的跨平台游戏开发工具,特别适合2D和3D游戏的制作。本框架专注于2D小游戏的开发,提供了丰富的功能模块,旨在简化游戏开发流程,提高开发效率。接下来,我们将深入探讨该框架包含的核心知识点。 ...

    Unity3d怎么嵌入抓猫聚合平台广告

    在Unity3d游戏开发中,集成第三方广告平台如抓猫聚合SDK,可以帮助开发者实现游戏内的广告展示,从而获得收益或推广。以下将详细介绍如何在Unity3d中嵌入抓猫聚合平台广告的步骤: 1. **获取必要的资源**:首先,你...

    Unity3D直接运行Lua语言

    Unity3D是一款强大的跨平台游戏开发引擎,广泛用于制作2D和3D游戏、模拟器以及可视化应用。Lua是一种轻量级的脚本语言,因其简洁的语法和高效的执行性能,常常被用作游戏开发中的嵌入式脚本语言。在Unity3D中集成Lua...

    unity3d 打包成webgl 错误解决办法

    Unity3D是一款强大的跨平台游戏开发引擎,支持创建2D和3D游戏,以及互动式三维应用程序。在尝试将项目打包成WebGL格式时,开发者可能会遇到一些问题,特别是与il2cpp相关的错误。"il2cpp.exe did not run properly!...

    Unity3D Editor类(Inspector)经验总结1

    【Unity3D Editor类(Inspector)经验总结】 在Unity3D开发中,Editor类用于自定义游戏对象在Inspector视图中的显示方式。以下是一些关键知识点: 1. **使用GUI和GUILayout**: - Editor类中的控件绘制是基于GUI...

Global site tag (gtag.js) - Google Analytics