Unity3D -- 平台宏定义

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

Property: Function:
UNITY_EDITOR Unity编辑器
UNITY_EDITOR_WIN Windows 操作系统.
UNITY_EDITOR_OSX macos操作系统
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_WII WII 游戏机平台
UNITY_IOS iOS系统平台
UNITY_IPHONE iPhone
UNITY_ANDROID android系统平台
UNITY_PS4 ps4平台
UNITY_SAMSUNGTV 三星TV平台
UNITY_XBOXONE Xbox One 平台
UNITY_TIZEN Tizen 平台
UNITY_TVOS Apple 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_WINRT UNITY_WSA.
UNITY_WINRT_10_0 UNITY_WSA_10_0
UNITY_WEBGL #define directive for WebGL.
UNITY_FACEBOOK faceBook平台(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_5 unity5版本, 包含所有的5.x.y版本
UNITY_5_0 Unity5.0版本,包含所有的5.0.x版本
UNITY_5_0_1 Unity5.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