Skip to content

Instantly share code, notes, and snippets.

@davepape
Last active October 5, 2015 14:49
Show Gist options
  • Select an option

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

Select an option

Save davepape/beadf4cf742b6b4784ed to your computer and use it in GitHub Desktop.
create a Unity mesh with UV texture coordinates
#pragma strict
// Script to create a mesh with 2 triangles with texture coordinates (UV).
// This expects to be attached to a GameObject that already has a mesh
// and material (such as a sphere), so that you can assign the texture
// through the Unity editor, instead of here in the code
// It uses the mesh's Clear() function to erase the old geometry before
// creating the triangles.
function Start ()
{
var p0 : Vector3 = Vector3(-1,0,-1);
var p1 : Vector3 = Vector3(0,1,-1);
var p2 : Vector3 = Vector3(1,0,-1);
var p3 : Vector3 = Vector3(0, -1,-1);
var newVertices = [ p0, p1, p2, p3 ];
var newUV = [ Vector2(0,0.5), Vector2(0.5,1), Vector2(1,0.5), Vector2(0.5,0) ];
var newTriangles = [ 0, 1, 2, 2, 3, 0 ];
var m = GetComponent.<MeshFilter>().mesh;
m.Clear();
m.vertices = newVertices;
m.uv = newUV;
m.triangles = newTriangles;
m.RecalculateNormals();
}
function Update()
{
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment