用于通过脚本创建或修改网格的类。
网格包含顶点和多个三角形数组。
请参阅程序化示例项目
以了解使用网格接口的示例。
三角形数组是顶点数组的简单索引;每个三角形三个索引。
对于每个顶点,可能有一条法线、两个纹理坐标、颜色和切线。
不过这些内容是可选的,可以随意删除。所有顶点信息都存储在具有相同
大小的单独数组中,因此如果网格有 10 个顶点,则还会有大小为 10 的数组
以用于法线和其他属性。
您可能需要将可修改的网格接口用于 3 种用途:
**1.从头开始构建网格**:
应始终按以下顺序进行:\
a) 分配 vertices\
b) 分配 triangles。
using UnityEngine;
public class Example : MonoBehaviour { Vector3[] newVertices; Vector2[] newUV; int[] newTriangles;
void Start() { Mesh mesh = new Mesh(); GetComponent<MeshFilter>().mesh = mesh; mesh.vertices = newVertices; mesh.uv = newUV; mesh.triangles = newTriangles; } }
**2.每一帧都修改顶点属性**:\ a) 获取顶点\ b) 修改它们\ c) 将它们分配回网格。
using UnityEngine;
public class Example : MonoBehaviour { void Update() { Mesh mesh = GetComponent<MeshFilter>().mesh; Vector3[] vertices = mesh.vertices; Vector3[] normals = mesh.normals;
for (var i = 0; i < vertices.Length; i++) { vertices[i] += normals[i] * Mathf.Sin(Time.time); }
mesh.vertices = vertices; } }
**3.连续更改网格三角形和顶点**:\
a) 调用 Clear 以开始刷新\
b) 分配顶点和其他属性\
c) 分配三角形索引。
在分配新顶点或三角形之前调用 Clear 十分重要。
Unity 始终检查提供的三角形索引是否未引用超出边界的顶点。
调用 Clear,分配顶点,然后分配三角形可确保不会有超出边界的数据。
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 | 绑定姿势。每个索引处的绑定姿势引用索引相同的骨骼。 |
blendShapeCount | 返回此网格上的 BlendShape 计数。 |
boneWeights | 每个顶点的骨骼权重。 |
bounds | 网格的包围体。 |
colors | 网格的顶点颜色。 |
colors32 | 网格的顶点颜色。 |
indexFormat | 网格索引缓冲区数据的格式。 |
isReadable | 如果网格支持读/写,则返回 true,否则返回 false。 |
normals | 网格的法线。 |
subMeshCount | Mesh 对象中的子网格数。 |
tangents | 网格的切线。 |
triangles | 包含网格中所有三角形的数组。 |
uv | 网格的基础纹理坐标。 |
uv2 | 网格的第二个纹理坐标集(如果存在)。 |
uv3 | 网格的第三个纹理坐标集(如果存在)。 |
uv4 | 网格的第四个纹理坐标集(如果存在)。 |
uv5 | 网格的第五个纹理坐标集(如果存在)。 |
uv6 | 网格的第六个纹理坐标集(如果存在)。 |
uv7 | 网格的第七个纹理坐标集(如果存在)。 |
uv8 | 网格的第八个纹理坐标集(如果存在)。 |
vertexBufferCount | 获取网格中存在的顶点缓冲区数。(只读) |
vertexCount | 返回网格中的顶点数(只读)。 |
vertices | 返回顶点位置的副本或分配新顶点位置数组。 |
Mesh | 创建空网格。 |
AddBlendShapeFrame | 添加新的混合形状帧。 |
Clear | 清除所有顶点数据和所有三角形索引。 |
ClearBlendShapes | 从网格中清除所有混合形状。 |
CombineMeshes | 将多个网格合并到此网格中。 |
GetBaseVertex | 获取给定 sub-mesh 的基顶点索引。 |
GetBindposes | 获取此实例的绑定姿势。 |
GetBlendShapeFrameCount | 返回混合形状的帧计数。 |
GetBlendShapeFrameVertices | 检索混合形状帧的 deltaNormals、deltaNormals 和 /deltaTangents/。 |
GetBlendShapeFrameWeight | 返回混合形状帧的权重。 |
GetBlendShapeIndex | 按给定名称返回 BlendShape 的索引。 |
GetBlendShapeName | 按给定索引返回 BlendShape 的名称。 |
GetBoneWeights | 获取此实例的骨骼权重。 |
GetColors | 获取此实例的顶点颜色。 |
GetIndexCount | 获取给定 sub-mesh 的索引计数。 |
GetIndexStart | 对于给定 /sub-mesh/,获取网格索引缓冲区中的起始索引位置。 |
GetIndices | 获取指定子网格的索引列表。 |
GetNativeIndexBufferPtr | 检索指向索引缓冲区的原生(底层图形 API)指针。 |
GetNativeVertexBufferPtr | 检索指向顶点缓冲区的原生(底层图形 API)指针。 |
GetNormals | 获取此实例的顶点法线。 |
GetTangents | 获取此实例的切线。 |
GetTopology | 获取子网格的拓扑。 |
GetTriangles | 获取此对象上指定子网格的切线列表。 |
GetUVDistributionMetric | UV 分布指标可用于根据摄像机的位置计算所需的 Mipmap 级别。 |
GetUVs | 获取网格的 UV。 |
GetVertices | 获取此实例的顶点位置。 |
MarkDynamic | 优化网格以便频繁更新。 |
RecalculateBounds | 从顶点重新计算网格的包围体。 |
RecalculateNormals | 从三角形和顶点重新计算网格的法线。 |
RecalculateTangents | 从法线和纹理坐标重新计算网格的切线。 |
SetColors | 网格的顶点颜色。 |
SetIndices | 为子网格设置索引缓冲区。 |
SetNormals | 设置网格的法线。 |
SetTangents | 设置网格的切线。 |
SetTriangles | 为子网格设置三角形列表。 |
SetUVs | 设置网格的 UV。 |
SetVertices | 分配新的顶点位置数组。 |
UploadMeshData | 将以前进行的网格修改上传到图形 API。 |
GetInstanceID | 返回对象的实例 ID。 |
ToString | 返回 GameObject 的名称。 |
Destroy | 删除 GameObject、组件或资源。 |
DestroyImmediate | 立即销毁对象 /obj/。强烈建议您改用 Destroy。 |
DontDestroyOnLoad | 在加载新的 Scene 时,请勿销毁 Object。 |
FindObjectOfType | 返回第一个类型为 type 的已加载的激活对象。 |
FindObjectsOfType | 返回所有类型为 type 的已加载的激活对象的列表。 |
Instantiate | 克隆 original 对象并返回克隆对象。 |
bool | 该对象是否存在? |
operator != | 比较两个对象是否引用不同的对象。 |
operator == | 比较两个对象引用,判断它们是否引用同一个对象。 |
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.