I have been creating some editing tools to aid in indoor level creating within Unity.
I have a Script called CubeInfo which has a single variable called cubeSize, a Vector3.
I made a Custom Editor class called CubeEditor which has an OnInspectorGUI method which allows editing the cubeSize.
If the GUI.changed variable is set, I call a Rebuild() method on the CubeInfo object.
The problem is with modifying meshes.
I cannot just grab the MeshFilter.mesh variable, because the editor yells at me to use sharedMesh.
If I use sharedMesh, I end up changing the mesh of any other objects I duplicated ( Ctrl-D ).
I can create a Mesh each time, but this is quite wasteful as a new one is generated each time I change cubeSize in the GUI. Basically there is no way from script to know when to modify a mesh and when to create a new one.
It seems quite odd that the Editor has no way to give you a modifiable copy of the Mesh like the .mesh variable does at runtime.
I tried going down that road but it got messy and ended up making the custom tools using scales instead. I do however know how to get you past the first few humps in the road:
Making a new shared mesh instance:
string meshName = meshFilter.sharedMesh.name;
meshFilter.sharedMesh = Object.Instantiate(meshFilter.sharedMesh) as Mesh;
meshFilter.sharedMesh.name = meshName;
Is the mesh stored persistently somewhere in the AssetDatabase?:
If it is not persistent, it is because you already made a new instance and the mesh is floating in the scene.
Saving an instance of a mesh back to the assetdatabase by adding to an existing object:
AssetDatabase.AddObjectToAsset(meshFilter.sharedMesh,<Some asset in the DB>);
..or by creating a new object:
AssetDatabase.CreateAsset(meshFilter.sharedMesh, "Assets/Meshes/" + meshFilter.sharedMesh.name + ".asset");
Things start to get really messy when your newly created mesh instance is stored inside a prefab and that prefab is used in another scene, because the mesh instance was stored in the scene and not the prefab. You can add your mesh to the prefab using the AddObjectToAsset but there are no API functions to remove it again or move it to another object. In fact you are not even allowed to save it again to a new object using AddObjectToAsset, so it is just stuck in the prefab.