无法在运行时将网格与网格创建合并

时间:2019-07-18 08:05:13

标签: c# unity3d mesh

我有以下代码:

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个: enter image description here 2-我通过这些路径循环生成Delaunay的网格。 对于1个网格,它可以很好地工作,但是我找不到结合它的方法。

团结的例子使用了我没有的其他一些子游戏对象...

有人可以解决吗?

1 个答案:

答案 0 :(得分:0)

我终于找到了一些不是优化但可以正常工作的东西:

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