zoukankan      html  css  js  c++  java
  • Unity宏+RSP文件定义宏

           参考文档:https://docs.unity3d.com/Manual/PlatformDependentCompilation.html

           在Unity开发中,可以使用预编译条件,宏定义。比如在一个cs文件中

    #define DevTest
    
    using UnityEngine;
    
    namespace Test
    {
        public class Dev : MonoBehaviour                                      
        {
            public void Start()
            {
    #if DevTest
                DoSomeThing();
    #else 
                DoSomeThing2();
    #endif
            }
    
            private void DoSomeThing()
            {
            }
    
            private void DoSomeThing2()
            {
            }
        }
    }

           在文件的首行定义一个局部宏,仅限在本文件中有效。然后使用#if的方式判断是否存在这个宏,如果存在则运行DoSomeThing1,否则运行DoSomeThing2。注意编译时,会把不需要运行的代码移除掉。所以编译出来的代码中Start函数中不会出现DoSomeThing2。

           除了这种我们定义的局部宏,还有一种是全局宏,在整个工程中都生效。比如

    #if UNITY_EDITOR
    
        DoSomething();
    
    #elif UNITY_ANDROID
    
        DoSomething2();
    
    #endif

           表示分别在编辑器环境下和android环境下做某些处理。

           这个宏是Unity提供的全局宏。注意:本文以下的宏都是指全局宏

    Unity中的宏

           这类的宏定义有以下几种。

    平台类宏

            由于各个平台的实现和接口都不同,所以很多时候需要根据平台来做。Unity提供了覆盖了大部分设备的宏定义。

    • UNITY_EDITOR #define directive for calling Unity Editor scripts from your game code.
    • UNITY_EDITOR_WIN #define directive for Editor code on Windows.
    • UNITY_EDITOR_OSX #define directive for Editor code on Mac OS X.
    • UNITY_STANDALONE_OSX #define directive for compiling/executing code specifically for Mac OS X (including Universal, PPC and Intel architectures).
    • UNITY_STANDALONE_WIN #define directive for compiling/executing code specifically for Windows standalone applications.
    • UNITY_STANDALONE_LINUX #define directive for compiling/executing code specifically for Linux standalone applications.
    • UNITY_STANDALONE #define directive for compiling/executing code for any standalone platform (Mac OS X, Windows or Linux).
    • UNITY_WII #define directive for compiling/executing code for the Wii console.
    • UNITY_IOS #define directive for compiling/executing code for the iOS platform.
    • UNITY_IPHONE Deprecated. Use UNITY_IOS instead.
    • UNITY_ANDROID #define directive for the Android platform.
    • UNITY_PS4 #define directive for running PlayStation 4 code.
    • UNITY_SAMSUNGTV #define directive for executing Samsung TV code.
    • UNITY_XBOXONE #define directive for executing Xbox One code.
    • UNITY_TIZEN #define directive for the Tizen platform.
    • UNITY_TVOS #define directive for the Apple TV platform.
    • UNITY_WSA #define directive for Universal Windows Platform. Additionally, NETFX_CORE is defined when compiling C# files against .NET Core and using .NET scripting backend.
    • UNITY_WSA_10_0 #define directive for Universal Windows Platform. Additionally WINDOWS_UWP is defined when compiling C# files against .NET Core.
    • UNITY_WINRT Same as UNITY_WSA.
    • UNITY_WINRT_10_0 Equivalent to UNITY_WSA_10_0
    • UNITY_WEBGL #define directive for WebGL.
    • UNITY_FACEBOOK #define directive for the Facebook platform (WebGL or Windows standalone).
    • UNITY_ADS #define directive for calling Unity Ads methods from your game code. Version 5.2 and above.
    • UNITY_ANALYTICS #define directive for calling Unity Analytics methods from your game code. Version 5.2 and above.
    • UNITY_ASSERTIONS #define directive for assertions control process.

    Unity版本类宏

           由于Unity每个版本的API可能不同,而项目成员使用的是不同版本的Unity。这时候就需要根据版本分别实现。

           Unity提供了2.6.0之后的大部分宏定义,形如UNITY_X或者UNITY_X_Y或者UNITY_X_Y_Z,比如UNITY_2017_1_0表示Unity2017.1.0版本。注意Unity并没有提供patch版本的宏定义,后面会说明如何实现自定义的宏

    代码编译类宏

           这类宏有

    • ENABLE_MONO Scripting backend #define for Mono.
    • ENABLE_IL2CPP Scripting backend #define for IL2CPP.
    • ENABLE_DOTNET Scripting backend #define for .NET.
    • NETFX_CORE Defined when building scripts against .NET Core class libraries on .NET.
    • NET_2_0 Defined when building scripts against .NET 2.0 API compatibility level on Mono and IL2CPP.
    • NET_2_0_SUBSET Defined when building scripts against .NET 2.0 Subset API compatibility level on Mono and IL2CPP.
    • NET_4_6 Defined when building scripts against .NET 4.6 API compatibility level on Mono and IL2CPP.
    • ENABLE_WINMD_SUPPORT Defined when Windows Runtime support is enabled on IL2CPP and .NET. See Windows Runtime Support for more details.

    自定义宏

           在C#工程中,我们是可以通过Project上的设置类定义自己的宏。如下图,通过在Project的属性页面中可以配置宏。

    2017111701

           但是在Unity生成的工程中是无法打开Project的属性配置界面。因此无法在C#工程中进行配置。

    使用PlayerSetting定义宏

           要进行自定义的宏,需要在Unity的PlayerSetting中进行设置,多个宏定义间用分号“;”隔开。设置的数据会被存储在项目的ProjectSettings/ProjectSettings.asset文件中。

    2017111702

           由于ProjectSettings/ProjectSettings.asset一般会被纳入版本管理,所以项目的全体成员一般都会同步宏定义。但是有时候存在单独的某些成员,需要自己单独的自定义宏的情况。

    特殊案例

           在Unity2017.2.0f3(f3版本)和Unity2017.2.0p2(p2版本)这2个版本中,由于某些API接口的变更,p2版本书写的代码就在f3版本中报错。f3版本是项目大多数成员使用的版本,p2版本为少数人使用的版本。但是由于Unity版本的版本宏定义中只有UNITY_2017_2_0,无法区分是f3版本和p2版本,所以只能使用自定义宏。但是由于PlayerSetting会被同步,所以又会造成全部开启宏的情况,f3版本和p2版本依然无法区分。

    使用RSP文件定义宏

           Unity提供一个方法,可以用一个rsp文件来定义宏。然后将rsp文件不要纳入版本库。这样就可以解决特殊案例中问题。

           一般来说开发Unity用的是C#,那么这个rsp文件必须命名为mcs.rsp并且放在Assets目录下。使用rsp文件定义宏,只需要每一行输入一个“-define:<宏名称>”即可,比如这里随意增加2个宏定义:

    -define:DevTest

    -define:UNITY_2017_2_0_P2

            输入完成后保存退出,在Unity中对任意一个代码reimort一次,使Unity重新编译。关闭并重新打开代码编辑器就会看到这个DevTest的全局宏已经生效。

    特殊案例的建议

             大部分时候还是建议使用PlayerSetting定义宏,统一使用同一个版本的Unity。

    转载注明:http://www.codegize.com http://www.cnblogs.com/CodeGize/

    个人博客请访问:http://www.cnblogs.com/CodeGize/
  • 相关阅读:
    navicat的快捷键
    NoSQL Redis的学习笔记
    awk的使用
    把自己的电脑做服务器发布tomcat的项目外网访问
    linux系统备份
    系统自动化配置和管理工具:SaltStack
    RSync实现文件备份同步
    传送文件
    面试题
    闭包closure
  • 原文地址:https://www.cnblogs.com/CodeGize/p/7853121.html
Copyright © 2011-2022 走看看