Esempio n. 1
0
        private void CreateCompoundCollider()
        {
            var selectedGameObject = Selection.activeGameObject;

            // Abort create if the selected game object is null
            if (selectedGameObject == null)
            {
                return;
            }

            GameObject compoundCollider = null;

            if (compoundShape.Equals(CompoundShape.Ring))
            {
                // Match height to width if enabled
                float ringHeight = matchHeightToWidth ? (outerRadius - innerRadius) : height;

                compoundCollider =
                    CompoundCollider.CreateRing(pivotAxis, segments, outerRadius, innerRadius, colliderShape, ringHeight, rotationOffset, hasEndCap);
            }
            else if (compoundShape.Equals(CompoundShape.Box))
            {
                compoundCollider = CompoundCollider.CreateBox(length, width, height, wallThickness);
            }
            else if (compoundShape.Equals(CompoundShape.Bowl))
            {
                compoundCollider = CompoundCollider.CreateBowl(outerRadius, segments, wallThickness, range);
            }

            // Move the collider so its position is relative to the selected game object
            compoundCollider.transform.position += selectedGameObject.transform.position;

            // If option is enabled, move collider so its position is relative to the select game object's mesh bounds center
            if (selectedHasMesh && meshBoundsAsCenter)
            {
                // NOTE: Mesh bounds center is local space
                compoundCollider.transform.position += selectedGameObject.GetComponent <MeshFilter>().sharedMesh.bounds.center;
            }

            // Temporarily set the selected game object's rotation to zero to avoid colliders being warped when collider's parent is set
            var originalRotation = selectedGameObject.transform.rotation;

            selectedGameObject.transform.rotation = Quaternion.identity;

            // Set the collider's parent to the selected game object
            compoundCollider.transform.SetParent(selectedGameObject.transform);

            // Restore the select game object's original rotation
            selectedGameObject.transform.rotation = originalRotation;

            if (renderersDisabled)
            {
                // Disable mesh renderers
                var meshRenderers = compoundCollider.GetComponentsInChildren <MeshRenderer>();
                foreach (var meshRenderer in meshRenderers)
                {
                    meshRenderer.enabled = false;
                }
            }

            // Set created collider to working collider
            workingCollider = compoundCollider;

            // Subscribe to undo events so we know when to clear reference to working collider
            Undo.undoRedoPerformed += OnUndoRedo;
        }