Dmitriy Demidov
Dmitriy Demidov

Reputation: 533

How to show grid lines in unity?

I'm working on my first 2d game. I have problem in generating play field. I need to do 3 things.

  1. Fill the field with square png tiles (done)
  2. Show random number over every tile (done)
  3. Show grid lines (problem is here)

To create my board I use script attached to 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 have used example from here (and modified it).

But when I run my game I see only snow ties and numbersScreenshot 1

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

Upvotes: 4

Views: 6018

Answers (1)

Foggzie
Foggzie

Reputation: 9821

That link you referenced is drawing lines in 3D. You should be loading an orthographic matrix to draw 2d primitives in screen space. Start by pushing a new matrix, loading the orthographic matrix, drawing, and then popping the matrix when you're done:

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

 // Set colors and draw verts

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

Don't forget your vertex coordinates should be between 0 and 1.

Upvotes: 1

Related Questions