unity 绘制三角形
时间: 2023-09-28 12:12:21 浏览: 47
在 Unity 中绘制三角形可以通过使用 Mesh 类来实现。以下是一个简单的示例代码:
```csharp
using UnityEngine;
public class Triangle : MonoBehaviour
{
private Mesh mesh;
void Start()
{
mesh = new Mesh();
GetComponent<MeshFilter>().mesh = mesh;
Vector3[] vertices = new Vector3[3];
vertices[0] = new Vector3(0, 0, 0);
vertices[1] = new Vector3(0, 1, 0);
vertices[2] = new Vector3(1, 0, 0);
int[] triangles = new int[3];
triangles[0] = 0;
triangles[1] = 1;
triangles[2] = 2;
mesh.vertices = vertices;
mesh.triangles = triangles;
}
}
```
在这个示例中,我们创建了一个 Mesh 对象并将其赋值给 MeshFilter 组件。接下来,我们定义三个顶点和一个三角形,然后将它们分别赋值给 Mesh 对象的 vertices 和 triangles 属性。最终,就可以在场景中看到一个平面的三角形了。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)