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中看到

相关推荐
游乐码1 小时前
Unity基础(十三)资源卸载
unity·游戏引擎
winlife_2 小时前
全程用 AI 做一款商业级手游 · EP7 表现层与手感:从“能跑“到“摸起来爽“
java·开发语言·人工智能·unity·ai编程·游戏开发·mcp
冰糖橘子ABC2 小时前
Unity 动作重定向
unity·游戏引擎
程序员正茂2 小时前
Unity3d程序发布后自动显示发布日期
unity
晓131312 小时前
【Cocos Creator 2.x】篇——第二章 入门
javascript·游戏引擎
nnsix14 小时前
Unity 贴图压缩格式 笔记
笔记·unity·贴图
ysn111111 天前
搭建状态同步框架的实践心得
unity·架构
一线灵1 天前
Axmol:小众引擎的硬核逆袭
游戏引擎
weixin_441940011 天前
【Unity教程】使用vuforia创建简单的AR实例
unity·游戏引擎·ar
郝学胜-神的一滴1 天前
[简化版 GAMES 101] 计算机图形学 12:可见性与 Z‑Buffer 深度缓存
unity·godot·图形渲染·three.js·opengl·unreal