Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Slein1 · Jan 26, 2017 at 12:54 PM · c#editormesheditor-scripting

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).

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Adam-Mechtley · Jan 26, 2017 at 01:08 PM 0
Share

Can you provide some context? Where is this code executed exactly?

avatar image Slein1 Adam-Mechtley · Jan 26, 2017 at 01:22 PM 0
Share

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).

avatar image Adam-Mechtley Adam-Mechtley · Jan 26, 2017 at 01:48 PM 0
Share

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.?

avatar image Slein1 Adam-Mechtley · Jan 26, 2017 at 02:07 PM 0
Share

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)

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Slein1 · Jan 26, 2017 at 03:39 PM 0
Share

What I want to do is the following:

  1. Copy some model to Assets folder

  2. Run Unity from the commandline and add it to a scene. (mesh + textures)

  3. 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.

avatar image Adam-Mechtley Slein1 · Jan 26, 2017 at 04:01 PM 0
Share

The same way you did above, e.g., AssetDatabase.LoadAssetAtPath<GameObject>("Assets/$$anonymous$$odels/scar_h.fbx");

avatar image Slein1 Adam-Mechtley · Jan 26, 2017 at 04:27 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

318 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges