C# 程序迷宫移除旧碰撞器

C# 程序迷宫移除旧碰撞器,c#,unity5,procedural-generation,C#,Unity5,Procedural Generation,我遵循了一个教程,使用C#在unity中制作一个程序生成的迷宫,当你点击鼠标按钮时,它会生成一个新的迷宫 然而,当我生成一个新的迷宫时,一切看起来都正常,但是旧迷宫网格的meshcliller仍然存在,除了新的迷宫meshcliller制作了许多看不见的墙 如何清除脚本中的旧meshclillers,以便删除它们,只保留新生成的网格 初始迷宫图像 第二个迷宫图像 在第一个迷宫之后,每个迷宫都有一个由绿色轮廓表示的旧的MeshColliders,看起来它只是在制作新的MeshColliders

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

然而,当我生成一个新的迷宫时,一切看起来都正常,但是旧迷宫网格的
meshcliller
仍然存在,除了新的迷宫
meshcliller
制作了许多看不见的墙

如何清除脚本中的旧
meshcliller
s,以便删除它们,只保留新生成的网格

初始迷宫图像

第二个迷宫图像

在第一个迷宫之后,每个迷宫都有一个由绿色轮廓表示的旧的
MeshCollider
s,看起来它只是在制作新的
MeshCollider
s,而不是用新的来替换旧的。我想我可以通过将网格和
MeshCollider
设置为null来解决这个问题,然后在每个新迷宫生成的开始处创建新网格,但它什么也不做:

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;
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类网格生成器:单行为{
公共方格网;
公共过滤墙;
公共过滤洞;
公共图书馆是2D;
列出顶点;
列出三角形;
Dictionary triangleDictionary=新字典();
列表大纲=新列表();
HashSet checkedVerticies=新HashSet();
公共void GenerateMesh(int[,]map,float squareSize){
Clear();
轮廓。清晰();
checkedVertices.Clear();
squareGrid=新的squareGrid(地图,squareSize);
顶点=新列表();
三角形=新列表();
对于(int x=0;x
从中,您应该调用
网格。在重建三角形阵列之前清除


我想我找到了问题的根源,正在发生的是,在我的地图生成器脚本中,它调用了上面列出的程序,该程序创建了一个MeshCollider
MeshCollider-wallCollider=walls.gameObject.AddComponent();
我需要做的是让它只在第一次发生,而不是在每次连续单击时发生
mesh = new Mesh ();
cave.mesh.Clear();
cave.mesh = mesh;