How can I delete a quad (mesh) by code?
Hello! So, I have a large 2D field where the ground and everything else created by mesh in the code. It's mean that vertices-uv-triangles has been created and the proper texture added to it. The field is one game object!
I have to delete the given mesh (quad) when I click by mouse. At this moment I use a different approach: I have an empty tile in my set and when I click the mesh uv change to this empty tile.... It looks like a deleted mesh but now, I must delete the quad properly. Of course I can't simply delete the mesh component as I need it, I want to delete only the certain quad.
Check picture to get what I mean! Should look like this after delete:
Is there any way to do it? I gone mad soon as I can't get the solution by myself :)
Answer by lgarczyn · Dec 22, 2019 at 07:11 PM
Simply convert the world coordinates of a Raycast hit into local coordinates, then iterate on all the triangles of the mesh.
For each triangle:
get the position of the 3 vertices
create a Bounds that contains all vertices using Encapsulate
check if the raycast hit is inside the Bounds
if it is, mark the triangle for deletion using a list
Then, create a new triangle list, but ignore triangles inside the delete list.
Then apply the triangle list to the mesh
Then if you want to be clean, delete orphan points: https://forum.unity.com/threads/remove-vertices-that-are-not-in-triangle-solved.342335/