From 5145dd09cab55a896d510482f2e22f74b4d4b76f Mon Sep 17 00:00:00 2001 From: chai Date: Sat, 5 Jun 2021 19:51:03 +0800 Subject: *misc --- .../EditorGUIHelper/Editor/EditorHandlesHelper.cs | 68 +++++++++++++++++++++- 1 file changed, 67 insertions(+), 1 deletion(-) (limited to 'UnityCollection/Assets/Tools/EditorGUIHelper/Editor/EditorHandlesHelper.cs') diff --git a/UnityCollection/Assets/Tools/EditorGUIHelper/Editor/EditorHandlesHelper.cs b/UnityCollection/Assets/Tools/EditorGUIHelper/Editor/EditorHandlesHelper.cs index 4bf4b8f..3b2d648 100644 --- a/UnityCollection/Assets/Tools/EditorGUIHelper/Editor/EditorHandlesHelper.cs +++ b/UnityCollection/Assets/Tools/EditorGUIHelper/Editor/EditorHandlesHelper.cs @@ -13,15 +13,50 @@ public static class EditorHandlesHelper static float s_ValueDrag; static float s_ScaleDrawLength; + // style + static GUIStyle s_StyleBlackLabel; + static GUIStyle s_StyleWhiteLabel; + static Dictionary s_StyleLabels; + static EditorHandlesHelper() { s_ValueScaleHandleHash = "ValueScaleHandleHash".GetHashCode(); + + SetupStyles(); + } + + static void SetupStyles() + { + s_StyleBlackLabel = new GUIStyle(); + s_StyleBlackLabel.normal.textColor = Color.black; + s_StyleWhiteLabel = new GUIStyle(); + s_StyleWhiteLabel.normal.textColor = Color.white; + s_StyleLabels = new Dictionary(); } #region 单一handle + // 箭头 + public static void Arrow(Vector3 position, Vector3 direction, float length = 1f, float arrowSize = 0.1f, bool dotted = false) + { + direction = direction.normalized; + if(!dotted) + Handles.DrawLine(position, position + direction * length); + else + Handles.DrawDottedLine(position, position + direction * length,1f); + Handles.ConeHandleCap(0, position + direction * length, Quaternion.LookRotation(direction, Vector3.up), arrowSize, EventType.Repaint); + } + + // 控制数值的箭头 + public static float ScaleValue(float value, Vector3 position, Vector3 direction, float length, Handles.CapFunction capFunc, Quaternion capRot, float size, float snap = 0) + { + Handles.DrawLine(position, position + direction * length); + value = ScaleValue(value, position + direction, capRot, size, capFunc, snap); + return value; + } + // 控制数值的handle - public static float ScaleValueHandle(float value, Vector3 position, Quaternion rotation, float size, Handles.CapFunction capFunc, float snap) + public static float ScaleValue(float value, Vector3 position, Quaternion rotation, float size, Handles.CapFunction capFunc, float snap) { int controlID = GUIUtility.GetControlID(s_ValueScaleHandleHash, FocusType.Keyboard); int id = controlID; @@ -90,10 +125,41 @@ public static class EditorHandlesHelper return value; } + // 带背景的label + public static void Label(Vector3 position, string label, Color textColor) + { + GUIStyle textStyle = TryGetLabelStyleOrCreate(textColor); + Handles.Label(position, label, textStyle); + } + + public static void WireCube(Vector3 position, Vector3 size, Color col) + { + Color c = Handles.color; + Handles.color = col; + Handles.DrawWireCube(position, size); + Handles.color = c; + } + #endregion #region 复合handle + #endregion + + #region 内部方法 + + static GUIStyle TryGetLabelStyleOrCreate(Color color) + { + GUIStyle style; + if (!s_StyleLabels.TryGetValue(color.GetHashCode(), out style)) + { + style = new GUIStyle(); + style.normal.textColor = color; + s_StyleLabels.Add(color.GetHashCode(), style); + } + return style; + } + #endregion } -- cgit v1.1-26-g67d0