Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by cassius · Jun 29, 2013 at 02:38 AM · combinemeshescombineinstance

MeshFilter and CombineMesh Doesn't Build Properly

I have a script (below) that I'm using to combine the mesh of a series of child gameObjects to save resources. The combining of the mesh works perfectly while in Unity's Game Editor, however once I build the game and run it, the combined mesh is nowhere to be found. There also doesn't appear to be any related errors or warnings during the build.

I'm on latest Unity4. (by the way, the CombineMeshes documentation on Unity's site is wrong. It doesn't even compile!)

Here's the code I'm using.

 #pragma strict
 
 @script RequireComponent(MeshFilter)
 @script RequireComponent(MeshRenderer)
 
 function Start () {
     var meshFilters = GetComponentsInChildren.<MeshFilter>();                        // Get the MeshFilter components of the children
     var combine : CombineInstance[] = new CombineInstance[meshFilters.Length-1];    // Create an array to hold the meshes
     var index = 0;
     for (var i=0;i<meshFilters.Length;i++) {                                        // Create a loop to iterate through the "combine" array
         if (meshFilters[i].sharedMesh == null) continue;
         combine[index].mesh = meshFilters[i].sharedMesh;                            // Assign the mesh of this child to the sharedMesh
         combine[index++].transform = meshFilters[i].transform.localToWorldMatrix;    // Position the mesh
         meshFilters[i].renderer.enabled = false;                                    // Disable the renderer as it's no longer needed
     }
     
     GetComponent(MeshFilter).mesh = new Mesh();
     GetComponent(MeshFilter).mesh.CombineMeshes(combine);                            // Get the combined mesh
     renderer.material = meshFilters[1].renderer.sharedMaterial;                        // Assign the material
 }
Comment
Add comment · Show 1
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 cassius · Jul 01, 2013 at 05:21 PM 0
Share

I'm still struggling with this 2 days later. Does anyone have any ideas on why the combined mesh doesn't work once the game is built, however works perfectly fine in the game editor?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by huhudage · Sep 22, 2013 at 02:41 AM

Hi cassius, have you fixed this problem yet? I have the same problem now...

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 cassius · Sep 22, 2013 at 02:46 AM 0
Share

I'll have a look and get back to you.

avatar image cassius · Sep 22, 2013 at 02:49 AM 0
Share

I'm not sure why but this one below is working for me. Not very different. It may have just ended up being a bug in the version of Unity I was using. I'm now on Unity 4.2.1f

 #pragma strict
 
 @script RequireComponent($$anonymous$$eshFilter)
 @script RequireComponent($$anonymous$$eshRenderer)
 
 function Start () {
     var meshFilters = GetComponentsInChildren.<$$anonymous$$eshFilter>();                        // Get the $$anonymous$$eshFilter components of the children
     var combine : CombineInstance[] = new CombineInstance[meshFilters.Length-1];    // Create an array to hold the meshes
     var index = 0;
     for(var i=0;i<meshFilters.Length;i++) {                                            // Create a loop to iterate through the "combine" array
         if(meshFilters[i].shared$$anonymous$$esh == null) continue;
         combine[index].transform = meshFilters[i].transform.localToWorld$$anonymous$$atrix;        // Position the mesh
         combine[index].mesh = meshFilters[i].shared$$anonymous$$esh;                            // Assign the mesh of this child to the shared$$anonymous$$esh
         index++;
         Destroy(meshFilters[i].renderer);                                            // Remove the renderer as it's no longer needed
         Destroy(meshFilters[i]);                                                    // Remove the mesh filter as it's no longer needed
     }
     
     transform.GetComponent($$anonymous$$eshFilter).mesh.Combine$$anonymous$$eshes(combine);                    // Get the combined mesh
     if(renderer)
         renderer.material = renderer.shared$$anonymous$$aterial; // Assign the material
 }
avatar image cassius · Sep 22, 2013 at 02:51 AM 0
Share

Also, I should point out that every time I've used this the overall triangle count has gone up. See this thread which I started if you want to know about that particular problem.

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

16 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

Related Questions

Combine (skinned) submeshes causes increased vertcount(for each submesh the whole vertcount) 0 Answers

Mesh.CombineMesh() results in different heights between objects 1 Answer

500K upper bound on total number of meshes? 0 Answers

Combing Meshes not working as expected? 1 Answer

GetComponentsInChildren(MeshFilter) not recognizing sharedMesh as variable 2 Answers


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