2018-08-14 10:04:11 Aa85641826 阅读数 317

Unity3d 项目开发切换平台与发布版本时,不同的平台,不同的版本,可能会有不同的实现方式,这样就需要宏去控制调用,执行需要在该平台的逻辑操作。

Unity预编译宏

Unity宏定义API:http://docs.unity3d.com/Documentation/Manual/PlatformDependentCompilation.html

手机开发通常使用下面4个

Property: Function:
UNITY_EDITOR #define directive for calling Unity Editor scripts from your game code.
UNITY_STANDALONE #define directive for compiling/executing code for any standalone platform (Mac OS X, Windows or Linux).
UNITY_IOS #define directive for compiling/executing code for the iOS platform.
UNITY_ANDROID #define directive for the Android platform.

 

使用方法

#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    

如果需要自定义宏需要在

 

进行设置 每个宏的名称用分号隔开。

这样每次需要的时候添加上去 不需要的时候删除就可以了。

实际开发中为确保误删除,我这里做了一个小工具,用来控制自定义的宏 打开与关闭。

先上代码

新建一个脚本Menu

using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

public class Menu {
    //在Unity菜单中创建一个菜单路径用于设置宏定义
    [MenuItem("L_Tools/Settings")]
    public static void Setting()
    {
        SettingsWindow win = (SettingsWindow)EditorWindow.GetWindow(typeof(SettingsWindow));
        win.titleContent = new GUIContent("全局设置");
        win.Show();
    }
}

新建一个脚本SettingWindow

using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

public class SettingsWindow : EditorWindow {

    private List<MacorItem> m_List =new List<MacorItem>();

    private Dictionary<string,bool> m_Dic =new Dictionary<string,bool>();

    private string m_Macor = null;
    public void OnEnable()
    {
        m_Macor = PlayerSettings.GetScriptingDefineSymbolsForGroup(BuildTargetGroup.Android);
        Debug.Log(m_Macor);
        m_List.Clear();
        //在这里加入你自己想要定义的宏
        m_List.Add(new MacorItem() { Name = "DEBUG_MODEL", DisplayName = "调试模式", IsDebug = true, IsRelease = false });
        m_List.Add(new MacorItem() { Name = "DEBUG_LOG", DisplayName = "打印日志", IsDebug = true, IsRelease = false });
        m_List.Add(new MacorItem() { Name = "STAT_TD", DisplayName = "开启统计", IsDebug = false, IsRelease = true });
        for (int i = 0; i < m_List.Count; i++)
        {
            if (!string.IsNullOrEmpty(m_Macor) && m_Macor.IndexOf(m_List[i].Name) == -1)
            {
                m_Dic[m_List[i].Name] = false;
            }
            else
            {
                m_Dic[m_List[i].Name] = true;
            }
        }
    }

    void OnGUI()
    {
        for(int i=0;i<m_List.Count;i++)
        {
        EditorGUILayout.BeginHorizontal("box");
        m_Dic[m_List[i].Name] = GUILayout.Toggle(m_Dic[m_List[i].Name], m_List[i].DisplayName);
        EditorGUILayout.EndHorizontal();
        }

        EditorGUILayout.BeginHorizontal("box");
        if(GUILayout.Button("保存",GUILayout.Width(100)))
        {
            SaveMacor();
        }

        if (GUILayout.Button("调试模式", GUILayout.Width(100)))
        {
            for (int i = 0; i < m_List.Count; i++)
            {
                m_Dic[m_List[i].Name] = m_List[i].IsDebug;
            }
            SaveMacor();
        }

        if (GUILayout.Button("发布模式", GUILayout.Width(100)))
        {
            for (int i = 0; i < m_List.Count; i++)
            {
                m_Dic[m_List[i].Name] = m_List[i].IsRelease;
            }
            SaveMacor();
        }
        EditorGUILayout.EndHorizontal();
    }
    private void SaveMacor()
    {
        m_Macor = string.Empty;
        foreach (var item in m_Dic)
        {
            if (item.Value)
            {
                m_Macor += string.Format("{0}:", item.Key);

            }
        }
        PlayerSettings.SetScriptingDefineSymbolsForGroup(BuildTargetGroup.Android, m_Macor);
        PlayerSettings.SetScriptingDefineSymbolsForGroup(BuildTargetGroup.iOS, m_Macor);
        PlayerSettings.SetScriptingDefineSymbolsForGroup(BuildTargetGroup.Standalone, m_Macor);
    }
    public class MacorItem
    {
        /// <summary>
        /// 名称
        /// </summary>
        public string Name;
        /// <summary>
        /// 显示的名称
        /// </summary>
        public string DisplayName;
        /// <summary>
        /// 是否调试xiang
        /// </summary>
        public bool IsDebug;
        ///是否发布项
        public bool IsRelease;
    }
}

在编辑器右上角寻找路径 L_Tools/Settings,打开窗口就可以动态设置自定义的宏了

2014-07-15 10:49:00 hackdjh 阅读数 2224

名称 描述
UNITY_EDITOR
Define for calling Unity Editor scripts from your game code.
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_NACL
Platform define when compiling code for Google native client (this will be set additionally to UNITY_WEBPLAYER).
UNITY_FLASH
Platform define when compiling code for Adobe Flash.
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 1 from the major release 2.6.
UNITY_3_0
Platform define for the major version of Unity 3.0.
UNITY_3_0_0
Platform define for the specific version 0 of Unity 3.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 major version of Unity 4.0.1.
UNITY_4_1
Platform define for major version of Unity 4.1.
根据平台和unity版本的宏变量,可以使用宏定义的方式来做平台和引擎的判断,例如:

[AppleScript] 纯文本查看 复制代码
?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
 
  }
}

2019-01-15 21:55:16 Czhenya 阅读数 318

Unity 宏定义官方文档地址:https://docs.unity3d.com/Manual/PlatformDependentCompilation.html

Unity宏定义的使用简例:

 void HongDingYi()
    {
#if UNITY_EDITOR
        Debug.Log("Unity 的编辑器...");
#elif UNITY_ANDROID || UNITY_IPHONE
     	Debug.Log("Android  &  iPhone 平台...");
#elif UNITY_STANDALONE
        Debug.Log("PC平台(包括window,Mac,Linux)...");       
#elif ZDY  //名称和框中填写的一致即可
        Debug.Log("自定义平台...");
#endif

//区分Unity版本
#if UNITY_5_6
         Debug.Log("也可以用来区分Unity的版本...5.6");
#elif UNITY_5_6_1
         Debug.Log("也可以用来区分Unity的版本...5.6.1");
#endif

#if UNITY_5_6_4
        Debug.Log("也可以用来区分Unity的版本...5.6.4");
#endif

    }

Bebug部分为灰色的,,说明当前的使用的宏定义和if条件中的不一致,,,满足当前条件的宏定义Bebug部分的代码颜色和不放在宏定义中代码颜色一致,,,


打开方式:File – BuildSettings – PlayerSetting – 找到要发布的平台设置(可以写多个宏定义中间使用“;’”分割开)

宏定义

2017-11-25 11:09:47 q764424567 阅读数 134

Unity3D 多平台_预编译相关宏定义

原创文章如需转载请注明:转载自 脱莫柔Unity3D学习之旅 Unity3D引擎技术交流QQ群:【119706192本文链接地址: Unity3D 多平台_预编译相关宏定义

预编译

原文地址:http://docs.unity3d.com/Documentation/Manual/PlatformDependentCompilation.html

平台定义

   
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_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。

运行平台

 
//获得当前运行平台
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,
}



没有更多推荐了,返回首页