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 /
avatar image
1
Question by birns92 · Apr 22, 2016 at 09:20 PM · gameobjectcollidermeshscript.

Assign a Mesh to Mesh Collider Automatically

I am making an Anatomy App and I am in the process of assigning mesh colliders to each bone and muscle so that they are clickable. Is there a way to assign the mesh automatically to The Collider so that I don't have to individually select the correct mesh for each Collider.

alt text

assign-mesh.jpg (174.4 kB)
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 Cherno · Apr 22, 2016 at 10:58 PM 0
Share

Sure... Either write an Editor Extension for an existing script or create a script for an Editor Window. Examples for both solutions are available on the User $$anonymous$$anual and Scripting API. The solution would load all meshes into an array with Resources.LoadAll, and then iterate through the body parts or whatever and assign meshes with the same name.

1 Reply

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

Answer by Bunny83 · Apr 22, 2016 at 11:44 PM

A quick and dirty solution would be a script like this:

 // AddMeshCollider.cs
 using UnityEngine;
 
 public class AddMeshCollider : MonoBehaviour
 {
     void Reset()
     {
         Mesh m = null;
         var mf = GetComponent<MeshFilter>();
         if (mf != null)
             m = mf.sharedMesh;
         var smr = GetComponent<SkinnedMeshRenderer>();
         if (smr != null)
             m = smr.sharedMesh;
         if (m != null)
         {
             var col = GetComponent<MeshCollider>();
             if (col == null)
                 col = gameObject.AddComponent<MeshCollider>();
             col.sharedMesh = m;
         }
     }
 }

This script, when added to a gameobject in the editor, should automatically:

  • Search for MeshFilter / SkinnedMeshRenderer components on the gameobject the script is attached to and if one is found it will copy the mesh reference.

  • ensure that a MeshCollider is attached to the gameobject if there's either a MeshFilter or SkinnedMeshRenderer attached and it will assign the mesh it has found in step 1.

Reset should be called automatically when you add the script to a gameobject. Once added you can also use the context menu of the script component and select "Reset" to run the method once more.

The script has no function at runtime so once you have added it, you could remove it immediately. Keep in mind that you can select multiple objects and drag the script onto the inspector to add that component to all objects at once. Same for removing it.

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 birns92 · Apr 23, 2016 at 12:12 AM 0
Share

Thank you for the answer as well as the explanation, you made it quite easy to understand. Cheers!!

avatar image beowulfkaine · Mar 21, 2017 at 03:21 AM 0
Share

Thank you this saved me a lot of time!

avatar image Jsbbvk · Dec 18, 2017 at 09:09 PM 0
Share

But what if there are A LOT of gameobjects in the scene. You wouldn't want to manually select all the gameobjects. Also, what if you are adding this script to a gameobject that stores children gameobjects that you also wanted to add a collider to? Is there a script that can loop through all possible child gameobjects (as well as the child's children's children's children...etc)?

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

62 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

Related Questions

How do I get rid of intersecting vertices between two gameobjects? 0 Answers

I need help with triggers 1 Answer

3d collider mesh to 2d collider mesh 0 Answers

Raycast starts ignoring gameobject after a few seconds 0 Answers

Prefab around a object 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