Answer the question
In order to leave comments, you need to log in
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);
}
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();
}
Answer the question
In order to leave comments, you need to log in
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 }
}
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question