【Unity】2D场景绘制瓦片地图的网格线

本解决方案的代码基于此文章所提供的代码所进行的修改调整:http://t.csdnimg.cn/zoKa2

绘制网格线是通过UnityEngine.GL库来实现

cs 复制代码
using UnityEngine;
using UnityEngine.Tilemaps;

/// <summary>
/// 挂在主摄像机上
/// </summary>
public class MapGridDraw : MonoBehaviour
{
    /// <summary>
    /// 线的材质
    /// 创建一个Unlit-Color材质就可以拿来用
    /// </summary>
    public Material lineMat;
 
    /// <summary>
    /// 线的颜色
    /// </summary>
    public Color lineColor = new Color(1, 1, 1, 0.5f);
 
 
    public Camera _camera; //相机
 
    public Tilemap tilemap; //瓦片地图


    /// <summary>
    ///  OnPostRender函数是Unity自带函数
    ///  与摄像机有关,因此该脚本必须挂在
    ///  主摄像机上
    /// </summary>
    void OnPostRender()
    { 
        DrawGrid();
    }


 
    /// <summary>
    /// 绘制网格
    /// </summary>
    void DrawGrid()
    {
        if (lineMat == null)
        {
            return;
        }
        Vector2 cameraPos = _camera.transform.position;
 
        //相机的宽高
        int width = _camera.pixelWidth;
        int height = _camera.pixelHeight;

        //视口在世界空间的四个点
        Vector2 leftUp = _camera.ScreenToWorldPoint(new Vector2(0, height));
        Vector2 rightUp = _camera.ScreenToWorldPoint(new Vector2(width, height));
        Vector2 leftDown = _camera.ScreenToWorldPoint(new Vector2(0, 0));
        Vector2 rightDown = _camera.ScreenToWorldPoint(new Vector2(width, 0));

        Vector3Int leftUp_Tile = tilemap.WorldToCell(leftUp);
        Vector3Int rightUp_Tile = tilemap.WorldToCell(rightUp);
        Vector3Int leftDown_Tile = tilemap.WorldToCell(leftDown);
        Vector3Int rightDown_Tile = tilemap.WorldToCell(rightDown);

        //转成基于瓦片地图坐标的世界坐标
        leftUp = tilemap.CellToWorld(leftUp_Tile);
        rightUp = tilemap.CellToWorld(rightUp_Tile);
        leftDown = tilemap.CellToWorld(leftDown_Tile);
        rightDown = tilemap.CellToWorld(rightDown_Tile);

        //相机视口在世界坐标中的宽高
        float viewWidth = rightUp.x - leftDown.x;
        float viewHeight = rightUp.y - leftDown.y;

        //cell在横轴和纵轴上的个数
        int x = (int) viewWidth + 1;
        int y = (int) viewHeight + 1;
 
        //x y变为奇数
        x = x / 2 * 2 + 1;
        y = y / 2 * 2 + 1;
 
        //中间的方块位置
        float centerX = x / 2+0.5f;
        float centerY = y / 2+0.5f;
        // float centerX = x / 2 + 1;
        // float centerY = y / 2 + 1;
 
        // //偏移
        float offsetX = cameraPos.x % 1;
        float offsetY = cameraPos.y % 1;
 
        //竖线
        for (int i = 0; i <= x; i++)
        {
            //初始位置
            float posX = i - centerX + 0.5f;
            //线跟随摄像机移动
            posX += cameraPos.x;
            //产生偏移
            posX -= offsetX;
 
            DrawLine(new Vector2(posX, leftUp.y), new Vector2(posX, leftDown.y), lineColor);
        }
        //横线
        for (int j = 1; j <= y; j++)
        {
            //线跟随摄像机移动
            float posY = j - centerY + 0.5f;
            //线跟随摄像机移动
            posY += cameraPos.y;
            //产生偏移
            posY -= offsetY;
 
            DrawLine(new Vector2(leftUp.x, posY), new Vector2(rightUp.x, posY), lineColor);
        }
    }
 
 
    /// <summary>
    /// 画一条线 世界坐标
    /// </summary>
    /// <param name="posA"></param>
    /// <param name="posB"></param>
    /// /// <param name="color"></param>
    void DrawLine(Vector2 posA, Vector2 posB,Color color)
    {
        GL.Begin(GL.LINES);
        lineMat.SetPass(0);
        GL.Color(color);
        GL.Vertex3(posA.x, posA.y, 0);
        GL.Vertex3(posB.x, posB.y, 0);
        GL.End();
    }
 
  

    
}

但是经过我在实际项目中测试发现,该方案存在一个待解决的问题,那就是如果场景中存在UI对象,那么这些网格线的部分线条会遮挡UI,因此请慎用。(如果有朋友解决了这个问题,还请在评论中提供一下解决办法,谢谢!)

相关推荐
用户7884773163421 小时前
WinForms 迁移 WPF:Upgrade Assistant 覆盖 30%,剩下 70% 我手写了六周
c#
寂寞的船1 天前
【Unity知识速递】第13期 Unity 内存泄漏排查实战:从 Memory Profiler 到 Lua 引用泄漏的生产级全链路诊断
unity·junit·c#·游戏引擎·lua
用户3721574261352 天前
C# 提取 PDF 附件:包括文档附件和页面注释附件
c#
寂寞的船2 天前
【Unity Bug挑战】第12期 Awaitable 二次 await 死锁之谜 —— 为什么同一个异步操作等两次就卡死?
unity·c#·游戏引擎·bug
探求虚无3 天前
用 AI 在 Unity 里做一款 RTS:最难的不是生成代码,而是证明它真的能玩
人工智能·unity·游戏引擎
huaqianzkh3 天前
WinForm C#:循环修改控件UI闪烁卡顿?SuspendLayout / ResumeLayout 与 BeginUpdate 优化
开发语言·ui·c#
用户3721574261354 天前
C# 实现 Excel 单元格保护:锁定公式并允许编辑指定区域
c#·ai编程·编程语言
17岁的勇气4 天前
Unity Shader unity文档学习笔记(二十二):shader变体
笔记·学习·unity
公子小六4 天前
基于.NET的Windows窗体编程之WinForms数据持久化(1)
windows·microsoft·c#·.net·winforms
三雷科技4 天前
第6章第4节:自定义分词器
开发语言·c#