Skip to content

Instantly share code, notes, and snippets.

@huihut
Last active January 13, 2019 06:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save huihut/fa493056ecd227ada1feca1c6f00fe94 to your computer and use it in GitHub Desktop.
Save huihut/fa493056ecd227ada1feca1c6f00fe94 to your computer and use it in GitHub Desktop.
Unity Platform dependent compilation

Platform dependent compilation
nity3D 多平台 预编译 宏定义

平台定义

UNITY_EDITOR 编辑器调用。

UNITY_STANDALONE_OSX 专门为Mac OS(包括Universal,PPC和Intelarchitectures)平台的定义。

UNITY_DASHBOARD_WIDGET Mac OS Dashboard widget (Mac OS仪表板小部件)。

UNITY_STANDALONE_WIN Windows 操作系统。

UNITY_STANDALONE_LINUX Linux的独立的应用程序。

UNITY_STANDALONE 独立的平台(Mac,Windows或Linux)。

UNITY_WEBPLAYER 网页播放器(包括Windows和Mac Web播放器可执行文件)。

UNITY_WII Wii游戏机平台。

UNITY_IPHONE iPhone平台。

UNITY_ANDROID Android平台。

UNITY_PS3 PlayStation 3。

UNITY_XBOX360 Xbox 360。

UNITY_NACL 谷歌原生客户端(使用这个必须另外使用UNITY_WEBPLAYER)。

UNITY_FLASH Adobe Flash。

判断Unity版本

也可以判断Unity版本,目前支持的版本

UNITY_2_6 平台定义为主要版本的Unity 2.6。

UNITY_2_6_1 平台定义的特定版本1的主要版本2.6。

UNITY_3_0 平台定义为主要版本的Unity 3.0。

UNITY_3_0_0 平台定义的特定版本的Unity 3.0 0。

UNITY_3_1 平台定义为主要版本的Unity 3.1。

UNITY_3_2 平台定义为主要版本的Unity 3.2。

UNITY_3_3 平台定义为主要版本的Unity 3.3。

UNITY_3_4 平台定义为主要版本的Unity 3.4。

UNITY_3_5 平台定义为主要版本的Unity 3.5。

UNITY_4_0 平台定义为主要版本的Unity 4.0。

UNITY_4_0_1 主要版本4.0.1统一的平台定义。

UNITY_4_1 平台定义为主要版本的Unity 4.1。

UNITY_4_2 平台定义为主要版本的Unity 4.2。

运行平台

//获得当前运行平台
   Debug.Log("plat = " + Application.platform);
 
//可以获取到的平台类型  
   public enum RuntimePlatform  
    {  
        OSXEditor = 0,  
        OSXPlayer = 1,  
        WindowsPlayer = 2,  
        OSXWebPlayer = 3,  
        OSXDashboardPlayer = 4,  
        WindowsWebPlayer = 5,  
        WiiPlayer = 6,  
        WindowsEditor = 7,  
        IPhonePlayer = 8,  
        PS3 = 9,  
        XBOX360 = 10,  
        Android = 11,  
        NaCl = 12,  
        LinuxPlayer = 13,  
        FlashPlayer = 15,  
    }

示例

// 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_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 UNITY_EDITOR
    Debug.Log("Unity Editor");

#elif UNITY_IOS
    Debug.Log("Unity iPhone");

#else
    Debug.Log("Any other platform");

#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment