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 天前
06-04-YooAsset源码-Unity加密解密服务
前端·unity·性能优化·c#·游戏引擎·yooasset
HH‘HH2 天前
Unity 项目创建标准指南:分辨率、尺寸、文件路径与命名规范
unity·游戏引擎
元气少女小圆丶2 天前
unity发布web嵌入到前端页面的接受参数
前端·unity·webgl
HH‘HH2 天前
Unity 打包程序设置及注意事项全攻略
unity·游戏引擎
fqbqrr2 天前
2607d,parin游戏引擎
游戏引擎·d
qq_170264752 天前
unity里 Burst的用法
unity·游戏引擎
HH‘HH2 天前
Unity 相机与光照调整:从基础到进阶的实战指南
unity
HH‘HH3 天前
Unity引擎界面各个功能面板详细介绍
unity·游戏引擎
℡枫叶℡3 天前
Unity 2D资产命名常用缩写
unity·资产命名缩写