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:
a) assign vertices
b) assign triangles.
using UnityEngine; using System.Collections;
public class ExampleClass : MonoBehaviour { public Vector3[] newVertices; public Vector2[] newUV; public int[] newTriangles; void Start() { Mesh mesh = new Mesh(); GetComponent<MeshFilter>().mesh = mesh; mesh.vertices = newVertices; mesh.uv = newUV; mesh.triangles = newTriangles; } }
2. Modifying vertex attributes every frame:
a) get vertices
b) modify them
c) assign them back to the mesh.
using UnityEngine; using System.Collections;
public class ExampleClass : MonoBehaviour { void Update() { Mesh mesh = GetComponent<MeshFilter>().mesh; Vector3[] vertices = mesh.vertices; Vector3[] normals = mesh.normals; int i = 0; while (i < vertices.Length) { vertices[i] += normals[i] * Mathf.Sin(Time.time); i++; } mesh.vertices = vertices; } }
3. Continously changing the mesh triangles and vertices:
a) call Clear to start fresh
b) assign vertices and other attributes
c) 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.
using UnityEngine;
public class ExampleClass : MonoBehaviour {
Vector3[] newVertices; Vector2[] newUV; int[] newTriangles; void Start() { Mesh mesh = GetComponent<MeshFilter>().mesh;
mesh.Clear(); // Do some calculations... mesh.vertices = newVertices; mesh.uv = newUV; mesh.triangles = newTriangles; } }
bindposes | The bind poses. The bind pose at each index refers to the bone with the same index. |
blendShapeCount | Returns BlendShape count on this mesh. |
boneWeights | The bone weights of each vertex. |
bounds | The bounding volume of the mesh. |
colors | Vertex colors of the Mesh. |
colors32 | Vertex colors of the Mesh. |
isReadable | Returns state of the Read/Write Enabled checkbox when model was imported. |
normals | The normals of the Mesh. |
subMeshCount | The number of sub-Meshes. Every Material has a separate triangle list. |
tangents | The tangents of the Mesh. |
triangles | An array containing all triangles in the Mesh. |
uv | The base texture coordinates of the Mesh. |
uv2 | The second texture coordinate set of the mesh, if present. |
uv3 | The third texture coordinate set of the mesh, if present. |
uv4 | The fourth texture coordinate set of the mesh, if present. |
vertexCount | Returns the number of vertices in the Mesh (Read Only). |
vertices | Returns a copy of the vertex positions or assigns a new vertex positions array. |
Mesh | Creates an empty Mesh. |
AddBlendShapeFrame | Adds a new blend shape frame. |
Clear | Clears all vertex data and all triangle indices. |
ClearBlendShapes | Clears all blend shapes from Mesh. |
CombineMeshes | Combines several Meshes into this Mesh. |
GetBlendShapeFrameCount | Returns the frame count for a blend shape. |
GetBlendShapeFrameVertices | Retreives deltaVertices, deltaNormals and deltaTangents of a blend shape frame. |
GetBlendShapeFrameWeight | Returns the weight of a blend shape frame. |
GetBlendShapeIndex | Returns index of BlendShape by given name. |
GetBlendShapeName | Returns name of BlendShape by given index. |
GetIndices | Returns the index buffer for the sub-Mesh. |
GetTopology | Gets the topology of a sub-Mesh. |
GetTriangles | Returns the triangle list for the sub-Mesh. |
GetUVs | Get the UVs for a given chanel. |
MarkDynamic | Optimize mesh for frequent updates. |
Optimize | Optimizes the Mesh for display. |
RecalculateBounds | Recalculate the bounding volume of the Mesh from the vertices. |
RecalculateNormals | Recalculates the normals of the Mesh from the triangles and vertices. |
SetColors | Vertex colors of the Mesh. |
SetIndices | Sets the index buffer for the sub-Mesh. |
SetNormals | Set the normals of the Mesh. |
SetTangents | Set the tangents of the Mesh. |
SetTriangles | Sets the triangle list for the sub-Mesh. |
SetUVs | Set the UVs for a given chanel. |
SetVertices | Assigns a new vertex positions array. |
UploadMeshData | Upload previously done Mesh modifications to the graphics API. |
hideFlags | Should the object be hidden, saved with the scene or modifiable by the user? |
name | The name of the object. |
GetInstanceID | Returns the instance id of the object. |
ToString | Returns the name of the game object. |
Destroy | Removes a gameobject, component or asset. |
DestroyImmediate | Destroys the object obj immediately. |
DontDestroyOnLoad | Makes the object target not be destroyed automatically when loading a new scene. |
FindObjectOfType | Returns the first active loaded object of Type type. |
FindObjectsOfType | Returns a list of all active loaded objects of Type type. |
Instantiate | Returns a copy of the object original. |
bool | Does the object exist? |
operator != | Compares if two objects refer to a different object. |
operator == | Compares two object references to see if they refer to the same object. |