Posts Tagged meshcollider

Unity automatic MeshCollider generation


When ever I get a model that has many sub elements, but no standard mesh colliders I use the wizard listed here to automatically generate them.  Thought it might come in useful to others…


using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Linq;
public class AddColliders : ScriptableWizard {

 [MenuItem("Wizards/Add Mesh Colliders")]
 static void CreateWizard()
 {
    ScriptableWizard.DisplayWizard<AddColliders>("Add mesh colliders", "Add Colliders");
 }

 void OnWizardCreate()
 {
    if(UnityEditor.Selection.activeGameObject != null)
    {
       foreach(var c in UnityEditor.Selection.activeGameObject.GetComponentsInChildren<MeshRenderer>().Cast<MeshRenderer>().
       Where(mr=>mr.GetComponent<MeshCollider>()==null))
       {
           c.gameObject.AddComponent(typeof(MeshCollider));
       }
    }
 }
 void OnWizardUpdate()
 {
    helpString = "Add mesh colliders to all items that have a mesh renderer";
 }

}

, , ,

Leave a comment