Mesh Manual     Reference     Scripting  
Scripting > Runtime Classes > Mesh
Mesh Class, inherits from Object

A class that allows creating or modifying meshes from scripts.

Meshes contain vertices and multiple triangle arrays. See the Procedural example project for examples of using the mesh interface.

The triangle arrays are simply indices into the vertex arrays; three indices for each triangle.

For every vertex there can be a normal, two texture coordinates, color and tangent. These are optional though and can be removed at will. All vertex information is stored in separate arrays of the same size, so if your mesh has 10 vertices, you would also have 10-size arrays for normals and other attributes.

There are probably 3 things you might want to use the modifyable mesh interface for:

1. Building a mesh from scratch: should always be done in the following order: 1) assign vertices 2) assign triangles

function Start () {
var mesh = new Mesh ();
GetComponent (MeshFilter).mesh = mesh;
mesh.vertices = newVertices;
mesh.uv = newUV;
mesh.triangles = newTriangles;
}

2. Modifying vertex attributes every frame: 1) get vertices, 2) modify them, 3) assign them back to the mesh.

function Update () {
var mesh : Mesh = GetComponent(MeshFilter).mesh;
var vertices = mesh.vertices;
var normals = mesh.normals;
for (var i=0;i<vertices.length;i++)
{
vertices[i] += normals[i] * Mathf.Sin(Time.time);
}
mesh.vertices = vertices;
}

3. Continously changing the mesh triangles and vertices: 1) call Clear to start fresh, 2) assign vertices and other attributes, 3) assign triangle indices.

It is important to call Clear before assigning new vertices or triangles. Unity always checks the supplied triangle indices whether they don't reference out of bounds vertices. Calling Clear then assigning vertices then triangles makes sure you never have out of bounds data.

function Update () {
var mesh : Mesh = GetComponent(MeshFilter).mesh;

mesh.Clear();
mesh.vertices = newVertices;
mesh.uv = newUV;
mesh.triangles = newTriangles;
}

Variables
vertices

Returns a copy of the vertex positions or assigns a new vertex positions array.

normals

The normals of the mesh.

tangents

The tangents of the mesh.

uv

The base texture coordinates of the mesh.

uv2

The second texture coordinate set of the mesh, if present.

bounds

The bounding volume of the mesh.

colors

Returns the vertex colors of the mesh.

triangles

An array containing all triangles in the mesh.

vertexCount

Returns the number of vertices in the mesh (Read Only).

subMeshCount

The number of submeshes. Every material has a seperate triangle list.

boneWeights

The bone weights of each vertex

bindposes

The bind poses. The bind pose at each index refers to the bone with the same index.

Constructors
Mesh

Creates an empty mesh

Functions
Clear

Clears all vertex data and all triangle indices.

RecalculateBounds

Recalculate the bounding volume of the mesh from the vertices.

RecalculateNormals

Recalculates the normals of the mesh from the triangles and vertices.

Optimize

Optimizes the mesh for display.

GetTriangles

Returns the triangle list for the submesh.

SetTriangles

Sets the triangle list for the submesh

CombineMeshes

Combines several meshes into this mesh.

Inherited members
Inherited Variables
name

The name of the object.

hideFlags

Should the object be hidden, saved with the scene or modifyable by the user?

Inherited Functions
GetInstanceID

Returns the instance id of the object.

Inherited Class Functions
operator bool

Does the object exist?

Instantiate

Clones the object original and returns the clone.

Destroy

Removes a gameobject, component or asset.

DestroyImmediate

Destroys the object obj immediately. It is strongly recommended to use Destroy instead.

FindObjectsOfType

Returns a list of all active loaded objects of Type type.

FindObjectOfType

Returns the first active loaded object of Type type.

operator ==

Compares if two objects refer to the same

operator !=

Compares if two objects refer to a different object

DontDestroyOnLoad

Makes the object target not be destroyed automatically when loading a new scene.