Unity3D平台宏定义

发表于2018-09-28
评论0 1.16w浏览
Unity包含一个“平台相关的编译”功能。这包括一些预处理指令,让你分割你的脚本编译和专为支持的平台之一执行代码段。

您可以Unity编辑器中运行代码,这样你就可以专门为你的目标平台编译代码,并在编辑器中测试!

平台#define指令

Unity支持脚本的平台#define指令如下:
Property:Function:
UNITY_EDITORUnity编辑器
UNITY_EDITOR_WINWindows 操作系统.
UNITY_EDITOR_OSXmacos操作系统
UNITY_STANDALONE_OSX专门为macos(包括Universal, PPC,Intel architectures)平台的定义
UNITY_STANDALONE_WIN专门为windows平台的定义
UNITY_STANDALONE_LINUX专门为Linux平台的定义
UNITY_STANDALONE独立平台 (Mac OS X, Windows or Linux).
UNITY_WIIWII 游戏机平台
UNITY_IOSiOS系统平台
UNITY_IPHONEiPhone
UNITY_ANDROIDandroid系统平台
UNITY_PS4ps4平台
UNITY_SAMSUNGTV三星TV平台
UNITY_XBOXONEXbox One 平台
UNITY_TIZENTizen 平台
UNITY_TVOSApple TV 平台
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_WINRTUNITY_WSA.
UNITY_WINRT_10_0UNITY_WSA_10_0
UNITY_WEBGL#define directive for WebGL.
UNITY_FACEBOOKfaceBook平台(WebGL or Windows standalone).
UNITY_ADS调用广告方法,版本 5.2 以后
UNITY_ANALYTICS调用unity分析服务,版本5.2以后
UNITY_ASSERTIONS控制指令的过程

Unity版本判定方式:UNITY_X,UNITY_X_Y,UNITY_X_Y_Z例如:
UNITY_5Unity5版本,包含所有的5.x.y版本
UNITY_5_0Unity5.0版本,包含所有的5.0.x版本
UNITY_5_0_1Unity5.0.1版本

打包的时候,选择File>Build Settings然后显示平台选择界面。

代码实例C#:
// C#
using UnityEngine;
using System.Collections;
public class PlatformDefines : MonoBehaviour {
  void Start () {
    #if UNITY_EDITOR
      Debug.Log("Unity Editor");
    #endif
    #if UNITY_IOS
      Debug.Log("Iphone");
    #endif
    #if UNITY_STANDALONE_OSX
    Debug.Log("Stand Alone OSX");
    #endif
    #if UNITY_STANDALONE_WIN
      Debug.Log("Stand Alone Windows");
    #endif
  }          
}

可以使用#if #elif
#if UNITY_EDITOR    
    Debug.Log("Unity Editor");
 #elif UNITY_IOS    
    Debug.Log("Unity iPhone"); #else
    Debug.Log("Any other platform"); 
#endif

可以自定义宏定义,打开Other Settings窗口,选择Player Settings>Scripting Define Symbols

如社区发表内容存在侵权行为,您可以点击这里查看侵权投诉指引