How to properly import mesh C# Editor

Hi there,

Hope someone can help me out, I’m working with .FBX files currently.

I did the following comparison, manually building my unity application leads to a mesh size in editor.log of 1.1 MB, whereas my build script which tries to load the same mesh only loads 13.5 KB. I’m guessing it’s only loading partially.

This is my current code for loading the mesh:

Mesh m = (Mesh)AssetDatabase.LoadAssetAtPath("Assets/Models/scar_h.fbx", typeof(Mesh));
GameObject Go = new GameObject();
Go.GetComponent<MeshFilter>().mesh = m;
Go.transform.localScale = new Vector3(0.25f, 0.25f, 0.25f);
Go.transform.position = new Vector3(0, 0, 2);

Perhaps I’m doing it the hard way, and should be using different code? (e.g saw a ModelImporter in unity C# api, but idk if that is more appropiate).

This scar_h model has 22 individual objects for example. This lead me to suspect that my script only imports one of those.

That is correct. You need to instead use AssetDatabase.LoadAllAssetsAtPath() and pick out the Meshes specifically, if they are all you need.

I’m not really clear what you’re trying to do though. If you have some model that you want added to the scene only at build time, you could load the GameObject at that path instead, and you would get the top-level GameObject in the hierarchy, as well as any meshes, materials, or other assets to which is has serialized references throughout its hierarchy.