How to show grid lines in unity?

I am working on my first 2nd game. I have a problem in creating a playing field. I need to do 3 things.

  • Fill the box with square pegs (done)
  • Show an arbitrary number on each plate (done)
  • Show grid lines ( problem here )

To create my board, I use a script attached to the main camera. This is the function I use:

    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, 0), new Vector3(rows-1, columns-1, 0f), step);
}

This is my CreateGrid block:

    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, y, 0f);
        GL.Vertex3(stop[0], y, 0f);
    }
    GL.End();
}

I used the example from here (and modified it).

But when I start my game, I see only snowy links and numbers Screenshot 1

I tried to run only CreateGrid (), but in this case I only see a black screen.

+4
source share
1

, , - 3D. , 2d- . , , , , :

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

 // Set colors and draw verts

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

, 0 1.

+1

Source: https://habr.com/ru/post/1618932/


All Articles