Пример #1
0
    /// <summary>
    ///
    /// </summary>
    private void OnGUI()
    {
        GUILayout.Label("Custom Wagons Creator", EditorStyles.boldLabel);

        //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);

        GUILayout.BeginHorizontal();

        GUILayout.BeginVertical(GUILayout.Width(100));
        EditorGUILayout.LabelField("Profile", GUILayout.Width(100));
        EditorGUILayout.LabelField("Model", GUILayout.Width(100));
        EditorGUILayout.LabelField("Name", GUILayout.Width(100));
        GUILayout.EndVertical();

        GUILayout.BeginVertical();

        _customProfile = EditorGUILayout.ObjectField(_customProfile, typeof(CustomWagonProfile), false) as CustomWagonProfile;
        _modelPrefab   = EditorGUILayout.ObjectField(_modelPrefab, typeof(GameObject), false) as GameObject;
        _wagonName     = EditorGUILayout.TextField(string.Empty, _wagonName);
        GUILayout.EndVertical();

        GUILayout.EndHorizontal();

        if (GUILayout.Button("Create"))
        {
            CreateWagon();
        }

        _errorMessageStyle = new GUIStyle();
        _errorMessageStyle.normal.textColor = _txtColor;
        _errorMessageStyle.fontStyle        = FontStyle.Bold;
        GUILayout.Label(_txtMessage, _errorMessageStyle);

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

        if (profile == null)
        {
            message = string.Format("Profile cannot be null.{0}Please select a profile and try again.", System.Environment.NewLine);
            return(false);
        }

        if (modelPrefab == null)
        {
            message = string.Format("Model cannot be null.{0}Please select your custom model and try again.", System.Environment.NewLine);
            return(false);
        }

        if (name == null || name == string.Empty)
        {
            message = string.Format("Name cannot be null.{0}Please select a name and try again.", System.Environment.NewLine);
            return(false);
        }

        return(true);
    }
    /// <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);
    }