前言:自定义宏的功能开发是必要的。例如,当我们在编辑器模式下,进行调试的时候会打应很多日志,而当发布了客户端,发布模式的时候,就不能打印这些日志。
1.创建"Menu.cs"脚本,脚本存储在Editor文件夹
using UnityEditor;
using UnityEngine;
public class Menu
{
[MenuItem("Dean27Tools/Settings")]
private static void Settings()
{
//打开SettingsWindow
SettingsWindow win = (SettingsWindow)EditorWindow.GetWindow(typeof(SettingsWindow));
win.titleContent = new GUIContent("全局设置");
win.Show();
}
[MenuItem("Dean27Tools/AssetBundleCreate")]
private static void AssetBundleCreate()
{
AssetBundleWindow win = EditorWindow.GetWindow<AssetBundleWindow>();
win.titleContent = new GUIContent("资源打包");
win.Show();
}
}
运行后的截图为:
2.自定义宏地址:
1.打开File-Build Settings-Player Settings-Other Settings-Scripting Define Symbols。
2.填写“DEBUG_MODEL” 和 “DEBUG_LOG”;
3.图示:
3.创建Test_Tools脚本,并挂载。
1.脚本内容:
using UnityEngine;
public class Test_Tools : MonoBehaviour
{
// Use this for initialization
void Start ()
{
#if DEBUG_LOG
Debug.Log("DEBUG_LOG");
#endif
}
// Update is called once per frame
void Update ()
{
}
}
2.挂载脚本:
3.运行
4.当自定义了宏"DEBUG_LOG"时,则会打印“ Debug.Log(“DEBUG_LOG”);”脚本内容,若是没有定义,则不会打印。
5.图示
4.完成自定义工具
1.创建脚本SettingWindow,脚本存储在Editor文件夹,脚本如下图:
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
public class SettingsWindow : EditorWindow
{
private List<MacorItem> m_Lst = new List<MacorItem>();
private Dictionary<string, bool> m_Dic = new Dictionary<string, bool>();
private string m_Macor = null;
private void OnEnable()
{
m_Macor = PlayerSettings.GetScriptingDefineSymbolsForGroup(BuildTargetGroup.Standalone);
m_Lst.Clear();
m_Lst.Add(new MacorItem() { Name = "DEBUG_MODEL", DisplayName = "调试模式", IsDebug = true, IsRelease = false});
m_Lst.Add(new MacorItem() { Name = "DEBUG_LOG", DisplayName = "打印日志", IsDebug = true, IsRelease = false });
m_Lst.Add(new MacorItem() { Name = "STAT_TD", DisplayName = "开启统计", IsDebug = false, IsRelease = true });
m_Dic.Clear();
for (int i = 0; i < m_Lst.Count; ++i)
{
if (!string.IsNullOrEmpty(m_Macor) && m_Macor.IndexOf(m_Lst[i].Name) != -1)
{
m_Dic[m_Lst[i].Name] = true;
}
else
{
m_Dic[m_Lst[i].Name] = false;
}
}
}
/// <summary>
/// 绘制界面
/// </summary>
private void OnGUI()
{
for (int i = 0; i < m_Lst.Count; ++i)
{
EditorGUILayout.BeginHorizontal("Box");
m_Dic[m_Lst[i].Name] = GUILayout.Toggle(m_Dic[m_Lst[i].Name], m_Lst[i].DisplayName);
EditorGUILayout.EndHorizontal();
}
EditorGUILayout.BeginHorizontal();
if (GUILayout.Button("保存", GUILayout.Width(100)))
{
SaveMacor();
}
if (GUILayout.Button("调试模式", GUILayout.Width(100)))
{
for (int i = 0; i < m_Lst.Count; ++i)
{
m_Dic[m_Lst[i].Name] = m_Lst[i].IsDebug;
}
SaveMacor();
}
if (GUILayout.Button("发布模式", GUILayout.Width(100)))
{
for (int i = 0; i < m_Lst.Count; ++i)
{
m_Dic[m_Lst[i].Name] = m_Lst[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.Standalone, m_Macor);
PlayerSettings.SetScriptingDefineSymbolsForGroup(BuildTargetGroup.iOS, m_Macor);
PlayerSettings.SetScriptingDefineSymbolsForGroup(BuildTargetGroup.Android, m_Macor);
}
public class MacorItem
{
/// <summary>
/// 名称(英文)
/// </summary>
public string Name;
/// <summary>
/// 显示的名称(中文)
/// </summary>
public string DisplayName;
/// <summary>
/// 是否调试项
/// </summary>
public bool IsDebug;
/// <summary>
/// 是否发布项
/// </summary>
public bool IsRelease;
}
}
2.运行截图
当点击了调试模式时,则会在Scripting Define Symbols处添加自定义宏“DEBUG_MODEL”。
当点击了打印模式时,则会在Scripting Define Symbols处添加自定义宏“DEBUG_LOG”。
而按钮调试模式会快速选择调试模式和打印模式,发布按钮则快速选择开启统计模式。