/* * @Author: 白哉 * @Description: 一些通用的 Inspector 工具函数 * @Date: 2025年04月22日 星期二 15:04:37 * @Modify: */ using UnityEngine; using UnityEditor; namespace Logic.Editor { public class InspectorUtils { public static GUIStyle GetHelpBoxStyle() { var boxStyle = new GUIStyle(EditorStyles.helpBox); boxStyle.normal.background = MakeTexture(2, 2, new Color(0.3f, 0.3f, 0.3f, 0.8f)); boxStyle.border = new RectOffset(1, 1, 1, 1); boxStyle.padding = new RectOffset(6, 6, 6, 6); boxStyle.normal.textColor = Color.white; return boxStyle; } public static void AddBorder(GUIStyle boxStyle, Color borderColor) { // 边框宽度 2像素 var borderWidth = 2; var background = boxStyle.normal.background; // 拓展纹理 int width = background.width + 2 * borderWidth; int height = background.height + 2 * borderWidth; // 新纹理的像素存储 Texture2D newBackground = new Texture2D(width, height); Color[] newBackgroundColors = new Color[width * height]; // 旧纹理的像素 Color[] backgroundColors = background.GetPixels(); // 增加边框,保留原像素 for (int i = 0; i < height; ++i) { for (int j = 0; j < width; ++j) { if (i < borderWidth || i >= height - borderWidth || j < borderWidth || j >= width - borderWidth) { newBackgroundColors[i * width + j] = borderColor; } else { int yOffset = (i - borderWidth) * background.width; int originalIndex = yOffset + j - borderWidth; newBackgroundColors[i * width + j] = backgroundColors[originalIndex]; } } } newBackground.SetPixels(newBackgroundColors); newBackground.Apply(); newBackground.wrapMode = TextureWrapMode.Clamp; boxStyle.normal.background = newBackground; boxStyle.border = new RectOffset(boxStyle.border.left + borderWidth, boxStyle.border.right + borderWidth, boxStyle.border.top + borderWidth, boxStyle.border.bottom + borderWidth); } public static Texture2D MakeTexture(int width, int height, Color color) { Color[] pix = new Color[width * height]; for (int i = 0; i < pix.Length; ++i) pix[i] = color; Texture2D result = new Texture2D(width, height); result.SetPixels(pix); result.Apply(); return result; } // 固化 Text 宽度 public static void InspectorTextWidth(string text) { var textWidth = GUI.skin.toggle.CalcSize(new GUIContent(text)).x; EditorGUILayout.LabelField(text, GUILayout.Width(textWidth)); } // 固化 Text 宽度并框选背景 public static void InspectorTextWidth(string text, float width) { EditorGUILayout.LabelField(text, GUILayout.Width(width)); } // 固化 Text 宽度并框选背景 public static void InspectorTextWidth(string text, Color color) { var recordColor = GUI.contentColor; GUI.contentColor = color; var size = GUI.skin.toggle.CalcSize(new GUIContent(text)); EditorGUILayout.LabelField(text, GUILayout.Width(size.x), GUILayout.Height(size.y)); GUI.contentColor = recordColor; } // 固化 Text 宽度并支持 rich text public static void InspectorTextWidthRich(string text) { var size = GUI.skin.toggle.CalcSize(new GUIContent(text)); GUIStyle richTextStyle = new GUIStyle(GUI.skin.label); richTextStyle.richText = true; EditorGUILayout.LabelField(text, richTextStyle, GUILayout.Width(size.x), GUILayout.Height(size.y)); } // 固化 Text 宽度并支持 rich text public static void InspectorTextWidthRich(string text, float width) { GUIStyle richTextStyle = new GUIStyle(GUI.skin.label); richTextStyle.richText = true; EditorGUILayout.LabelField(text, richTextStyle, GUILayout.Width(width)); } // 固化 Text 宽度并框选背景 public static void InspectorTextWidth(GUIContent label, GUIStyle style) { var textWidth = GUI.skin.toggle.CalcSize(label).x; if (style == null) EditorGUILayout.BeginVertical(GUILayout.Width(textWidth)); else EditorGUILayout.BeginVertical(style, GUILayout.Width(textWidth)); EditorGUILayout.LabelField(label, GUILayout.Width(textWidth)); EditorGUILayout.EndVertical(); } // 固化 toggle 宽度 public static bool InspectorToggleVertical(bool toggle) { toggle = EditorGUILayout.Toggle(toggle, GUILayout.Width(40)); return toggle; } // 固化 toggle 宽度 public static bool InspectorToggleVertical(string text, bool toggle) { var toggleWidth = GUI.skin.toggle.CalcSize(new GUIContent(text)).x; toggle = EditorGUILayout.ToggleLeft(text, toggle, GUILayout.Width(toggleWidth + 40)); return toggle; } // 固化 toggle 宽度并框选背景 public static bool InspectorToggleVertical(string text, bool toggle, GUIStyle style) { var toggleWidth = GUI.skin.toggle.CalcSize(new GUIContent(text)).x; if (style == null) EditorGUILayout.BeginVertical(GUILayout.Width(toggleWidth)); else EditorGUILayout.BeginVertical(style, GUILayout.Width(toggleWidth)); toggle = EditorGUILayout.ToggleLeft(text, toggle, GUILayout.Width(toggleWidth + 40)); EditorGUILayout.EndVertical(); return toggle; } // 固化 button 的宽度并框选背景 public static bool InspectorButtonWithTextWidth(string text, GUIStyle style) { float buttonWidth = GUI.skin.button.CalcSize(new GUIContent(text)).x; if (style == null) EditorGUILayout.BeginVertical(GUILayout.Width(buttonWidth)); else EditorGUILayout.BeginVertical(style, GUILayout.Width(buttonWidth)); var trigger = GUILayout.Button(text, GUILayout.Width(buttonWidth)); EditorGUILayout.EndVertical(); return trigger; } // 固化 button 的宽度 public static bool InspectorButtonWithTextWidth(string text) { float buttonWidth = GUI.skin.button.CalcSize(new GUIContent(text)).x; if (GUILayout.Button(text, GUILayout.Width(buttonWidth))) return true; return false; } // 固化 button 的宽度 public static bool InspectorRepeatButtonWithTextWidth(string text) { float buttonWidth = GUI.skin.button.CalcSize(new GUIContent(text)).x; if (GUILayout.RepeatButton(text, GUILayout.Width(buttonWidth))) return true; return false; } public static bool InspectorButtonWithTextWidth(GUIContent content) { float buttonWidth = GUI.skin.button.CalcSize(content).x; if (GUILayout.Button(content, GUILayout.Width(buttonWidth))) return true; return false; } public static void DrawDivider(Color color, float thickness) { Rect rect = EditorGUILayout.GetControlRect(false, thickness); rect.height = thickness; EditorGUI.DrawRect(rect, color); } } }