/// <summary> /// 绘制Border的Gizmos /// </summary> protected void EditorDrawBorderGizmos() { Color old = Gizmos.color; GUIStyle textStyle = new GUIStyle(); textStyle.normal.textColor = m_EditorBorderColor; // 获取边框左下角与右上角的世界坐标 Vector3 leftDown = grid.GetCellCenterWorld(leftDownPosition) - halfCellSize; Vector3 rightUp = grid.GetCellCenterWorld(rightUpPosition) + halfCellSize; // 绘制左下角Cell与右上角Cell的Position Handles.Label(leftDown, (new Vector2Int(leftDownPosition.x, leftDownPosition.y)).ToString(), textStyle); Handles.Label(rightUp, (new Vector2Int(rightUpPosition.x, rightUpPosition.y)).ToString(), textStyle); if (mapRect.width > 0 && mapRect.height > 0) { Gizmos.color = m_EditorBorderColor; // 边框的长与宽 Vector3 size = Vector3.Scale(new Vector3(width, height), grid.cellSize); // 边框的中心坐标 Vector3 center = leftDown + size / 2f; // 绘制边框 Gizmos.DrawWireCube(center, size); } Gizmos.color = old; }
public static void DrawLabel(Vector3 position, string text, GUIStyle style = default(GUIStyle), Color color = default(Color), float offsetX = 0f, float offsetY = 0f) { #if UNITY_EDITOR if (IsHandleHackAvailable) { Transform cam = UnityEditor.SceneView.currentDrawingSceneView != null ? UnityEditor.SceneView.currentDrawingSceneView.camera.transform : // Scene View Camera.main.transform; // Only Game View if (Vector3.Dot(cam.forward, position - cam.position) > 0) { Vector3 pos = position; if (offsetX != 0f || offsetY != 0f) { Vector3 camRightVector = cam.right * offsetX; // base on view pos += camRightVector + new Vector3(0f, offsetY, 0f); // base on target } if (style == default(GUIStyle)) { if (color == default(Color)) { Handles.Label(pos, text, GUI.skin.textArea); } else { style = new GUIStyle(GUI.skin.textArea); Color old = style.normal.textColor; style.normal.textColor = color; Handles.Label(pos, text, style); style.normal.textColor = old; } } else { if (color == default(Color)) { Handles.Label(pos, text, style); } else { Color old = style.normal.textColor; style.normal.textColor = color; Handles.Label(pos, text, style); style.normal.textColor = old; } } } } #endif }
/// <summary> /// 绘制Cell的Gizmos /// </summary> protected void EditorDrawCellGizmos() { // 用于获取鼠标位置 Event e = Event.current; if (e.type != EventType.Repaint) { return; } // 获取当前操作Scene面板 SceneView sceneView = SceneView.currentDrawingSceneView; if (sceneView == null) { return; } Color old = Gizmos.color; /// 获取鼠标世界坐标: /// Event是从左上角(Left Up)开始, /// 而Camera是从左下角(Left Down), /// 需要转换才能使用Camera的ScreenToWorldPoint方法。 Vector2 screenPosition = new Vector2(e.mousePosition.x, sceneView.camera.pixelHeight - e.mousePosition.y); Vector2 worldPosition = sceneView.camera.ScreenToWorldPoint(screenPosition); // 当前鼠标所在Cell的Position Vector3Int cellPostion = grid.WorldToCell(worldPosition); // 当前鼠标所在Cell的Center坐标 Vector3 cellCenter = grid.GetCellCenterWorld(cellPostion); /// 绘制当前鼠标下的Cell边框与Position /// 如果包含Cell,正常绘制 /// 如果不包含Cell,改变颜色,并多绘制一个叉 if (Contains(cellPostion)) { GUIStyle textStyle = new GUIStyle(); textStyle.normal.textColor = m_EditorCellColor; Gizmos.color = m_EditorCellColor; Handles.Label(cellCenter - halfCellSize, (new Vector2Int(cellPostion.x, cellPostion.y)).ToString(), textStyle); Gizmos.DrawWireCube(cellCenter, grid.cellSize); } else { GUIStyle textStyle = new GUIStyle(); textStyle.normal.textColor = m_EditorErrorColor; Gizmos.color = m_EditorErrorColor; Handles.Label(cellCenter - halfCellSize, (new Vector2Int(cellPostion.x, cellPostion.y)).ToString(), textStyle); Gizmos.DrawWireCube(cellCenter, grid.cellSize); // 绘制Cell对角线 Vector3 from = cellCenter - halfCellSize; Vector3 to = cellCenter + halfCellSize; Gizmos.DrawLine(from, to); float tmpX = from.x; from.x = to.x; to.x = tmpX; Gizmos.DrawLine(from, to); } Gizmos.color = old; }