How can I change a mesh with a model file after it was imported from editor code? I have tried modifying it via the sharedMesh instance of the prefab, but the changes are not persistent, as if the prefab was giving me only a copy. Next, I’ve tried to use the AssetPostProcessor, which works, but now I’m interested in knowing, if I can apply custom changes to a mesh in an imported model manually. Maybe by running a custom processor again or via some other method.
As an example:
I would like to modify vertex colors and normals via script on meshes of my FBX and OBJ files. Some of these changes apply to all models and can be done via a custom AssetPostProcessor like this:
public class MeshVertexColorProcessor : AssetPostprocessor
{
void OnPostprocessModel (GameObject go)
{
Mesh mesh = go.GetComponent<MeshFilter>().sharedMesh;
Color[] colors = new Color[mesh.vertexCount];
for (int i = 0; i < colors.Length; i++)
{
colors[i] = new Color(Random.value, Random.value, Random.value, 1f);
}
mesh.colors = colors;
}
}
Now all models get random vertex colors. It is important to note, that this seems to be the only way to do this kind of modification. When trying to modify the mesh after it was imported by getting a reference to it via the sharedMesh property on a prefab’s MeshFilter, it won’t persist changes. After reopening Unity, the mesh will be the way it was before in the model file. I’ve tested different scenarios, making sure that I was getting the correct reference and setting everything as dirty, but after reading the documentation, I realized, that the MeshFilter component associated with the imported model is only some kind of placeholder, which gets recreated on import.
After running my automated processor, I would still like to be able to select a few models manually and apply custom settings, but the only way seems to be via the OnPostprocessModel method, but this would mean re-importing and therefore the default automated processor also runs.
Can I somehow create a custom importer object in code and let it run a specific instance of my processor on selected models or am I missing something obvious to make changes to their meshes persist?
