For my game, the setting is a huge city with over 6000 buildings, and thus over 6000 meshes. It would be close to impossible for me to got into each one and add a collider, so is there a way to add a collider to the city prefab? I was thinking that a script could do it, but Googling has not turned up any results.
When you add a collider to an object, Unity tries to fit the collider to the object’s renderer (works with Box, Mesh, and Sphere at least). So you could just have an editor script that’s something like this:
foreach(GameObject obj in objects) {
obj.AddComponent<WhateverCollider>();
}
A scriptablewizard might be your best bet:
public class BatchColliders : ScriptableWizard {
public enum ColliderType { Box, Mesh }
public ColliderType typeOfCollider;
public GameObject[] listOfObjects;
[MenuItem("Custom/Batch Colliders")]
public static void CreateWizard() {
ScriptableWizard.CreateWizard<BatchColliders>("Batch Add Colliders", "Add");
}
void OnWizardCreate() { // called when you click "Add", I think
foreach(GameObject obj in listOfObjects ) {
if ( obj.GetComponent<Collider>() ) continue;
if ( typeOfCollider == ColliderType.Box ) obj.AddComponent<BoxCollider>();
if ( typeOfCollider == ColliderType.Mesh ) obj.AddComponent<MeshCollider>();
}
}
}