Skip to content

Instantly share code, notes, and snippets.

@davepape
Created October 11, 2018 03:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davepape/d76eed342b319f9b6b0be8d41cd9ec04 to your computer and use it in GitHub Desktop.
Save davepape/d76eed342b319f9b6b0be8d41cd9ec04 to your computer and use it in GitHub Desktop.
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