Skip to content

Instantly share code, notes, and snippets.

@louis-e
Forked from mdomrach/GridMesh.cs
Last active June 22, 2020 21:42
Show Gist options
  • Save louis-e/e80befce57eff15c88a943453cdefbb8 to your computer and use it in GitHub Desktop.
Save louis-e/e80befce57eff15c88a943453cdefbb8 to your computer and use it in GitHub Desktop.
Fixed line 17; otherwise the top and the right side of the grid won't be closed
using UnityEngine;
using System.Collections.Generic;
[RequireComponent(typeof(MeshRenderer))]
[RequireComponent(typeof(MeshFilter))]
public class GridMesh : MonoBehaviour
{
public int GridSize;
void Awake()
{
MeshFilter filter = gameObject.GetComponent<MeshFilter>();
var mesh = new Mesh();
var verticies = new List<Vector3>();
var indicies = new List<int>();
for (int i = 0; i <= GridSize; i++)
{
verticies.Add(new Vector3(i, 0, 0));
verticies.Add(new Vector3(i, 0, GridSize));
indicies.Add(4 * i + 0);
indicies.Add(4 * i + 1);
verticies.Add(new Vector3(0, 0, i));
verticies.Add(new Vector3(GridSize, 0, i));
indicies.Add(4 * i + 2);
indicies.Add(4 * i + 3);
}
mesh.vertices = verticies.ToArray();
mesh.SetIndices(indicies.ToArray(), MeshTopology.Lines, 0);
filter.mesh = mesh;
MeshRenderer meshRenderer = gameObject.GetComponent<MeshRenderer>();
meshRenderer.material = new Material(Shader.Find("Sprites/Default"));
meshRenderer.material.color = Color.white;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment