how to get .fbx vertex count when unity import it

void OnPreprocessModel()
{
ModelImporter modelImporter = assetImporter as ModelImporter;
modelImporter.importMaterials = false;
}
void OnPostprocessModel(GameObject g)
{
GameObject go =GameObject.Instantiate(g);
MeshFilter meshFilter;
foreach (Transform child in g.transform)
{
meshFilter = child.GetComponent();
if (meshFilter!=null)
{
Debug.LogError(meshFilter.mesh.vertexCount);
//i want to get it !!!
}
}
}

I try to get it , but unluckly no use. because when it create successed, the Skinned Mesh Render-- Mesh property is Missing(Mesh)

please help!
please help!
please help!

Well there are a few things you should keep in mind:

  • First of all an imported model might have either a MeshRenderer + MeshFilter component or a SkinnedMeshRenderer. So you may want to check for both a SkinnedMeshRenderer and a MeshFilter in case no SkinnedMeshRenderer was found.
  • The second point is: do not use the mesh property inside the editor, ever. The mesh property will try to instantiate the mesh for this MeshFilter only. However this only works at runtime and is only ment for runtime use. Inside the editor you should always use the sharedMesh property.
  • inside editor code you should not use Instantiate unless you really know what that means. Instantiating an object means you create a clone in the current scene. Note that this clone is not connected to the prefab / source object it came from. For anything prefab related inside the editor you want to have a look at the PrefabUtility class and in particular the InstantiatePrefab method.

To count all the vertices of a prefab when it’s imported you may want to use:

void OnPostprocessModel(GameObject g)
{
    int vertexCount = 0;
    foreach (var smr = g.GetComponentsInChildren<SkinnedMeshRenderer>())
        vertexCount += smr.sharedMesh.vertexCount;
    foreach (var mf = g.GetComponentsInChildren<MeshFilter>())
        vertexCount += mf.sharedMesh.vertexCount;
    Debug.Log("Imported model "+g.name+" has " + vertexCount + " vertices", g);
}