/// <summary>
 ///
 /// </summary>
 /// <param name="carScript"></param>
 /// <param name="sfxs"></param>
 private static void ConfigureConnectionSFX(Wagon_v3 carScript, List <GameObject> sfxs)
 {
     if (sfxs != null && sfxs.Count > 0)
     {
         foreach (var item in sfxs)
         {
             AudioSource connectionSFX = item.GetComponent <AudioSource>();
             if (connectionSFX != null)
             {
                 carScript.wagonConnectionSFX = connectionSFX;
             }
         }
     }
 }
 /// <summary>
 ///
 /// </summary>
 /// <param name="wagonScript"></param>
 /// <param name="joints"></param>
 private static void ConfigureDefaultJointAnchor(Wagon_v3 wagonScript, List <GameObject> joints)
 {
     if (joints != null && joints.Count > 0)
     {
         foreach (var item in joints)
         {
             Rigidbody rigid = item.GetComponent <Rigidbody>();
             if (rigid != null)
             {
                 wagonScript.jointAnchor = rigid;
             }
         }
     }
 }
    /// <summary>
    ///
    /// </summary>
    /// <param name="parent"></param>
    /// <param name="wagonScript"></param>
    /// <param name="couplers"></param>
    private static void ConfigureFrontCoupler(Rigidbody parent, Wagon_v3 wagonScript, HingeJoint parentJoint, List <GameObject> couplers)
    {
        ConnectHingeAnchor(parent, couplers);

        if (couplers != null && couplers.Count > 0)
        {
            foreach (var item in couplers)
            {
                Rigidbody rigid = item.GetComponent <Rigidbody>();
                if (rigid != null)
                {
                    wagonScript.frontJoint = rigid;
                }
                parentJoint.anchor = item.transform.position;
            }
        }
    }
    public override void OnInspectorGUI()
    {
        //base.OnInspectorGUI();
        _wagon = target as Wagon_v3;

        EditorGUI.BeginChangeCheck();
        _selectedMenuIndex = GUILayout.Toolbar(_selectedMenuIndex, _toolbarMenuOptions);
        if (EditorGUI.EndChangeCheck())
        {
            GUI.FocusControl(null);
        }

        //Set up the box style if null
        if (_menuBoxStyle == null)
        {
            _menuBoxStyle = new GUIStyle(GUI.skin.box);
            _menuBoxStyle.normal.textColor = GUI.skin.label.normal.textColor;
            _menuBoxStyle.fontStyle        = FontStyle.Bold;
            _menuBoxStyle.alignment        = TextAnchor.UpperLeft;
        }
        GUILayout.BeginVertical(_menuBoxStyle);

        if (_selectedMenuIndex == (int)WagonInspectorMenu.Coupling)
        {
            #region Coupling

            GUILayout.Label("COUPLING SETTINGS", EditorStyles.boldLabel);

            EditorGUI.BeginChangeCheck();
            serializedObject.Update();
            EditorGUILayout.PropertyField(serializedObject.FindProperty("coupling"), true);
            EditorGUILayout.PropertyField(serializedObject.FindProperty("decouplingSettings"), true);
            EditorGUILayout.PropertyField(serializedObject.FindProperty("recouplingTimeout"), true);
            if (EditorGUI.EndChangeCheck())
            {
                Undo.RecordObject(_wagon, "Changed Coupling Settings");
                serializedObject.ApplyModifiedProperties();
                MarkSceneAlteration();
            }
            #endregion
        }
        else if (_selectedMenuIndex == (int)WagonInspectorMenu.Lights)
        {
            #region Lights

            GUILayout.Label("WAGON LIGHTS", EditorStyles.boldLabel);

            EditorGUI.BeginChangeCheck();
            serializedObject.Update();
            EditorGUILayout.PropertyField(serializedObject.FindProperty("externalLights"), true);
            EditorGUILayout.PropertyField(serializedObject.FindProperty("internalLights"), true);
            if (EditorGUI.EndChangeCheck())
            {
                Undo.RecordObject(_wagon, "Changed Lights");
                serializedObject.ApplyModifiedProperties();
                MarkSceneAlteration();
            }
            #endregion
        }
        else if (_selectedMenuIndex == (int)WagonInspectorMenu.SFX)
        {
            #region SFX

            GUILayout.Label("WAGON SFXS", EditorStyles.boldLabel);

            EditorGUI.BeginChangeCheck();
            serializedObject.Update();
            EditorGUILayout.PropertyField(serializedObject.FindProperty("wheelsSFX"), true);
            EditorGUILayout.PropertyField(serializedObject.FindProperty("wagonConnectionSFX"), true);

            if (EditorGUI.EndChangeCheck())
            {
                Undo.RecordObject(_wagon, "Changed SFX");
                serializedObject.ApplyModifiedProperties();
                MarkSceneAlteration();
            }
            #endregion
        }
        else if (_selectedMenuIndex == (int)WagonInspectorMenu.OtherSettings)
        {
            #region Other Settings

            GUILayout.Label("MECHANICAL COMPONENTS", EditorStyles.boldLabel);

            EditorGUI.BeginChangeCheck();
            serializedObject.Update();
            EditorGUILayout.PropertyField(serializedObject.FindProperty("wheelsScripts"), true);
            EditorGUILayout.PropertyField(serializedObject.FindProperty("sensors"), true);
            EditorGUILayout.PropertyField(serializedObject.FindProperty("backJoint"), true);
            EditorGUILayout.PropertyField(serializedObject.FindProperty("frontJoint"), true);
            EditorGUILayout.PropertyField(serializedObject.FindProperty("jointAnchor"), true);

            if (EditorGUI.EndChangeCheck())
            {
                Undo.RecordObject(_wagon, "Changed Other Settings");
                serializedObject.ApplyModifiedProperties();
                MarkSceneAlteration();
            }
            #endregion
        }

        GUILayout.EndVertical();
    }
    /// <summary>
    ///
    /// </summary>
    /// <param name="profile"></param>
    /// <param name="modelPrefab"></param>
    /// <param name="offset"></param>
    /// <param name="name"></param>
    public static bool Create(CustomWagonProfile profile, GameObject modelPrefab, string name, out string message)
    {
        message = string.Empty;

        Vector3 railsOffset = new Vector3(0f, 0.14f, 0f);

        //Wagon/locomotive game object
        GameObject wagonInstance = new GameObject(name);
        //Model
        GameObject modelInstance;

        if (modelPrefab != null)
        {
            modelInstance = InstantiateChild(modelPrefab, modelPrefab.name, profile.modelOffset, Quaternion.identity, wagonInstance.transform);
        }
        //Default child instances
        GameObject wheels     = InstantiateChild("Wheels", railsOffset, Quaternion.identity, wagonInstance.transform);
        GameObject sfx        = InstantiateChild("SFX", Vector3.zero, Quaternion.identity, wagonInstance.transform);
        GameObject sensors    = InstantiateChild("Sensors", railsOffset, Quaternion.identity, wagonInstance.transform);
        GameObject colliders  = InstantiateChild("Colliders", Vector3.zero, Quaternion.identity, wagonInstance.transform);
        GameObject bumpers    = InstantiateChild("Bumpers", Vector3.zero, Quaternion.identity, wagonInstance.transform);
        GameObject doors      = InstantiateChild("Doors", Vector3.zero, Quaternion.identity, wagonInstance.transform);
        GameObject lights     = InstantiateChild("Lights", Vector3.zero, Quaternion.identity, wagonInstance.transform);
        GameObject suspension = InstantiateChild("Suspension", railsOffset, Quaternion.identity, wheels.transform);

        //Add common unity components
        Rigidbody rigidbody = wagonInstance.AddComponent <Rigidbody>();

        //Configure common unity components
        rigidbody.mass = 10000f;

        //Instantiate common profile components
        InstantiateWagonComponents(profile.bumper, bumpers.transform, null);
        InstantiateWagonComponents(profile.suspensionCollider, suspension.transform, null);
        InstantiateWagonComponents(profile.colliders, colliders.transform, null);
        InstantiateWagonComponents(profile.internalDetails, wagonInstance.transform, null);
        InstantiateWagonComponents(profile.passengerSensor, wagonInstance.transform, null);

        //Instantiate specific profile components
        if (profile.type == WagonType.Wagon)
        {
            //Add specific unity components
            Wagon_v3   wagonScript = wagonInstance.AddComponent <Wagon_v3>();
            HingeJoint hingeJoint  = wagonInstance.AddComponent <HingeJoint>();

            //Configure specific unity components
            hingeJoint.axis = new Vector3(1f, 1f, 0f);

            //Instantiate specific unity components
            InstantiateWagonComponents(profile.wheelsSFX, sfx.transform, new Action <Wagon_v3, List <GameObject> >(ConfigureWheelSFX), wagonScript);
            InstantiateWagonComponents(profile.wagonConnectionSFX, sfx.transform, new Action <Wagon_v3, List <GameObject> >(ConfigureConnectionSFX), wagonScript);
            //Couplers
            InstantiateWagonComponents(profile.frontCoupler, wagonInstance.transform, new Action <Rigidbody, Wagon_v3, HingeJoint, List <GameObject> >(ConfigureFrontCoupler), rigidbody, wagonScript, hingeJoint);
            InstantiateWagonComponents(profile.backCoupler, wagonInstance.transform, new Action <Rigidbody, Wagon_v3, List <GameObject> >(ConfigureBackCoupler), rigidbody, wagonScript);
            InstantiateWagonComponents(profile.defaultJointAnchor, wagonInstance.transform, new Action <Wagon_v3, List <GameObject> >(ConfigureDefaultJointAnchor), wagonScript);
            //Wheels
            InstantiateWagonComponents(profile.wheelsPhysics, wheels.transform, new Action <Rigidbody, Wagon_v3, List <GameObject> >(ConfigurePhysicsWheels), rigidbody, wagonScript);
            InstantiateWagonComponents(profile.wheelsVisuals, wheels.transform, new Action <Wagon_v3, List <GameObject> >(ConfigureWheels), wagonScript);
            //Sensors
            InstantiateWagonComponents(profile.railSensor, sensors.transform, new Action <Wagon_v3, List <GameObject> >(ConfigureSensors), wagonScript);
            //Lights
            InstantiateWagonComponents(profile.externalLights, lights.transform, new Action <Wagon_v3, List <GameObject> >(ConfigureExternalLights), wagonScript);
            InstantiateWagonComponents(profile.internalLights, lights.transform, new Action <Wagon_v3, List <GameObject> >(ConfigureInternalLights), wagonScript);
        }
        else if (profile.type == WagonType.Locomotive)
        {
            //Add specific unity components
            TrainController_v3 locomotiveScript = wagonInstance.AddComponent <TrainController_v3>();
            wagonInstance.AddComponent <TrainStationController>();
            TrainSpeedMonitor speedMonitor  = wagonInstance.AddComponent <TrainSpeedMonitor>();
            TrainPlayerInput  playerInput   = wagonInstance.AddComponent <TrainPlayerInput>();
            PassengerTags     passengerTags = wagonInstance.AddComponent <PassengerTags>();
            passengerTags.passengerTags = new List <string>()
            {
                "Player", "NPC"
            };
            BoxCollider locomotiveTrigger = wagonInstance.AddComponent <BoxCollider>();

            //Configure specific unity components
            locomotiveScript.enginesOn    = true;
            locomotiveScript.acceleration = 1f;
            playerInput.inputSettings     = profile.inputSettings;
            locomotiveTrigger.isTrigger   = true;
            locomotiveTrigger.size        = new Vector3(0.5f, 0.5f, 0.5f);
            locomotiveTrigger.center      = profile.controlZoneTriggerPosition;
            speedMonitor.speedUnit        = profile.speedUnits;

            //Instantitate specific unity components
            InstantiateWagonComponents(profile.engineSFX, sfx.transform, new Action <TrainController_v3, List <GameObject> >(ConfigureEngineSFX), locomotiveScript);
            InstantiateWagonComponents(profile.brakesSFX, sfx.transform, new Action <TrainController_v3, List <GameObject> >(ConfigureBrakesSFX), locomotiveScript);
            InstantiateWagonComponents(profile.wheelsSFX, sfx.transform, new Action <TrainController_v3, List <GameObject> >(ConfigureWheelSFX), locomotiveScript);
            InstantiateWagonComponents(profile.hornSFX, sfx.transform, new Action <TrainController_v3, List <GameObject> >(ConfigureHornSFX), locomotiveScript);
            InstantiateWagonComponents(profile.bellSFX, sfx.transform, new Action <TrainController_v3, List <GameObject> >(ConfigureBellSFX), locomotiveScript);
            //Couplers
            InstantiateWagonComponents(profile.frontCoupler, wagonInstance.transform, new Action <Rigidbody, List <GameObject> >(ConnectHingeAnchor), rigidbody);
            InstantiateWagonComponents(profile.backCoupler, wagonInstance.transform, new Action <Rigidbody, TrainController_v3, List <GameObject> >(ConfigureBackCoupler), rigidbody, locomotiveScript);
            //Wheels
            InstantiateWagonComponents(profile.wheelsPhysics, wheels.transform, new Action <Rigidbody, TrainController_v3, List <GameObject> >(ConfigurePhysicsWheels), rigidbody, locomotiveScript);
            InstantiateWagonComponents(profile.wheelsVisuals, wheels.transform, new Action <TrainController_v3, List <GameObject> >(ConfigureWheels), locomotiveScript);
            //Sensors
            InstantiateWagonComponents(profile.railSensor, sensors.transform, new Action <TrainController_v3, List <GameObject> >(ConfigureSensors), locomotiveScript);
            //Lights
            InstantiateWagonComponents(profile.externalLights, lights.transform, new Action <TrainController_v3, List <GameObject> >(ConfigureExternalLights), locomotiveScript);
            InstantiateWagonComponents(profile.internalLights, lights.transform, new Action <TrainController_v3, List <GameObject> >(ConfigureInternalLights), locomotiveScript);
        }

        TrainDoorsController doorsController = wagonInstance.AddComponent <TrainDoorsController>();

        wagonInstance.AddComponent <SMR_IgnoredObject>();
        wagonInstance.AddComponent <TrainSuspension>();

        InstantiateWagonComponents(profile.cabinDoorLeft, doors.transform, new Action <TrainDoorsController, List <GameObject> >(ConfigureLeftCabinDoor), doorsController);
        InstantiateWagonComponents(profile.cabinDoorRight, doors.transform, new Action <TrainDoorsController, List <GameObject> >(ConfigureRightCabinDoor), doorsController);
        InstantiateWagonComponents(profile.passengerDoorLeft, doors.transform, new Action <TrainDoorsController, List <GameObject> >(ConfigureLeftPassengerDoors), doorsController);
        InstantiateWagonComponents(profile.passengerDoorRight, doors.transform, new Action <TrainDoorsController, List <GameObject> >(ConfigureRightPassengerDoors), doorsController);
        InstantiateWagonComponents(profile.openDoorSFX, sfx.transform, new Action <TrainDoorsController, List <GameObject> >(ConfigureOpenDoorSFX), doorsController);
        InstantiateWagonComponents(profile.closeDoorSFX, sfx.transform, new Action <TrainDoorsController, List <GameObject> >(ConfigureCloseDoorSFX), doorsController);

        string carType = profile.type.ToString();

        message = string.Format("{0} created successfully!", carType);
        return(true);
    }