我正在进行我的第一场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, 0), new Vector3(rows-1, columns-1, 0f), 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, y, 0f);
GL.Vertex3(stop[0], y, 0f);
}
GL.End();
}
我使用了here的例子(并修改了它).
但是当我运行我的游戏时,我只看到了雪地联系和数字
我试图只运行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之间.