加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 如何统一显示网格线?

发布时间:2020-12-15 22:23:48 所属栏目:百科 来源:网络整理
导读:我正在进行我的第一场2D比赛. 我在生成游戏领域时遇到问题. 我需要做3件事. 用方形瓷砖填充场地(完成) 在每个瓷砖上显示随机数(完成) 显示网格线(问题在这里) 要创建我的主板,我使用附加到主相机的脚本. 这是我使用的功能: void BoardSetup(){ board = new
我正在进行我的第一场2D比赛.
我在生成游戏领域时遇到问题.
我需要做3件事.

>用方形瓷砖填充场地(完成)
>在每个瓷砖上显示随机数(完成)
>显示网格线(问题在这里)

要创建我的主板,我使用附加到主相机的脚本.
这是我使用的功能:

void BoardSetup()
{
    board = new GameObject("Board");
    boardHolder = board.transform;

    for (int x = 0; x < columns; x++)
    {
        for (int y = 0; y < rows; y++)
        {
            GameObject toInstantiateBackground = snowTile;
            GameObject backgroundInstance = Instantiate(toInstantiateBackground,new Vector3(x,y,0f),Quaternion.identity) as GameObject;
            backgroundInstance.transform.SetParent(boardHolder);

            AddRandomNumber(backgroundInstance,x,y);
        }
    }

    float step = snowTile.GetComponent<SpriteRenderer>().bounds.max[0] - snowTile.GetComponent<SpriteRenderer>().bounds.min[0];
    CreateGrid(new Vector3(0,0),new Vector3(rows-1,columns-1,step);
}

这是我的CreateGrid块:

void CreateLineMaterial()
{

    if (!lineMaterial)
    {
        lineMaterial = new Material("Shader "Lines/Colored Blended" {" +
            "SubShader { Pass { " +
            "    Blend SrcAlpha OneMinusSrcAlpha " +
            "    ZWrite Off Cull Off Fog { Mode Off } " +
            "    BindChannels {" +
            "      Bind "vertex",vertex Bind "color",color }" +
            "} } }");
        lineMaterial.hideFlags = HideFlags.HideAndDontSave;
        lineMaterial.shader.hideFlags = HideFlags.HideAndDontSave;
    }
}

private void CreateGrid(Vector3 start,Vector3 stop,float step)
{
    CreateLineMaterial();
    // set the current material
    lineMaterial.SetPass(0);
    GL.Begin(GL.LINES);
    GL.Color(gridColor);
    // Vertical lines
    for (float x = start[0]; x <= stop[0]; x += step)
    {

        GL.Vertex3(x,0f,0f);
        GL.Vertex3(x,stop[1],0f);
    }
    // Horizontal lines
    for (float y = start[0]; y <= stop[1]; y += step)
    {

        GL.Vertex3(0f,0f);
        GL.Vertex3(stop[0],0f);
    }
    GL.End();
}

我使用了here的例子(并修改了它).

但是当我运行我的游戏时,我只看到了雪地联系和数字

Screenshot 1

我试图只运行CreateGrid()但在这种情况下我只看到黑屏.

解决方法

您引用的链接是在3D中绘制线条.您应该加载正交矩阵以在屏幕空间中绘制2d图元.首先推动一个新矩阵,加载正交矩阵,绘图,然后在完成后弹出矩阵:

GL.PushMatrix();
mat.SetPass(0);
GL.LoadOrtho();
GL.Begin(GL.LINES);

 // Set colors and draw verts

GL.End();
GL.PopMatrix();

不要忘记你的顶点坐标应该在0和1之间.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读