Skip to content

Instantly share code, notes, and snippets.

@mdomrach
Created June 25, 2016 06:33
Show Gist options
  • Star 18 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save mdomrach/a66602ee85ce45f8860c36b2ad31ea14 to your computer and use it in GitHub Desktop.
Save mdomrach/a66602ee85ce45f8860c36b2ad31ea14 to your computer and use it in GitHub Desktop.
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;
}
}
@DarkenSoda
Copy link

Thanks for sharing.
I tweaked the code a bit to fit my needs.

  1. Removed the bending.
  2. Made the grid on the XZ Plane.
  3. Added a Vector2Int for the gridSize in case the grid doesn't have the same length and width.
  4. Added float for the gridSpacing, simply the size of each cell in the grid.
public class GridMesh : MonoBehaviour {
        public MeshFilter filter;
        public Vector2Int gridSize;
        public float gridSpacing;

        Mesh mesh;
        List<Vector3> verticies;
        List<int> indices;

        private void Start() {
            mesh = new Mesh();
            MeshRenderer meshRenderer = filter.GetComponent<MeshRenderer>();
            meshRenderer.material = new Material(Shader.Find("Sprites/Default"));
            meshRenderer.material.color = Color.black;

            Rebuild();
        }

        private void Update() {
            Rebuild();
        }

        private void Rebuild() {
            verticies = new List<Vector3>();
            indices = new List<int>();

            float xMin = gridSpacing * gridSize.x / 2f;
            float zMin = gridSpacing * gridSize.y / 2f;

            for (int i = 0; i <= gridSize.x; i++) {
                for (int j = 0; j <= gridSize.y; j++) {
                    float x1 = i * gridSpacing - xMin;
                    float x2 = (i + 1) * gridSpacing - xMin;
                    float z1 = j * gridSpacing - zMin;
                    float z2 = (j + 1) * gridSpacing - zMin;

                    if (i != gridSize.x) {
                        verticies.Add(new Vector3(x1, 0, z1));
                        verticies.Add(new Vector3(x2, 0, z1));
                    }

                    if (j != gridSize.y) {
                        verticies.Add(new Vector3(x1, 0, z1));
                        verticies.Add(new Vector3(x1, 0, z2));
                    }
                }
            }

            int indicesCount = verticies.Count;
            for (int i = 0; i < indicesCount; i++) {
                indices.Add(i);
            }

            mesh.vertices = verticies.ToArray();
            mesh.SetIndices(indices.ToArray(), MeshTopology.Lines, 0);
            filter.mesh = mesh;
        }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment