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.AddComponent<MeshFilter>();
Go.AddComponent<MeshRenderer>();
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).
Can you provide some context? Where is this code executed exactly?
Yes, under my Assets folder I have a build.cs file which opens a scene and sets some Editor settings, then tries to add the model to the scene and eventually builds a player. So I call Unity from the commandline in batch mode, and call the static function in this build.cs file. ( This becomes a HoloLens app eventually).
What does the process look like when you manually build? Are you manually adding a GameObject to the scene, adding the $$anonymous$$eshFilter/$$anonymous$$eshRenderer, assigning the $$anonymous$$esh reference, etc.?
I place the models also under Assets/$$anonymous$$odels together with their textures, Unity then discovers them on startup (same happens from the build script btw, it regenerates the meta files if I delete them). I then drag the model onto the scene and set the positon to x:0,y:0,z:2.
Important to note is perhaps that you can see that this model consists of many subobjects (in the hierarchy tab). This scar_h model has 22 individual objects for example. This lead me to suspect that my script only imports one of those.
Also I have an FBX version of the scar_h model that I corrected with blender, and the version that I downloaded somewhere. This original version does show the full mesh it seems. But for my other downloaded model the original version has the same problem as this scar_h exported from blender. ( I did this to correct the textures on this scar_h model). So perhaps the problem is also with different versions of the FBX format? ( At least in the build script, because in the editor there are no problems)
Answer by Adam-Mechtley · Jan 26, 2017 at 02:30 PM
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.
What I want to do is the following:
Copy some model to Assets folder
Run Unity from the commandline and add it to a scene. (mesh + textures)
Build with Nuget / $$anonymous$$SBuild to get a HoloLens app. (this already works)
So if I could load it as an GameObject that would be great, but I have the requirement that all sorts of models should be able to get loaded into my scene.
But I do not know how I would load it as a GameObject directly, I only found solutions to do that at runtime, which is not what I want.
The same way you did above, e.g., AssetDatabase.LoadAssetAtPath<GameObject>("Assets/$$anonymous$$odels/scar_h.fbx");
Ah yes, finally got it to work :)
Though I did need to call UnityEngine.Object.Instantiate(GameObject); Unlike the mesh it is not automatically added it seems.
Thanks a lot !
Your answer
Follow this Question
Related Questions
How to Have a ReoderableList in a Custom Editor Window 1 Answer
DrawGizmo - Accessing Editor script in DrawGizmo 1 Answer
Edit SkinnedMesh with vertices 0 Answers
How to read from editor preferences? 0 Answers
Gizmos and Handles 1 Answer