D
D
Dmitry Demidov2015-12-07 22:34:58
C++ / C#
Dmitry Demidov, 2015-12-07 22:34:58

How to display grid lines on the playing field?

Hello.
I keep trying to overlay the grid on the playing field :)
So, I generate the field:

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);
    }

I impose a grid (I took the code from here and modified it)
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)
    {
        GL.PushMatrix();
        
        CreateLineMaterial();
        // set the current material
        lineMaterial.SetPass(0);
        GL.LoadOrtho();

        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();
        GL.PopMatrix();
    }

I'm launching. I see a field without a grid. If you comment out the generation of tiles and numbers (the very first cycle), then just a black screen.
The original question on SOF

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Daniil Basmanov, 2015-12-08
@ptitca_zu

Since version 5.1 compilation of shaders at runtime is not supported. I'm assuming you're on the latest version, so your material constructor doesn't do anything. You can manually create a material with a shader, and then put a link to it in the inspector.

Shader "Lines/Colored Blended"
{
    SubShader
    {
        Pass
        {
            Blend SrcAlpha OneMinusSrcAlpha
            ZWrite Off Cull Off Fog { Mode Off }
            BindChannels { Bind "vertex", vertex Bind "color", color }
        }
    }
}

Or you can try other ways to draw lines.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question