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

相关推荐
魔士于安2 小时前
Unity UI图片 复活节UI,卡通风格
游戏·ui·unity·游戏引擎·材质·贴图
weixin_423995002 小时前
unity 团结开发小游戏,加载AssetBundles(第二种方法)
unity·游戏引擎
魔士于安2 小时前
unity 卡通风整套资源 小鸟N套带动作+一套卡通风村落 和 相关道具+落叶粒子效果 buildin
游戏·unity·游戏引擎·贴图·模型
伽蓝_游戏4 小时前
第一章:解构游戏资源
游戏·unity·性能优化·c#·游戏引擎·游戏程序·assetbundle
星辰徐哥5 小时前
Unity C#入门:Visual Studio与Unity的关联配置
unity·c#·visual studio
Sparkle Star5 小时前
Unity VRTK4包导入和依赖关系
unity·游戏引擎
WiChP6 小时前
【V0.1B7】从零开始的2D游戏引擎开发之路
游戏引擎
Sparkle Star9 小时前
Unity VRTK4+SteamVR传送组件使用和层级关系
unity·游戏引擎
cheniie9 小时前
Windows下Unity开发VisionPro应用
windows·unity·vision pro