public override void OnInspectorGUI()
        {
            var boldFoldout = new GUIStyle(EditorStyles.foldout);

            boldFoldout.fontStyle = FontStyle.Bold;
            var targetScript = (Suspension.Suspension) this.target;
            var allTargets   = new Suspension.Suspension[this.targets.Length];

            this.isPrefab = PrefabUtility.GetPrefabType(targetScript) == PrefabType.Prefab;

            for (var i = 0; i < this.targets.Length; i++)
            {
                Undo.RecordObject(this.targets[i], "Suspension Change");
                allTargets[i] = this.targets[i] as Suspension.Suspension;
            }

            if (!targetScript.wheel)
            {
                EditorGUILayout.HelpBox("Wheel must be assigned.", MessageType.Error);
            }

            this.DrawDefaultInspector();

            if (!this.isPrefab && targetScript.gameObject.activeInHierarchy)
            {
                showButtons = EditorGUILayout.Foldout(showButtons, "Quick Actions", boldFoldout);
                EditorGUI.indentLevel++;
                if (showButtons)
                {
                    if (GUILayout.Button("Get Wheel"))
                    {
                        foreach (var curTarget in allTargets)
                        {
                            curTarget.wheel = curTarget.transform.GetComponentInChildren <Wheel>();
                        }
                    }

                    if (GUILayout.Button("Get Opposite Wheel"))
                    {
                        foreach (var curTarget in allTargets)
                        {
                            var vp = (VehicleParent)F.GetTopmostParentComponent <VehicleParent>(curTarget.transform);
                            Suspension.Suspension closestOne = null;
                            var closeDist = Mathf.Infinity;

                            foreach (var curWheel in vp.wheels)
                            {
                                var curDist =
                                    Vector2.Distance(new Vector2(curTarget.transform.localPosition.y,
                                                                 curTarget.transform.localPosition.z),
                                                     new Vector2(curWheel.transform.parent.localPosition.y,
                                                                 curWheel.transform.parent.localPosition.z));
                                if (Mathf.Sign(curTarget.transform.localPosition.x)
                                    != Mathf.Sign(curWheel.transform.parent.localPosition.x) &&
                                    curDist < closeDist)
                                {
                                    closeDist  = curDist;
                                    closestOne = curWheel.transform.parent.GetComponent <Suspension.Suspension>();
                                }
                            }

                            curTarget.oppositeWheel = closestOne;
                        }
                    }
                }

                EditorGUI.indentLevel--;
            }

            if (GUI.changed)
            {
                EditorUtility.SetDirty(targetScript);
            }
        }
Exemple #2
0
        void Start()
        {
            this.tr = this.transform;
            this.rb = (Rigidbody)F.GetTopmostParentComponent <Rigidbody>(this.tr);
            this.vp = (VehicleParent)F.GetTopmostParentComponent <VehicleParent>(this.tr);
            this.suspensionParent    = this.tr.parent.GetComponent <Suspension.Suspension>();
            this.travelDist          = this.suspensionParent.targetCompression;
            this.canDetach           = this.detachForce < Mathf.Infinity && Application.isPlaying;
            this.initialTirePressure = this.tirePressure;

            if (this.tr.childCount > 0)
            {
                //Get rim
                this.rim = this.tr.GetChild(0);

                //Set up rim glow material
                if (this.rimGlow > 0 && Application.isPlaying)
                {
                    this.rimMat = new Material(this.rim.GetComponent <MeshRenderer>().sharedMaterial);
                    this.rimMat.EnableKeyword("_EMISSION");
                    this.rim.GetComponent <MeshRenderer>().material = this.rimMat;
                }

                //Create detached wheel
                if (this.canDetach)
                {
                    this.detachedWheel           = new GameObject(this.vp.transform.name + "'s Detached Wheel");
                    this.detachedWheel.layer     = LayerMask.NameToLayer("Detachable Part");
                    this.detachFilter            = this.detachedWheel.AddComponent <MeshFilter>();
                    this.detachFilter.sharedMesh = this.rim.GetComponent <MeshFilter>().sharedMesh;
                    var detachRend = this.detachedWheel.AddComponent <MeshRenderer>();
                    detachRend.sharedMaterial = this.rim.GetComponent <MeshRenderer>().sharedMaterial;
                    this.detachedCol          = this.detachedWheel.AddComponent <MeshCollider>();
                    this.detachedCol.convex   = true;
                    this.detachedBody         = this.detachedWheel.AddComponent <Rigidbody>();
                    this.detachedBody.mass    = this.mass;
                }

                //Get tire
                if (this.rim.childCount > 0)
                {
                    this.tire = this.rim.GetChild(0);
                    if (this.deformAmount > 0 && Application.isPlaying)
                    {
                        this.tireMat = new Material(this.tire.GetComponent <MeshRenderer>().sharedMaterial);
                        this.tire.GetComponent <MeshRenderer>().material = this.tireMat;
                    }

                    //Create detached tire
                    if (this.canDetach)
                    {
                        this.detachedTire = new GameObject("Detached Tire");
                        this.detachedTire.transform.parent        = this.detachedWheel.transform;
                        this.detachedTire.transform.localPosition = Vector3.zero;
                        this.detachedTire.transform.localRotation = Quaternion.identity;
                        this.detachTireFilter            = this.detachedTire.AddComponent <MeshFilter>();
                        this.detachTireFilter.sharedMesh = this.tire.GetComponent <MeshFilter>().sharedMesh;
                        var detachTireRend = this.detachedTire.AddComponent <MeshRenderer>();
                        detachTireRend.sharedMaterial = this.tireMat
                                                ? this.tireMat
                                                : this.tire.GetComponent <MeshRenderer>().sharedMaterial;
                    }
                }

                if (Application.isPlaying)
                {
                    //Generate hard collider
                    if (this.generateHardCollider)
                    {
                        var sphereColNew = new GameObject("Rim Collider");
                        sphereColNew.layer             = GlobalControl.ignoreWheelCastLayer;
                        this.sphereColTr               = sphereColNew.transform;
                        this.sphereCol                 = sphereColNew.AddComponent <SphereCollider>();
                        this.sphereColTr.parent        = this.tr;
                        this.sphereColTr.localPosition = Vector3.zero;
                        this.sphereColTr.localRotation = Quaternion.identity;
                        this.sphereCol.radius          = Mathf.Min(this.rimWidth * 0.5f, this.rimRadius * 0.5f);
                        this.sphereCol.material        = GlobalControl.frictionlessMatStatic;
                    }

                    if (this.canDetach)
                    {
                        this.detachedWheel.SetActive(false);
                    }
                }
            }

            this.targetDrive = this.GetComponent <DriveForce>();
            this.currentRPM  = 0;
        }