Esempio n. 1
0
    static void SaveStationsAndLines()
    {
        var sgos = GameObject.FindGameObjectsWithTag("TransStation");

        if (sgos.Length < 1)
        {
            Debug.Log("no stations to save.");
            return;
        }
        var lgos = GameObject.FindGameObjectsWithTag("TransLine");

        if (lgos.Length < 1)
        {
            Debug.Log("no lines to save.");
            return;
        }

        var path = EditorUtility.SaveFilePanel("Save XML Data", "Assets/Transportations", "", "xml");

        if (path.Length == 0)
        {
            EditorUtility.DisplayDialog("Saving Cancelled", "No file was provided", "OK");
            return;
        }

        List <BaseStation> stations = new List <BaseStation>();

        foreach (var go in sgos)
        {
            var sc = go.GetComponent <StationController>();
            var bs = new BaseStation
            {
                id   = int.Parse(sc.name),
                x    = go.transform.position.x,
                y    = go.transform.position.y,
                z    = go.transform.position.z,
                name = sc.stationName,
            };
            stations.Add(bs);
        }

        List <BaseLine> lines = new List <BaseLine>();

        foreach (var go in lgos)
        {
            var lc = go.GetComponent <LineController>();
            var bl = LineController.CreateBaseLine(lc);
            lines.Add(bl);
        }

        var container = new TrafficContainer();

        container.stations = stations;
        container.lines    = lines;
        container.Save(path);
        string stationStats = string.Format("{0} stations saved.", container.stations.Count);
        string lineStats    = string.Format("{0} lines saved.", container.lines.Count);

        EditorUtility.DisplayDialog("Saving Finished", stationStats + "\n" + lineStats + "\n to: " + path, "OK");
    }
Esempio n. 2
0
    static void LoadStationsAndLines()
    {
        AddModuleIfNotExist(transModuleName);

        var path = EditorUtility.OpenFilePanel("Load XML Data", "Assets/Transportations", "xml");

        if (path.Length == 0)
        {
            EditorUtility.DisplayDialog("Loading Cancelled", "No file was provided", "OK");
            return;
        }

        ClearAll();

        // Load stations and lines from xml.
        var container = TrafficContainer.Load(path);
        // Add stations from container to the scene.
        StationCreator stationCreator = StationCreator.AttachToGameObject();

        stationCreator.SetStations(FindObjectsOfType <StationController>());
        // the station might get a new id, therefor using a dictionary to
        // find the right stationController when creating line.
        Dictionary <int, StationController> idLookUp = new Dictionary <int, StationController>();

        foreach (var s in container.stations)
        {
            var station = stationCreator.AddNewStation(s.GetPoint(), s.name);
            idLookUp.Add(s.id, station);
        }
        DestroyImmediate(stationCreator);

        // Add lines from container to the scene.
        TransLineCreator lineCreator = TransLineCreator.AttachToGameObject();

        lineCreator.SetLines(GameObject.FindGameObjectsWithTag("TransLine"));
        foreach (var line in container.lines)
        {
            lineCreator.ResetEditingInfo();
            lineCreator.editLineName = line.name;
            lineCreator.lineCategory = line.GetCategory();

            foreach (var id in line.GetStationIds())
            {
                var station = idLookUp[id];
                lineCreator.AddStationToNewLine(station, line);
            }
            lineCreator.CreateNewLine();
        }
        DestroyImmediate(lineCreator);

        string stationStats = string.Format("{0} stations loaded to the scene.", container.stations.Count);
        string lineStats    = string.Format("{0} lines loaded to the scene.", container.lines.Count);

        EditorUtility.DisplayDialog("Loading Finished", stationStats + "\n" + lineStats, "OK");
    }