Hello,
I’m currently generating a mesh in the Editor (not in play time) and am able to modify it. The mesh is a “terrain” made of multiple hexagone tiles. The edition consists of raising or lowering a tile. It’s working well, but as the mesh is not saved into an asset, it’s saved into the scene file (I suppose).
What I would like is a way to
- Save my generated mesh into its own asset
- Save the gameobject containing my generated mesh (and other stuff) in its own prefab
- Be able to modify the mesh
I managed to save the mesh in its own asset file (using AssetDatabase CreateAsset and SaveAssets), and the edition still works after that.
But when I try to create a Prefab from my GameObject (drag and dropping in a folder) and try to edit the mesh as before, the editor is freezing, and a pop-up displays:
- Hold on (busy for xxs…)
- Application.UpdateScene
- Waiting for Unity’s code to finish executing
It takes approximately 2-3 minutes, after that, the mesh is modified as wanted.
(tbh, if I generate my mesh, create an asset from this mesh, try to edit it, create a prefab from the GameObject, try to edit it, is will work once or twice. After that, the behaviour written above will apply)
Is this workflow possible? Am I missing something? Doing something wrong?
Thanks in advance!
The code to generate the mesh / regenerate the mesh after an edition:
void UpdateMesh()
{
if (!GetComponent<MeshFilter>().sharedMesh)
{
mesh = new Mesh();
mesh.name = $"chunk_{_posX}_{_posY}";
GetComponent<MeshFilter>().sharedMesh = mesh;
}
else
{
mesh = GetComponent<MeshFilter>().sharedMesh;
}
mesh.Clear();
mesh.vertices = _newVertices;
mesh.uv = _newUV;
mesh.triangles = _newTriangles;
//mesh.Optimize();
mesh.RecalculateNormals();
col = GetComponent<MeshCollider>();
col.sharedMesh = null;
col.sharedMesh = mesh;
}
The code to save the mesh in its own asset:
public static void SaveMesh(Mesh mesh, string path)
{
AssetDatabase.CreateAsset(mesh, path);
AssetDatabase.SaveAssets();
}
called from
SaveMesh(generatedChunk.GetComponent<MeshFilter>().sharedMesh, meshPath);