holopy3/Assets/Scripts/GridMesh.cs

40 lines
1.1 KiB
C#
Raw Permalink Normal View History

2020-12-10 14:25:12 +00:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
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;
}
}