Unity的可视化网格和文字标签

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);
			}
		}
	}

注意:只能在scene中看到

相关推荐
小白学鸿蒙19 小时前
Funplay Unity MCP 接入 trae 实战
unity·游戏引擎·mcp
相信神话202121 小时前
3.5《酒魂》体验与失败设计
游戏引擎·godot·godot4
游乐码1 天前
Unity基础(一)游戏中的数学Mathf函数
游戏·unity·游戏引擎
地狱为王1 天前
Unity实现猫脸关键点检测
unity·游戏引擎·猫脸关键点检测
598866753@qq.com2 天前
Unity Job System笔记
unity
winlife_2 天前
Funplay Unity MCP 与 Unity AI Assistant 详细对比:开源 MCP 工具集 vs 官方全栈 AI 产品
人工智能·unity·开源·ai编程·claude·mcp
御水流红叶2 天前
Android-Unity游戏逆向思路
android·游戏·unity
ellis19702 天前
Unity图集Atlas
unity
想不明白的过度思考者2 天前
Unity全局事件中心与新版输入架构实现练习——上帝模式与英雄模式的输入系统映射切换
java·unity·架构
GLDbalala3 天前
Unity基于自定义管线实现风格化水
unity·游戏引擎