cs
using UnityEditor;
using static UnityEngine.Rendering.DebugUI.Table;
using static UnityEngine.Rendering.DebugUI.MessageBox;
void OnDrawGizmos()
{
// 编辑模式下的参数计算
if (!Application.isPlaying)
{
Camera currentCam = null;
if (SceneView.lastActiveSceneView != null)
currentCam = SceneView.lastActiveSceneView.camera;
if (currentCam == null) currentCam = Camera.main;
if (currentCam == null) return;
cam = currentCam;
CalculateGridParams();
}
if (cellSize == Vector2.zero) return;
// 用新的 origin 计算边界
float rightX = origin.x + cols * cellSize.x;
float bottomY = origin.y - rows * cellSize.y;
// 画网格线
Gizmos.color = Color.yellow;
for (int i = 0; i <= cols; i++)
{
float x = origin.x + i * cellSize.x;
Gizmos.DrawLine(new Vector3(x, origin.y, 0), new Vector3(x, bottomY, 0));
}
for (int j = 0; j <= rows; j++)
{
float y = origin.y - j * cellSize.y;
Gizmos.DrawLine(new Vector3(origin.x, y, 0), new Vector3(rightX, y, 0));
}
// 标注坐标 (c, r)
GUIStyle style = new GUIStyle();
style.normal.textColor = Color.white;
style.fontSize = 12;
style.alignment = TextAnchor.MiddleCenter;
for (int c = 0; c < cols; c++)
{
for (int r = 0; r < rows; r++)
{
// 计算格子中心
Vector3 center = origin + new Vector3(c * cellSize.x + cellSize.x / 2f,
-r * cellSize.y - cellSize.y / 2f, 0);
Handles.Label(center, $"({c},{r})", style);
}
}
}