/// <summary> /// Removes all attached colliders from the specified game object. /// </summary> public static void RemoveAllColliders(this GameObject gameObject) { // Loop through all colliders and destroy them Collider[] all3DColliders = gameObject.GetComponents <Collider>(); foreach (Collider collider3D in all3DColliders) { // Destory collider #if UNITY_EDITOR RuntimeEditorApplication.DestroyImmediate(collider3D); #else RuntimeEditorApplication.Destroy(collider3D); #endif } Collider2D[] all2DColliders = gameObject.GetComponents <Collider2D>(); foreach (Collider2D collider2D in all2DColliders) { // Destory collider #if UNITY_EDITOR RuntimeEditorApplication.DestroyImmediate(collider2D); #else RuntimeEditorApplication.Destroy(collider2D); #endif } }
/// <summary> /// Attaches a collider to all scene objects using the specified collider /// attachment settings. /// </summary> /// <remarks> /// The method will remove any existing object colliders. Also, the method /// will attach colliders only to mesh, light and particle system objects. /// If an object has 2 or more of these components attached, the colliders /// will be attached in the following manner: /// a) if the object has a mesh attached to it (i.e. a mesh filter with /// a valid mesh or a skinned mesh renderer with a valid mesh), the /// mesh collider attachement settings will be used; /// b) if the object doesn't have a mesh, but it has a light component, /// the light collider attachement settings will be used; /// c) if the object doesn't have a light either, but it has a particle /// system, the particle system collider attachment settings will be used. /// </remarks> public void AttachCollidersToAllSceneObjects(ObjectColliderAttachmentSettings colliderAttachmentSettings) { // Loop through all scene objects and attach colliders GameObject[] sceneObjects = RuntimeEditorApplication.FindObjectsOfType <GameObject>(); foreach (GameObject gameObject in sceneObjects) { AttachColliderToGameObject(gameObject, colliderAttachmentSettings); } }
/// <summary> /// Destroys all children of the specified game object. /// </summary> public static void DestroyAllChildren(this GameObject gameObject) { // Loop through all child transforms Transform objectTransform = gameObject.transform; Transform[] allChildTransforms = gameObject.GetComponentsInChildren <Transform>(); foreach (Transform childTransform in allChildTransforms) { // Same as parent object? if (objectTransform == childTransform) { continue; } // Destroy object #if UNITY_EDITOR RuntimeEditorApplication.DestroyImmediate(childTransform.gameObject); #else RuntimeEditorApplication.Destroy(childTransform.gameObject); #endif } }
/// <summary> /// Called when the editor application object is selected in the scene view. /// </summary> protected virtual void OnEnable() { _editorApplication = target as RuntimeEditorApplication; }