Version: 2017.2

Mesh

class in UnityEngine

/

Hereda de:Object

Cambiar al Manual

Descripción

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; } }

Variables

bindposesThe bind poses. The bind pose at each index refers to the bone with the same index.
blendShapeCountReturns BlendShape count on this mesh.
boneWeightsThe bone weights of each vertex.
boundsThe bounding volume of the mesh.
colorsVertex colors of the Mesh.
colors32Vertex colors of the Mesh.
isReadableReturns state of the Read/Write Enabled checkbox when model was imported.
normalsThe normals of the Mesh.
subMeshCountThe number of sub-Meshes. Every Material has a separate triangle list.
tangentsThe tangents of the Mesh.
trianglesAn array containing all triangles in the Mesh.
uvThe base texture coordinates of the Mesh.
uv2The second texture coordinate set of the mesh, if present.
uv3The third texture coordinate set of the mesh, if present.
uv4The fourth texture coordinate set of the mesh, if present.
vertexBufferCountGet the number of vertex buffers present in the Mesh. (Read Only)
vertexCountReturns the number of vertices in the Mesh (Read Only).
verticesReturns a copy of the vertex positions or assigns a new vertex positions array.

Constructores

MeshCreates an empty Mesh.

Funciones Públicas

AddBlendShapeFrameAdds a new blend shape frame.
ClearLimpia todos los datos de los índices de los vertices y los triángulos.
ClearBlendShapesClears all blend shapes from Mesh.
CombineMeshesCombines several Meshes into this Mesh.
GetBindposesGets the bind poses for this instance.
GetBlendShapeFrameCountReturns the frame count for a blend shape.
GetBlendShapeFrameVerticesRetreives deltaVertices, deltaNormals and deltaTangents of a blend shape frame.
GetBlendShapeFrameWeightReturns the weight of a blend shape frame.
GetBlendShapeIndexReturns index of BlendShape by given name.
GetBlendShapeNameReturns name of BlendShape by given index.
GetBoneWeightsGets the bone weights for this instance.
GetColorsGets the vertex colors for this instance.
GetIndexCountGets the index count of the given submesh.
GetIndexStartGets the starting index location within the Mesh's index buffer, for the given submesh.
GetIndicesGets the index buffer for the specified sub mesh on this instance.
GetNativeIndexBufferPtrRetrieves a native (underlying graphics API) pointer to the index buffer.
GetNativeVertexBufferPtrRetrieves a native (underlying graphics API) pointer to the vertex buffer.
GetNormalsGets the vertex normals for this instance.
GetTangentsGets the tangents for this instance.
GetTopologyGets the topology of a sub-Mesh.
GetTrianglesGets the triangle list for the specified sub mesh on this instance.
GetUVsGet the UVs for a given chanel.
GetVerticesGets the vertex positions for this instance.
MarkDynamicOptimize mesh for frequent updates.
RecalculateBoundsRecalculate the bounding volume of the Mesh from the vertices.
RecalculateNormalsRecalculates the normals of the Mesh from the triangles and vertices.
RecalculateTangentsRecalculates the tangents of the Mesh from the normals and texture coordinates.
SetColorsVertex colors of the Mesh.
SetIndicesSets the index buffer for the sub-Mesh.
SetNormalsSet the normals of the Mesh.
SetTangentsSet the tangents of the Mesh.
SetTrianglesSets the triangle list for the sub-Mesh.
SetUVsSet the UVs for a given chanel.
SetVerticesAssigns a new vertex positions array.
UploadMeshDataUpload previously done Mesh modifications to the graphics API.

Miembros heredados

Variables

hideFlags¿Debería el objeto estar oculto, guardado con la escena o modificable por el usuario?
nameEl nombre del objeto.

Funciones Públicas

GetInstanceIDDevuelve el id de la instancia del objeto.
ToStringDevuelve el nombre del objeto.

Funciones Estáticas

DestroyElimina un gameobject, componente o asset.
DestroyImmediateDestroys the object obj immediately. You are strongly recommended to use Destroy instead.
DontDestroyOnLoadHace que el objeto target no sea destruido automáticamente cuando se cargue una nueva escena.
FindObjectOfTypeDevuelve el primer objeto activo cargado de tipo type.
FindObjectsOfTypeDevuelve una lista de todos los objetos activos cargados de tipo type.
InstantiateClona el objeto original y devuelve el clon.

Operadores

bool¿Existe el objeto?
operator !=Compare si dos objetos se refieren a un objeto diferente.
operator ==Compara dos referencias de objeto para ver si se refieren al mismo objeto.