无法在运行时将网格与网格创建相结合 [英] Can't combine mesh from mesh create on runtime
本文介绍了无法在运行时将网格与网格创建相结合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有此代码:
public void BrowseColliderToCreateMesh (PolygonCollider2D polygonColliderAdded){
//browse all path from collider
pathCount=polygonColliderAdded.pathCount;
CombineInstance[] combine = new CombineInstance[pathCount];
for (int i = 0; i < pathCount; i++)
{
Vector2[] path = polygonColliderAdded.GetPath(i);
Polygon2D polygon = Polygon2D.Contour(path);
Triangulation2D triangulation = new Triangulation2D(polygon, 22.5f);
// build a mesh from triangles in a Triangulation2D instance
singleMesh = triangulation.Build();
combine[i].mesh = singleMesh;
}
testDelaunay.GetComponent<MeshFilter>().mesh = new Mesh;
testDelaunay.GetComponent<MeshFilter>().mesh.CombineMeshes(combine);
}
1-我有一个来自polygonCollider2D的点的列表,分为3个: 2-我遍历这些路径以与Delaunay生成网格. 对于1个网格,它可以很好地工作,但是我找不到将其组合的方法.
1- I have a list of point from a polygonCollider2D, divide in 3 : 2- I loop through these path to generate mesh with Delaunay. For 1 mesh it work well, but I can't find a way to combine it.
团结的例子使用了我没有的其他一些子游戏对象...
Example from unity use some other children gameobject that I don't have...
有人可以解决吗?
推荐答案
我终于找到了一些不是优化但可以工作的东西:
I finally find something which is not optimize but which work :
private Mesh CombineMeshes(List<Mesh> meshes)
{
var combine = new CombineInstance[meshes.Count];
for (int i = 0; i < meshes.Count; i++)
{
combine[i].mesh = meshes[i];
combine[i].transform = transform.localToWorldMatrix;
}
var mesh = new Mesh();
mesh.CombineMeshes(combine);
return mesh;
}
public void BrowseColliderToCreateMesh (PolygonCollider2D polygonColliderAdded){
pathCount=polygonColliderAdded.pathCount;
for (int i = 0; i < pathCount; i++)
{
if(i==0){
Vector2[] path = polygonColliderAdded.GetPath(i);
Polygon2D polygon = Polygon2D.Contour(path);
Triangulation2D triangulation = new Triangulation2D(polygon, 22.5f);
// build a mesh from triangles in a Triangulation2D instance
singleMesh = triangulation.Build();
}else if (i==1){
Vector2[] path = polygonColliderAdded.GetPath(i);
Polygon2D polygon = Polygon2D.Contour(path);
Triangulation2D triangulation = new Triangulation2D(polygon, 22.5f);
// build a mesh from triangles in a Triangulation2D instance
newMesh = triangulation.Build();
combineMesh=CombineMeshes(new List<Mesh> { newMesh, singleMesh });
}else if(i>1){
Vector2[] path = polygonColliderAdded.GetPath(i);
Polygon2D polygon = Polygon2D.Contour(path);
Triangulation2D triangulation = new Triangulation2D(polygon, 22.5f);
newMesh = triangulation.Build();
combineMesh=CombineMeshes(new List<Mesh> { newMesh, combineMesh });
}
}
GetComponent<MeshFilter>().mesh = combineMesh;
}
这篇关于无法在运行时将网格与网格创建相结合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文