Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Create a Unity Mesh with 2 triangles
// Creating Mesh data in Unity
// This script should be attached to an empty GameObject. It requires vertexColor.shader (https://gist.github.com/davepape/0dee3b1550a1f4159bc173e1b32dbe0c)
// On startup, it will create a MeshFilter containing 2 triangles, and apply a Material.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class triangles2 : MonoBehaviour {
void Start ()
{
gameObject.AddComponent<MeshFilter>();
gameObject.AddComponent<MeshRenderer>();
Material mat = new Material(Shader.Find("Custom/Vertex Colored"));
GetComponent<Renderer>().material = mat;
Vector3[] newVertices = { new Vector3(-2f,0f,-1f), new Vector3(0f,1f,-1f), new Vector3(1f,0f,-1f), new Vector3(0f,-1f,-1f) };
Color[] newColors = { Color.red, Color.green, Color.blue, Color.yellow };
int[] newTriangles = { 0,1,2, 0,2,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
You can’t perform that action at this time.