Hello!
I searched here some answers but they didn't help me with my problem...
I want to create a random cave generator (see the [lesson][1]) but with some modes. The idea is simple – I press RMB and new cave is generated. I decided to load material from recourses by code.
What I’m doing:
- 1. I'm generating map and loading material "Cave".
- 2. I'm creating a helping squares to draw mesh with a "new CSquareGridXY(genMap, width, height, 1)".
private CRandMap map;
private Material caveMat;
void Start () {
SetUI();
map = new CRandMap(width, height, threshold,useSeed, seed);
genMap = map.Generate();
caveMat = Resources.Load(“Cave”) as Material;
new CMeshGenerator(new CSquareGridXY(genMap, width, height, 1), caveMat);
}
- 3. In a “new CMeshGenerator” I’m creating vertices and triangles and assaying them all. Here is the leak, but i don’t know where exactly is. In method “prepareMesh” I fill the Lists of vertices and triangles
public class CMeshGenerator { public List<Vector3> vertices; public List<int> triangles; MeshFilter meshF; MeshRenderer meshR; Mesh mesh; public CMeshGenerator(CSquareGridXY grid, Material material) { vertices = new List<Vector3>(); triangles = new List<int>(); mesh = new Mesh(); GameObject gameObject = GameObject.Find("GameController"); meshF = gameObject.GetComponent<MeshFilter>(); if(meshF == null) meshF = GameObject.Find("GameController").AddComponent<MeshFilter>(); meshR = gameObject.GetComponent<MeshRenderer>(); if (meshR == null) meshR = GameObject.Find("GameController").AddComponent<MeshRenderer>(); for (int y = 0; y < grid.squares.GetLength(1); y++) { for (int x = 0; x < grid.squares.GetLength(0); x++) { PrepareMesh(grid.squares[x, y]); } } mesh.SetVertices(vertices); mesh.SetTriangles(triangles,0); mesh.RecalculateNormals(); meshF.sharedMesh = mesh; meshR.sharedMaterial = material; } ... }
- 4. On Update I only recreate my CMeshGeneratoclass.
void Update () { CheckUI(); if (Input.GetMouseButtonDown(1)) { map.MapUpdate(threshold, useSeed, seed); genMap = map.Generate(); new CMeshGenerator(new CSquareGridXY(genMap, width, height, 1), caveMat); GC.Collect(); }
I'll be glad if someone can help me with this problem :)