Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
6
Question by Heisenbug · Sep 03, 2012 at 02:36 PM · c#meshsubmesh

Submesh creation from script

I'm learning to use Unity these days, so just a newbie question.

I've successfully built a mesh from a C# script, following this tutorial.

Basically I created the following class:

     [RequireComponent (typeof (MeshCollider))]
     [RequireComponent (typeof (MeshFilter))]
     [RequireComponent (typeof (MeshRenderer))]
     
     
     public class MyMesh : MonoBehaviour {
      void Start () 
      {
         MeshFilter meshFilter = gameObject.GetComponent<MeshFilter>();
         this.mesh = new Mesh ();
         meshFilter.mesh = this.mesh;
             
         //vertex, triangles and uvs generation....
     
             
         this.mesh.RecalculateNormals();
         this.mesh.RecalculateBounds ();
         this.mesh.Optimize();
      }
 
 }
 

Now, my mesh is becoming very complex and I need to organize it using sub meshes, but I don't know how to do this in Unity using C#.

Basically I need to:

  • create submeshes

  • add submeshes as children of my mesh object

Can anyone put me in the right direction?

thanks

Comment
Add comment · Show 2
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 Heisenbug · Sep 03, 2012 at 03:56 PM 0
Share

@Fattie: thanks for the help. So the submesh index 0 is the first submesh? Or it's the main mesh?

avatar image mexicano · Aug 15, 2021 at 10:13 PM 0
Share

this is what all of you are looking for:

https://answers.unity.com/questions/1436857/small-submesh-example.html

don't forget to add the Mesh.Clear(); at the top and not accidentally erase the Mesh.subMeshCount; like I was doing.

3 Replies

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

Answer by Eric5h5 · Sep 03, 2012 at 04:32 PM

Use Mesh.subMeshCount to set the number of submeshes, then SetTriangles to set the triangle list for each submesh. There isn't a "main" mesh in this case. Then, make a Material array (where each material corresponds to each submesh), and set renderer.materials to that Material array.

You can't have submeshes as children. The point of submeshes is that they are all part of the same mesh. If you want children, just make separate meshes.

Comment
Add comment · Show 6 · 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 Bunny83 · Sep 03, 2012 at 06:39 PM 1
Share

Yes, the only purpose of using submeshes is to be able to use different materials on each submesh. There's no other advantage.

If you have a mesh that is too big for a single mesh, split it into multiple objects. That means a new Gameobject, a new mesh, a new $$anonymous$$eshRenderer / $$anonymous$$eshFilter. This new object can be the child of your first object by setting it's transform.parent to your parent object's transform.

avatar image Heisenbug · Sep 04, 2012 at 12:23 PM 0
Share

Well, yes I have to use different materials on submesh. And hopefully I can use different transformations for each submesh, right?

avatar image Eric5h5 · Sep 04, 2012 at 04:20 PM 0
Share

Definitely not. As I mentioned, if you want the meshes to be separate like that, you should just make them as separate meshes and attach them to child GameObjects. In this case submeshes are not what you want at all.

avatar image Heisenbug · Sep 04, 2012 at 04:38 PM 0
Share

@Eric5h5: thanks for clarify. $$anonymous$$aybe you have a link to some references on how building game objects hierarchies from script?

avatar image Eric5h5 · Sep 04, 2012 at 04:53 PM 0
Share

Use transform.parent.

Show more comments
avatar image
0

Answer by AllOfM · Jan 27, 2016 at 03:17 PM

can somebody give a code example for Submesh and the Materials for each Submesch ?

Comment
Add comment · Show 2 · 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 Cherno · Jan 28, 2016 at 12:04 AM 0
Share

Here: $$anonymous$$aterial for each Submesh in script.

avatar image mexicano · Aug 15, 2021 at 10:14 PM 0
Share

this is what all of you are looking for:

https://answers.unity.com/questions/1436857/small-submesh-example.html

don't forget to add the Mesh.Clear(); at the top and not accidentally erase the Mesh.subMeshCount; like I was doing.

avatar image
0

Answer by fenderrex · Dec 21, 2016 at 02:55 PM

@Heisenbug

And hopefully I can use different transformations for each submesh, right?

you may be able to add a bone like in blender pragmatically if you can't achieveit using SkinnedMeshRenderer.bones https://docs.unity3d.com/ScriptReference/SkinnedMeshRenderer-bones.html look at how blender does it (all in easy to read python) but it looks like unity imports the bone data to the public Transform[] SkinnedMeshRenderer.bones my bet is that one bone is made up of a set of 2 transforms, i don't know what other arrays you might need to populate to connect the vertices if that even how its done... this is how blender would do some mesh setup bone set up... https://wiki.blender.org/index.php/Dev:Py/Scripts/Cookbook/Code_snippets/Three_ways_to_create_objects at worse this could also export it to your server via file transfer or even ftp, python is awesome, if you need a web hook just use yo the social network api just stuff i ran into doing research to see how I would add some features im working on ....

Comment
Add comment · 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

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

14 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

Related Questions

Where is the submesh Reference of vertex/triangle? 1 Answer

Drawing emojis with Unity's Dynamic Font 1 Answer

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

uv sub meshes in cube c# 0 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