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

相关推荐
叶帆12 天前
【YFIOs】用C#开发硬件之设备上云
开发语言·unity·c#
久数君12 天前
AI三维建模工具“造形家”:地理场景三维化的高效解决方案
unity·glb·ai算法·ai三维建模工具·地图框选·造形家·城市建筑模型
会思考的猴子13 天前
Unity VFX 属性 Postion 和 TargetPostion
unity
hai31524754313 天前
九章编程法 · 猜数字游戏 (GW-BASIC 重构版) *
人工智能·microsoft·游戏引擎·游戏程序
心前阳光13 天前
Unity资源导入之自动化资源导入
unity·自动化·游戏引擎
心前阳光13 天前
Unity之2021.3.45f2c1发布安卓程序遇到的问题
android·unity·游戏引擎
纪纯13 天前
PicoVR Unity Integration SDK 3.4 常用交互API
unity·游戏引擎·vr·pico
龙智DevSecOps解决方案13 天前
3A 游戏优化技术栈:如何打通引擎级分析工具与 DevOps 持续集成管线?
unity·性能优化·游戏开发·技术美术·perforce·unrealengine
葛兰岱尔13 天前
从 SolidWorks 到 Three.js,从 Inventor 到 Unity——制造业CAD模型“几何-语义一体化“转换,不再是天方夜谭!
开发语言·javascript·unity
鼎艺创新科技13 天前
三维电子沙盘中OSGB倾斜摄影数据的加载与渲染
游戏引擎·cocos2d