Unity3D 宏定义

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,打开窗口就可以动态设置自定义的宏了

猜你喜欢

转载自blog.csdn.net/Aa85641826/article/details/81661096
今日推荐