微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

正在进行我的第一场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之间.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐