public override void OnInspectorGUI()
        {
            ShatterTool source = (ShatterTool)target;

            EditorGUILayout.BeginVertical();

            // Generation
            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.LabelField("Generation", source.Generation.ToString());
            EditorGUILayout.EndHorizontal();

            // GenerationLimit
            EditorGUILayout.BeginHorizontal();
            source.GenerationLimit = EditorGUILayout.IntSlider(new GUIContent("Generation Limit", generationLimitTooltip), source.GenerationLimit, 1, 30);
            EditorGUILayout.EndHorizontal();

            // Cuts
            EditorGUILayout.BeginHorizontal();
            source.Cuts = EditorGUILayout.IntSlider(new GUIContent("Cuts", cutsTooltip), source.Cuts, 1, 25);
            EditorGUILayout.EndHorizontal();

            // FillCut
            EditorGUILayout.BeginHorizontal();
            source.FillCut = EditorGUILayout.Toggle(new GUIContent("Fill Cut", fillCutTooltip), source.FillCut);
            EditorGUILayout.EndHorizontal();

            // SendPreSplitMessage
            EditorGUILayout.BeginHorizontal();
            source.SendPreSplitMessage = EditorGUILayout.Toggle(new GUIContent("Pre Split msg", preSplitMsgTooltip), source.SendPreSplitMessage);
            EditorGUILayout.EndHorizontal();

            // SendPostSplitMessage
            EditorGUILayout.BeginHorizontal();
            source.SendPostSplitMessage = EditorGUILayout.Toggle(new GUIContent("Post Split msg", postSplitMsgTooltip), source.SendPostSplitMessage);
            EditorGUILayout.EndHorizontal();

            // InternalHullType
            EditorGUILayout.BeginHorizontal();
            source.InternalHullType = (HullType)EditorGUILayout.EnumPopup(new GUIContent("Internal Hull Type", internalHullTypeTooltip), source.InternalHullType);
            EditorGUILayout.EndHorizontal();

            EditorGUILayout.EndVertical();

            // Handle change
            if (GUI.changed)
            {
                EditorUtility.SetDirty(target);
            }
        }
Example #2
0
        protected void CreateNewGameObjects(IList <IHull> newHulls, out GameObject[] newGameObjects)
        {
            // Get new meshes
            Mesh[]  newMeshes  = new Mesh[newHulls.Count];
            float[] newVolumes = new float[newHulls.Count];

            float totalVolume = 0.0f;

            for (int i = 0; i < newHulls.Count; i++)
            {
                Mesh    mesh   = newHulls[i].GetMesh();
                Vector3 size   = mesh.bounds.size;
                float   volume = size.x * size.y * size.z;

                newMeshes[i]  = mesh;
                newVolumes[i] = volume;

                totalVolume += volume;
            }

            MeshFilter   meshFilter   = GetComponent <MeshFilter>();
            MeshCollider meshCollider = GetComponent <MeshCollider>();
            Rigidbody    rigidbody    = GetComponent <Rigidbody>();

            // Remove mesh references to speed up instantiation
            //meshFilter.sharedMesh = null;

            //if (meshCollider != null)
            //{
            //    meshCollider.sharedMesh = null;
            //}

            // Create new game objects
            newGameObjects = new GameObject[newHulls.Count];

            for (int i = 0; i < newHulls.Count; i++)
            {
                IHull newHull = newHulls[i];
                Mesh  newMesh = newMeshes[i];
                float volume  = newVolumes[i];

                GameObject newGameObject = (GameObject)Instantiate(gameObject);
                newGameObject.tag = "cubeClone";
                /* Set position */
                newGameObject.transform.position = gameObject.transform.position;
                // Set shatter tool
                ShatterTool newShatterTool = newGameObject.GetComponent <ShatterTool>();

                if (newShatterTool != null)
                {
                    newShatterTool.hull = newHull;
                }

                // Set mesh filter
                MeshFilter newMeshFilter = newGameObject.GetComponent <MeshFilter>();

                if (newMeshFilter != null)
                {
                    newMeshFilter.sharedMesh = newMesh;
                }

                // Set mesh collider
                MeshCollider newMeshCollider = newGameObject.GetComponent <MeshCollider>();

                if (newMeshCollider != null)
                {
                    newMeshCollider.sharedMesh = newMesh;
                }
                // isTrigger
                //newMeshCollider.isTrigger = true;
                // Set rigidbody
                Rigidbody newRigidbody = newGameObject.GetComponent <Rigidbody>();

                if (rigidbody != null && newRigidbody != null)
                {
                    newRigidbody.mass = .1f;

                    newRigidbody.constraints = RigidbodyConstraints.None;
                    //newRigidbody.constraints = RigidbodyConstraints.FreezePositionY;
                    if (!gameObject.GetComponent <Bullet>())
                    {
                        float randomX = Random.Range(-10, 10f);
                        newRigidbody.AddForce(new Vector3(randomX, 20f, 0f));
                    }
                    else
                    {
                    }
                    //if (!newRigidbody.isKinematic)
                    //{
                    //    newRigidbody.velocity = rigidbody.GetPointVelocity(newRigidbody.worldCenterOfMass);

                    //    newRigidbody.angularVelocity = rigidbody.angularVelocity;
                    //}
                }

                //newGameObject.tag = "DestroyedSpike";

                // Update properties
                newShatterTool.CalculateCenter();

                newGameObjects[i] = newGameObject;
            }
        }
        protected void CreateNewGameObjects(IList <IHull> newHulls, out GameObject[] newGameObjects)
        {
            // Get new meshes
            Mesh[]  newMeshes  = new Mesh[newHulls.Count];
            float[] newVolumes = new float[newHulls.Count];

            float totalVolume = 0.0f;

            for (int i = 0; i < newHulls.Count; i++)
            {
                Mesh    mesh   = newHulls[i].GetMesh();
                Vector3 size   = mesh.bounds.size;
                float   volume = size.x * size.y * size.z;

                newMeshes[i]  = mesh;
                newVolumes[i] = volume;

                totalVolume += volume;
            }

            MeshFilter   meshFilter   = GetComponent <MeshFilter>();
            MeshCollider meshCollider = GetComponent <MeshCollider>();
            Rigidbody    rigidbody    = GetComponent <Rigidbody>();

            // Remove mesh references to speed up instantiation
            meshFilter.sharedMesh = null;

            if (meshCollider != null)
            {
                meshCollider.sharedMesh = null;
            }

            // Create new game objects
            newGameObjects = new GameObject[newHulls.Count];

            for (int i = 0; i < newHulls.Count; i++)
            {
                IHull newHull = newHulls[i];
                Mesh  newMesh = newMeshes[i];
                float volume  = newVolumes[i];

                GameObject newGameObject = (GameObject)Instantiate(gameObject);

                // Set shatter tool
                ShatterTool newShatterTool = newGameObject.GetComponent <ShatterTool>();

                if (newShatterTool != null)
                {
                    newShatterTool.hull = newHull;
                }

                // Set mesh filter
                MeshFilter newMeshFilter = newGameObject.GetComponent <MeshFilter>();

                if (newMeshFilter != null)
                {
                    newMeshFilter.sharedMesh = newMesh;
                }

                // Set mesh collider
                MeshCollider newMeshCollider = newGameObject.GetComponent <MeshCollider>();

                if (newMeshCollider != null)
                {
                    newMeshCollider.sharedMesh = newMesh;
                }

                // Set rigidbody
                Rigidbody newRigidbody = newGameObject.GetComponent <Rigidbody>();

                if (rigidbody != null && newRigidbody != null)
                {
                    newRigidbody.mass = rigidbody.mass * (volume / totalVolume);

                    if (!newRigidbody.isKinematic)
                    {
                        newRigidbody.velocity = rigidbody.GetPointVelocity(newRigidbody.worldCenterOfMass);

                        newRigidbody.angularVelocity = rigidbody.angularVelocity;
                    }
                }

                // Update properties
                newShatterTool.CalculateCenter();

                newGameObjects[i] = newGameObject;
            }
        }