Skip to content

Instantly share code, notes, and snippets.

@davepape
Last active October 11, 2018 03:39
Show Gist options
  • Select an option

  • Save davepape/56f5968b919bb0252f197f6326593bc2 to your computer and use it in GitHub Desktop.

Select an option

Save davepape/56f5968b919bb0252f197f6326593bc2 to your computer and use it in GitHub Desktop.
Create a Unity mesh of 2 triangles, in C#
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class triangles : MonoBehaviour {
void Start ()
{
gameObject.AddComponent<MeshFilter>();
gameObject.AddComponent<MeshRenderer>();
Material mat = new Material(Shader.Find("Custom/Vertex Colored"));
GetComponent<Renderer>().material = mat;
Vector3 p0 = new Vector3(-2f,0f,-1f);
Vector3 p1 = new Vector3(0f,1f,-1f);
Vector3 p2 = new Vector3(1f,0f,-1f);
Vector3 p3 = new Vector3(0f,-1f,-1f);
Vector3[] newVertices = new Vector3[4];
newVertices[0] = p0;
newVertices[1] = p1;
newVertices[2] = p2;
newVertices[3] = p3;
Color[] newColors = new Color[4];
newColors[0] = new Color(1, 0, 0);
newColors[1] = new Color(0, 1, 0);
newColors[2] = new Color(0, 0, 1);
newColors[3] = new Color(1, 1, 0);
int[] newTriangles = new int[6];
newTriangles[0] = 0;
newTriangles[1] = 1;
newTriangles[2] = 2;
newTriangles[3] = 0;
newTriangles[4] = 2;
newTriangles[5] = 3;
Mesh m = GetComponent<MeshFilter>().mesh;
m.vertices = newVertices;
m.colors = newColors;
m.triangles = newTriangles;
}
void Update ()
{
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment