How to save a procedural mesh as a Prefab?

I’m tring to add new types of 3D GameObjects to the Unity Editor, in addition to the predefined ones (cube, sphere, capsule, cylinder, plane and quad)… to do so I created a procedural mesh, the script does exacly what it is supposed to do, the new object is rendered correctly and materials and textures are also applied properly, everything works out just fine…

However if I drag the newly created object into the Assets folder, the resulting Prefab loses the Mesh Filter, so it is not rendered anymore. If I click on the select mesh filter button, I can only access the predefined ones, even if there’s a copy of it on the scene.

You can use an exporter like ExportOBJ, ObjExporter or Collada Exporter, and export your mesh to a file in the editor.

Another option is to use AssetDatabase.CreateAsset with your mesh, and then save it with AssetDatabase.SaveAssets();

I would like to suggest a possible other solution for the next readers facing this same problem

add [ExecuteInEditMode] before your mono class definition

it ll make your generative mesh stay on the scene .