程序迷宫删除旧碰撞者

时间:2017-06-16 03:26:47

标签: c# unity5 procedural-generation

我遵循了一个教程,使用C#制作程序生成的迷宫,当你点击鼠标按钮时,它会产生一个新的迷宫。

然而,当我生成一个新的迷宫时,一切看起来都正确,但旧迷宫网格的MeshCollider仍然存在,此外还有新的迷宫MeshCollider制作了无数的隐形墙。

如何清除脚本中的旧MeshCollider,以便删除它们并且只保留新生成的网格?

初始迷宫图片

Initial maze image

第二个迷宫图片

Second maze image

在第一个迷宫之后,每个迷宫都有绿色轮廓所代表的旧MeshCollider,看起来它只是制作新的MeshCollider而不是用新的替换旧的MeshCollider。我想我可以通过将网格和using System.Collections; using System.Collections.Generic; using UnityEngine; public class Mesh_Generator : MonoBehaviour { public SquareGrid squareGrid; public MeshFilter walls; public MeshFilter cave; public bool is2D; List<Vector3> vertices; List<int> triangles; Dictionary<int,List<Triangle>> triangleDictionary = new Dictionary<int, List<Triangle>> (); List<List<int>> outlines = new List<List<int>> (); HashSet<int> checkedVerticies = new HashSet<int> (); public void GenerateMesh(int[,] map, float squareSize){ triangleDictionary.Clear (); outlines.Clear (); checkedVerticies.Clear (); squareGrid = new SquareGrid (map, squareSize); vertices = new List<Vector3> (); triangles = new List<int> (); for (int x = 0; x < squareGrid.squares.GetLength (0); x++) { for (int y = 0; y < squareGrid.squares.GetLength (1); y++) { TriangulateSquare (squareGrid.squares [x, y]); } } Mesh mesh = null; mesh = new Mesh (); cave.mesh = mesh; mesh.vertices = vertices.ToArray (); mesh.triangles = triangles.ToArray (); mesh.RecalculateNormals (); if (!is2D) { CreateWallMesh (); } } void CreateWallMesh(){ CalculateMeshOutLines (); List<Vector3> wallVerticies = new List<Vector3> (); List<int> wallTriangles = new List<int> (); Mesh wallMesh = new Mesh (); float wallHeight = 5; foreach (List<int> outline in outlines){ for (int i = 0; i < outline.Count -1; i ++){ int startIndex = wallVerticies.Count; wallVerticies.Add (vertices [outline [i]]); // left vertex wallVerticies.Add (vertices [outline [i+1]]); // right vertex wallVerticies.Add (vertices [outline [i]] - Vector3.up * wallHeight); // bottom left vertex wallVerticies.Add (vertices [outline [i+1]] - Vector3.up * wallHeight); // bottom right vertex wallTriangles.Add (startIndex + 0); //triangle 1 wallTriangles.Add (startIndex + 2); wallTriangles.Add (startIndex + 3); wallTriangles.Add (startIndex + 3); //triangle 2 wallTriangles.Add (startIndex + 1); wallTriangles.Add (startIndex + 0); } } wallMesh.vertices = wallVerticies.ToArray (); wallMesh.triangles = wallTriangles.ToArray (); walls.mesh = wallMesh; MeshCollider wallCollider = walls.gameObject.AddComponent<MeshCollider> (); wallCollider = null; wallCollider = new MeshCollider (); wallCollider.sharedMesh = wallMesh; } 设置为null来解决这个问题,然后在每个新迷宫生成开始时使用新网格,但它什么都不做:

org.springframework.web.client.RestTemplate

1 个答案:

答案 0 :(得分:0)

here开始,您应该在重建三角形数组之前调用Mesh.Clear。“

mesh = new Mesh ();
cave.mesh.Clear();
cave.mesh = mesh;