Example #1
0
        void Use_StoredValues()
        {
            Static_Track_Parent_CS staticTrackParentScript = thisTransform.parent.GetComponentInChildren <Static_Track_Parent_CS> ();

            if (staticTrackParentScript)
            {
                if (staticTrackParentScript.RoadWheelsProp_Array.Length == 0)
                {
                    Debug.LogWarning("The values for Static_Track are not stored. Please set 'Angle' and 'SphereCollider Radius' manually.");
                    return;
                }
                for (int i = 0; i < staticTrackParentScript.RoadWheelsProp_Array.Length; i++)
                {
                    if (staticTrackParentScript.RoadWheelsProp_Array [i].parentName == thisTransform.name)
                    {
                        // Set the radius.
                        Wheel_RadiusProp.floatValue = staticTrackParentScript.RoadWheelsProp_Array [i].baseRadius;

                        // Set the angles.
                        Set_IndividuallyProp.boolValue = true;
                        Sus_AnglesProp.arraySize       = NumProp.intValue;
                        for (int j = 0; j < NumProp.intValue; j++)
                        {
                            Sus_AnglesProp.GetArrayElementAtIndex(j).floatValue = staticTrackParentScript.RoadWheelsProp_Array [i].angles [j];
                        }
                        break;
                    }
                }
            }
            else
            {
                Debug.LogWarning("Static_Track cannot be found in this tank.");
                return;
            }
        }
        public void Prepare_With_Static_Track(Static_Track_Parent_CS tempStaticTrackScript)
        { // Called from "Static_Track_Parent_CS".
            staticTrackScript = tempStaticTrackScript;

            // Set the rotation rate.
            leftStaticTrackRate  = staticTrackScript.Reference_Radius_L / Wheel_Radius;
            rightStaticTrackRate = staticTrackScript.Reference_Radius_R / Wheel_Radius;
        }
        void Set_Parent_Script(Static_Track_Parent_CS parentScript)
        {
            // Set the values.
            parentScript.Length           = Collider_InfoProp.boundsValue.size.z;
            parentScript.Stored_Body_Mass = thisTransform.GetComponentInParent <Rigidbody>().mass + (Track_MassProp.floatValue * thisTransform.childCount);             // To increase the MainBody's mass later.

            // Store the roadwheels information.
            Create_RoadWheel_CS[] createRoadWheelScripts = thisTransform.parent.GetComponentsInChildren <Create_RoadWheel_CS>();
            RoadWheelsProp[]      roadWheelsPropArray    = new RoadWheelsProp [createRoadWheelScripts.Length];
            for (int i = 0; i < createRoadWheelScripts.Length; i++)
            {
                // Call "Create_RoadWheel_CS" to get the roadwheels information, such as the wheel radius and the arm angle.
                roadWheelsPropArray [i]            = createRoadWheelScripts [i].Get_Current_Angles();
                roadWheelsPropArray[i].baseRadius += Collider_InfoProp.boundsValue.size.y;                 // Add the hight of the piece.
            }
            parentScript.RoadWheelsProp_Array = roadWheelsPropArray;
        }
        void Create_Prefab()
        {
            EditorApplication.isPaused = true;

            // Create the new parent object.
            GameObject newParentObject = new GameObject("Static_Track");

            newParentObject.transform.parent        = thisTransform.parent;
            newParentObject.transform.localPosition = thisTransform.localPosition;
            newParentObject.transform.localRotation = thisTransform.localRotation;
            Static_Track_Parent_CS parentScript = newParentObject.AddComponent <Static_Track_Parent_CS>();

            Set_Parent_Script(parentScript);

            // Call all the "Static_Track_Setting_CS" in the pieces.
            thisTransform.BroadcastMessage("Set_Child_Scripts", parentScript, SendMessageOptions.DontRequireReceiver);
            thisTransform.BroadcastMessage("Set_Front_And_Rear_Scripts", parentScript, SendMessageOptions.DontRequireReceiver);
            thisTransform.BroadcastMessage("Remove_Setting_Script", SendMessageOptions.DontRequireReceiver);

            // Change the hierarchy.
            Static_Track_Piece_CS[] piecesScripts = thisTransform.GetComponentsInChildren <Static_Track_Piece_CS>();
            for (int i = 0; i < piecesScripts.Length; i++)
            {
                piecesScripts[i].transform.parent = newParentObject.transform;
            }

            // Create Prefab.
            if (Directory.Exists("Assets/Physics Tank Maker/User/") == false)
            {
                AssetDatabase.CreateFolder("Assets/Physics Tank Maker", "User");
            }
            PrefabUtility.SaveAsPrefabAsset(newParentObject, "Assets/Physics Tank Maker/User/" + "Static_Track " + DateTime.Now.ToString("yyMMdd_HHmmss") + ".prefab");

            // Message.
            Debug.Log("New 'Static_Track' has been created in 'User' folder.");

            // Return to EditMode.
            EditorApplication.isPlaying = false;
        }
        void Set_Child_Scripts(Static_Track_Parent_CS parentScript)
        { // Called from "Create_TrackBelt_CSEditor".
            // Add "Static_Track_Piece_CS" and set the values.
            var pieceScript = gameObject.AddComponent <Static_Track_Piece_CS>();

            // Set the "This_Transform".
            pieceScript.This_Transform = thisTransform;

            // Set the "Parent_Script".
            pieceScript.Parent_Script = parentScript;

            // Set the "Is_Left".
            pieceScript.Is_Left = (thisTransform.localPosition.y > 0.0f);

            // Set the "Invert_Angle".
            var localAngleY = thisTransform.localEulerAngles.y;

            if (localAngleY > 90.0f && localAngleY < 270.0f)
            { // Upper piece.
                pieceScript.Invert_Angle = 180.0f;
            }
            else
            { // Lower piece
                pieceScript.Invert_Angle = 0.0f;
            }

            // Set the "Half_Length".
            pieceScript.Half_Length = pieceScript.Parent_Script.Length * 0.5f;

            // Set the "Pieces_Count".
            pieceScript.Pieces_Count = thisTransform.parent.childCount / 2;

            // Find the front and rear pieces using this name.
            var baseName = this.name.Substring(0, 12);         // e.g. "TrackBelt_L_"
            var thisNum  = int.Parse(this.name.Substring(12)); // e.g. "1"

            // Find the front piece.
            Transform frontTransform = thisTransform.parent.Find(baseName + (thisNum + 1)); // Find a piece having next number.

            if (frontTransform == null)
            {                                                             // It must be the last piece.
                frontTransform = thisTransform.parent.Find(baseName + 1); // The 1st piece.
            }
            pieceScript.Front_Transform = frontTransform;

            // Find the rear piece.
            Transform rearTransform = thisTransform.parent.Find(baseName + (thisNum - 1)); // Find a piece having previous number.

            if (rearTransform == null)
            {                                                                                                // It must be the 1st piece.
                rearTransform = thisTransform.parent.Find(baseName + (thisTransform.parent.childCount / 2)); // The last piece.
            }
            pieceScript.Rear_Transform = rearTransform;

            // Set the "Type".
            switch (type)
            {
            case 0:   // Static
                if (frontTransform.GetComponent <Static_Track_Setting_CS>().type == 1)
                {     // The front piece is Anchor type.
                    // Change to Dynamic type.
                    pieceScript.Type = 2;
                }
                else if (rearTransform.GetComponent <Static_Track_Setting_CS>().type == 1)
                {     // The rear piece is Anchor type.
                    // Change to Dynamic type.
                    pieceScript.Type = 2;
                }
                else
                {
                    pieceScript.Type = 0;     // Static.
                }
                break;

            case 1:     // Anchor
                pieceScript.Type               = 1;
                pieceScript.Anchor_Name        = anchorTransform.name;
                pieceScript.Anchor_Parent_Name = anchorTransform.parent.name;
                break;

            case 2:     // Dynamic
                pieceScript.Type = 2;
                break;
            }


            // Add "Static_Track_Switch_Mesh_CS".
            if (Use_2ndPiece)
            {
                var switchScript = gameObject.AddComponent <Static_Track_Switch_Mesh_CS>();
                switchScript.Piece_Script    = pieceScript;
                switchScript.Parent_Script   = parentScript;
                switchScript.This_MeshFilter = switchScript.GetComponent <MeshFilter>();
                switchScript.Mesh_A          = switchScript.This_MeshFilter.sharedMesh;
                switchScript.Mesh_B          = switchScript.Piece_Script.Front_Transform.GetComponent <MeshFilter>().sharedMesh;
                switchScript.Is_Left         = switchScript.Piece_Script.Is_Left;
            }


            // Remove the useless components.
            var hingeJoint = GetComponent <HingeJoint>();

            if (hingeJoint)
            {
                DestroyImmediate(hingeJoint);
            }
            var rigidbody = GetComponent <Rigidbody>();

            if (rigidbody)
            {
                DestroyImmediate(rigidbody);
            }
            var damageScript = GetComponent <Damage_Control_03_Physics_Track_Piece_CS>();

            if (damageScript)
            {
                DestroyImmediate(damageScript);
            }
            var stabilizerScript = GetComponent <Stabilizer_CS>();

            if (stabilizerScript)
            {
                DestroyImmediate(stabilizerScript);
            }


            // Disable the Colliders.
            var colliders = GetComponents <Collider>();

            for (int i = 0; i < colliders.Length; i++)
            {
                colliders[i].enabled = false;
            }


            // Remove the child objects such as Reinforce piece.
            for (int i = 0; i < transform.childCount; i++)
            {
                var childObject = transform.GetChild(i).gameObject;
                if (childObject.layer == Layer_Settings_CS.Reinforce_Layer)
                { // Reinforce.
                    DestroyImmediate(childObject);
                }
            }
        }