I’m using the method BuildQuad() to build a mesh (I don’t really need to but let’s ignore that).
I’m running the script calling the method in the editor. When I switch scenes, I get this message:
Cleaning up leaked objects in scene since no game object, component or manager is referencing them
Mesh My Built Mesh has been leaked 1 times.
UnityEditor.DockArea:OnGUI()
I don’t really understand why this generated mesh doesn’t get destroyed when I stop referencing it.
public void BuildQuad()
{
Vector3[] vertices = new Vector3[4];
Vector3[] normals = new Vector3[4];
Vector2[] uv = new Vector2[4];
int[] triangles = new int[6];
//manually create 4 vertices
//the size of the quad is columns * tileSize
vertices[0] = Vector3.zero;
vertices[3] = new Vector3(0, columns*tileSize);
vertices[2] = new Vector3(rows*tileSize, columns*tileSize);
vertices[1] = new Vector3(rows*tileSize, 0);
//assign those vertices to triangles
triangles[0] = 0;
triangles[1] = 3;
triangles[2] = 1;
triangles[3] = 3;
triangles[4] = 2;
triangles[5] = 1;
//create a very simple uv map
uv[0] = new Vector2(0, 0f);
uv[2] = new Vector2(1f, 1f);
uv[3] = new Vector2(0, 1f);
uv[1] = new Vector2(1f, 0);
// Create a new Mesh and populate with the data
Mesh mesh = new Mesh();
mesh.vertices = vertices;
mesh.triangles = triangles;
mesh.normals = normals;
mesh.uv = uv;
mesh.name = "My Built Mesh";
// Assign our mesh to our filter/renderer/collider
mesh_filter.sharedMesh = mesh;
mesh_collider.sharedMesh = mesh;
BuildTexture();
}
private void OnApplicationQuit()
{
mesh_filter.sharedmesh = null;
mesh_collider.sharedMesh = null;
}