Skip to content

Instantly share code, notes, and snippets.

@davepape
Created September 29, 2015 04:45
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/31c65060a53fbea5d77f to your computer and use it in GitHub Desktop.
Save davepape/31c65060a53fbea5d77f to your computer and use it in GitHub Desktop.
create a mesh in Unity, reading some data from a text file
#pragma strict
// Script to create a mesh with 2 triangles, getting colors from a text file.
function Start ()
{
gameObject.AddComponent.<MeshFilter>();
gameObject.AddComponent.<MeshRenderer>();
var mat = new Material(Shader.Find("Custom/Vertex Colored"));
GetComponent.<Renderer>().material = mat;
var p0 : Vector3 = Vector3(-2,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 s = new System.IO.File.ReadAllLines('colors.txt');
var newColors = new Color[4];
for (var i=0; (i < s.Length) && (i < 4); i++)
{
var vals = s[i].Split(" "[0]);
newColors[i] = Color(parseFloat(vals[0]),parseFloat(vals[1]),parseFloat(vals[2]));
}
var newTriangles = [ 0, 1, 2, 0, 2, 3 ];
var m = GetComponent.<MeshFilter>().mesh;
m.vertices = newVertices;
m.colors = newColors;
m.triangles = newTriangles;
}
function Update()
{
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment