Search Unity

How To Remove All Colliders From Asset

Discussion in 'Editor & General Support' started by f1b0n4cc1, Oct 27, 2016.

  1. f1b0n4cc1

    f1b0n4cc1

    Joined:
    Aug 27, 2015
    Posts:
    38
    Hello, i need to remove all colliders from all objects and it takes time when do manually

    My main aim to make my mobile game faster. The models i use such as buildings etc will not interact with my car object and they are out of road , as like side objects.

    Anyway , the models i purchased have colliders default even i do not need.

    I am searching a way to remove all colliders from all the objects of my asset purchase. There are more than 400.

    Thank you for any helps
     
  2. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    9,052
    There isn't any way built in to do that, however you can write an editor script to do it in a variety of ways depending on how complex you want to get. Heh, when googling an "Apply prefab" example, I found an old post I made. ;)

    Using that script as a base, I added a check for collider and remove it. So save this script into an Editor folder:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3.  
    4. public class PrefabRemoveCollider : MonoBehaviour
    5. {
    6.     [MenuItem("Tools/Remove Collider And Apply Prefab Changes %#p")]
    7.     static public void removeColliderAndApplyPrefabChanges()
    8.     {
    9.         string log = "";
    10.         var obj = Selection.gameObjects;
    11.         if(obj!=null)
    12.         {
    13.             for (int i = 0; i < obj.Length; i++)
    14.             {
    15.                 bool modified = false;
    16.                 // check to see if selected object is connected to a prefab
    17.                 var prefab_root = PrefabUtility.FindPrefabRoot(obj[i]);
    18.                 var prefab_src = PrefabUtility.GetPrefabParent(prefab_root);
    19.                 if(prefab_src!=null)
    20.                 {
    21.                     log+="<color=white>CHECKING PREFAB:</color> "+obj[i].name+"\n";
    22.  
    23.                     // now check to see if has a collider
    24.                     Collider[] colliders = obj[i].GetComponentsInChildren<Collider>( );
    25.                     foreach( Collider collider in colliders )
    26.                     {
    27.                         log+="\t<color=red>REMOVING COLLIDER:</color> "+collider.name+"\n";
    28.                        
    29.                         // remove the collider
    30.                         DestroyImmediate(collider);
    31.                         modified = true;
    32.                     }
    33.  
    34.                     if(modified)
    35.                     {
    36.                         // apply updated
    37.                         PrefabUtility.ReplacePrefab(prefab_root, prefab_src,  ReplacePrefabOptions.ConnectToPrefab);
    38.                         log+="\t<color=yellow>APPLYING PREFAB:</color> "+AssetDatabase.GetAssetPath(prefab_src)+"\n\n";
    39.                     }
    40.                 }
    41.             }
    42.             Debug.Log(log);
    43.         }
    44.         else
    45.         {
    46.             Debug.Log("Nothing selected");
    47.         }
    48.     }
    49. }
    Then drop the prefabs you want to remove colliders from into the scene. Select them, and run this. It will remove the colliders and save them. This is a pretty basic approach, no undo or anything, so use with caution.
     
    pachermann and Kamil_Reich like this.
  3. f1b0n4cc1

    f1b0n4cc1

    Joined:
    Aug 27, 2015
    Posts:
    38
    thank you @zombiegorilla script work wonderful. Is there a way to apply it to a prefab folder ? if it can search and find every file which ending with .prefab and remove colliders , it can be more useful. Thanks a lot :)
     
  4. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    9,052
    Yea, it is fairly easy to iterate through assets in the project, and manipulate them, even prefabs.

    However, I'm not going to post code that does that. Mainly because it is a destructive process, and I don't want someone coming along, grabbing a piece of posted code, using it or tweaking it, not knowing what they are doing and wreaking thier whole project with a button click.

    I would caution anyone, regardless of experience, from performing bulk operations on project files. (At least without careful planning and VCS).

    Removing colliders is a pretty specific case, something you may only do once, and as you progress in project, may not want on everything. Dropping them on the stage, selecting and running the script is very simple, even with hundreds of prefabs. Plus you easily undo if you grab the wrong one. Doing it on the project, isn't undo able and not fully visible. Technically you could add checks, and confirmations, but it is a lot of work, and wouldn't save time then in the end.

    Since it is rare use, and destructive, it would like having a loaded gun with the hammer cocked sitting in a drawer in the kitchen. Odds are good it would cause more damage than good. ;)
     
  5. _creatio_

    _creatio_

    Joined:
    Mar 21, 2013
    Posts:
    43
    @zombiegorilla thank you sir! Ypur script is still working and with minimal tuning I've made to do what I need. You've saved me a piece of time on writing such a script myself! :)
     
  6. pachermann

    pachermann

    Joined:
    Dec 18, 2013
    Posts:
    133
    This is amazing Thank you very much for sharing!