- Home /
Possible material or mesh leak
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) 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 :)
Answer by Bunny83 · Oct 21, 2015 at 03:20 AM
Materials and Meshes are like most things that are derived from UnityEngine.Object managed by Unity itself. So they aren't garbage collected even when you have lost all references to them. You can still regain a reference by using FindObjectsOfType.
Since you replace the mesh of an existing object with a new one, the old Mesh still exists. So you either should "Destroy()" the old mesh before you assign the new one, or simple reuse the mesh. You can call Clear() on the mesh to start new. However if you assign new arrays to the mesh that isn't even necessary.
So instead of mesh = new Mesh();
you should do after you have the MeshFilter reference:
mesh = meshF.sharedMesh;
if(mesh == null)
meshF.sharedMesh = mesh = new Mesh();
// mesh.Clear();
The Material shouldn't be a problem since you already reuse the same material and assign it to the sharedMaterial property. Watch out: If you call Destroy on references to assets in your project, they will be gone for good. That's actually true for all kind of assets. In a build those assets won't be erased forever, but you would have to restart the application if you delete an asset. In the editor however you literally delete it. So be careful when you "clean up" ^^. Unity has now a security check when you try to destroy an asset. That's why you shouldn't use DestroyImmediate in runtime code ^^.
Omg! I'm so thankful for you! You are my savior! :3 Your advice solved my problem) But I steel have some misunderstanding: Why I have had leak (20-40 kb) every second, when I created only one mesh (call constructor only once). And when I use old mesh (mesh = meshF.shared$$anonymous$$esh
) leaks stoppes. I can understand if I were constantly calling constructor and it was creating new mesh every time and loosing old pointer, but even when I called it once I have had leak, why is that? =\