/* * This file is part of the CatLib package. * * (c) CatLib * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. * * Document: http://catlib.io/ */ using System; using UnityEditor; using UnityEngine; namespace CatLib.Editor { /// /// 编辑栏工具 /// public static class InspectorTool { /// /// GUI样式 /// private static GUIStyle guiStyle; /// /// GUI样式 /// public static GUIStyle GUIStyle { get { return guiStyle = guiStyle ?? GUI.skin.FindStyle("box"); } set { guiStyle = value; } } /// /// 绘制水平内容 /// /// public static void Horizontal(Action closure) { EditorGUILayout.BeginHorizontal(GUIStyle); try { closure(); } finally { EditorGUILayout.EndHorizontal(); } } /// /// 绘制水平内容 /// /// public static void Vertical(Action closure) { EditorGUILayout.BeginVertical(GUIStyle); try { closure(); } finally { EditorGUILayout.EndVertical(); } } /// /// 绘制标签盒子 /// /// 标题 /// 代码块 public static void LabelBox(string title, Action codeBlock) { Vertical(() => { GUILayout.Label(title); Vertical(codeBlock); }); } /// /// 绘制折叠盒子 /// /// 是否是可见的 /// 标题 /// 代码块 /// public static bool ToggleBox(bool visiable, string title, Action codeBlock) { Vertical(() => { visiable = GUILayout.Toggle(visiable, title, EditorStyles.foldout); if (!visiable) { EditorGUILayout.EndVertical(); } Vertical(codeBlock); }); return visiable; } /// /// 绘制按钮 /// /// 按钮标题 /// 按钮提示 /// 按钮是否是可用的 /// 按钮宽度 /// 按钮高度 /// 样式信息 /// public static bool Button(string title, string tooltip, bool enabled, float width = -1, float height = -1, GUIStyle style = null) { var widthOptions = (width <= 0) ? GUILayout.ExpandWidth(true) : GUILayout.Width(width); var heightOptions = (height <= 0) ? GUILayout.ExpandHeight(true) : GUILayout.Height(height); style = style ?? EditorStyles.miniButton; if (enabled) { return GUILayout.Button(new GUIContent(title, tooltip), style, widthOptions, heightOptions); } return (bool) ApplyColor( () => GUILayout.Button(new GUIContent(title, tooltip), style, widthOptions, heightOptions), new Color(1f, 1f, 1f, 0.25f)); } /// /// 应用颜色 /// /// 闭包 /// 指定颜色 public static object ApplyColor(Func action, Color color) { return ApplyColor(action, color, GUI.contentColor); } /// /// 应用颜色 /// /// 闭包 /// 指定颜色 /// 文字颜色 public static object ApplyColor(Func action, Color color, Color contentColor) { var backup = GUI.color; var backupContent = GUI.contentColor; try { GUI.color = color; GUI.contentColor = contentColor; return action(); } finally { GUI.color = backup; GUI.contentColor = backupContent; } } } }