Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.IO;
- using CityGen3D;
- using Unity.EditorCoroutines.Editor;
- using UnityEditor;
- using UnityEditor.SceneManagement;
- using UnityEngine;
- using UnityEngine.Networking;
- using UnityEngine.SceneManagement;
- #if UNITY_EDITOR
- public class FetchOsmFileForEasyRoads3DImportWizard : ScriptableWizard
- {
- private const float WindowWidth = 500f;
- private const float WindowHeight = 350f;
- [SerializeField] private string saveFileName;
- private GeoCoord _minBounds;
- private GeoCoord _maxBounds;
- [MenuItem ("Tools/CityGen3D/Fetch OSM File for EasyRoads3D Import", priority = 20)]
- private static void CreateWizard ()
- {
- DisplayWizard (
- "Fetch OSM File for EasyRoads3D Import",
- typeof (FetchOsmFileForEasyRoads3DImportWizard),
- "Fetch OSM File");
- }
- private void OnEnable ()
- {
- ConstrainWindowRectSize (WindowWidth, WindowHeight);
- Landscape [] landscapes = FindObjectsOfType<Landscape> ();
- if (landscapes == null || landscapes.Length == 0)
- {
- helpString = "No CityGen3D Landscape objects in scene.";
- throw new ArgumentException (helpString);
- }
- // Map.Instance doesn't seem to cover bounds across multiple landscapes quite right, so aggregate these ourselves
- _minBounds = new GeoCoord ();
- _maxBounds = new GeoCoord ();
- bool setFirst = true;
- foreach (Landscape landscape in landscapes)
- {
- GeoCoord min = landscape.locationMin;
- GeoCoord max = landscape.locationMax;
- if (setFirst || min.latitude < _minBounds.latitude)
- {
- _minBounds.latitude = min.latitude;
- }
- if (setFirst || min.longitude < _minBounds.longitude)
- {
- _minBounds.longitude = min.longitude;
- }
- if (setFirst || max.latitude > _maxBounds.latitude)
- {
- _maxBounds.latitude = max.latitude;
- }
- if (setFirst || max.longitude > _maxBounds.longitude)
- {
- _maxBounds.longitude = max.longitude;
- }
- setFirst = false;
- }
- if (string.IsNullOrEmpty (saveFileName))
- {
- saveFileName = SceneManager.GetActiveScene ().name + ".osm";
- }
- }
- private static FetchOsmFileForEasyRoads3DImportWizard GetWindowReference ()
- {
- // Get the window without changing the focus away from the current window
- return (FetchOsmFileForEasyRoads3DImportWizard) GetWindow (
- typeof (FetchOsmFileForEasyRoads3DImportWizard), false, "Fetch OSM File", false);
- }
- private static void ConstrainWindowRectSize (float windowWidth, float windowHeight)
- {
- FetchOsmFileForEasyRoads3DImportWizard window = GetWindowReference ();
- window.minSize = new Vector2 (windowWidth, windowHeight);
- window.maxSize = new Vector2 (windowWidth, windowHeight);
- }
- protected override bool DrawWizardGUI ()
- {
- GUIStyle labelStyle = new (EditorStyles.label) {richText = true};
- GUILayoutOption labelWidth = GUILayout.Width (600f);
- GUILayout.Label ("<b><i>USAGE</i></b>", labelStyle, labelWidth);
- GUILayout.Space (10);
- GUILayout.Label ("1. Run this wizard in a scene with CityGen3D terrain landscapes present", labelStyle, labelWidth);
- GUILayout.Label ("2. Set appropriate filename (to save in Database folder) and click <b>Fetch OSM File</b>", labelStyle, labelWidth);
- GUILayout.Label ("3. Create an EasyRoads3D Road Network object in the scene", labelStyle, labelWidth);
- GUILayout.Label ("4. Go under the Road Network's <i>General Settings/Import Road Data</i> tab", labelStyle, labelWidth);
- GUILayout.Label ("5. Leave <i>Global Terrain Coordinates</i> on defaults of 0", labelStyle, labelWidth);
- GUILayout.Label ("6. Adjust any of the <i>OSM Import</i> dropdowns as needed", labelStyle, labelWidth);
- GUILayout.Label ("7. Click <b>Open OSM File</b> and select the newly created file", labelStyle, labelWidth);
- GUILayout.Space (20);
- GUILayout.Label ("Landscape min: " + _minBounds, labelStyle, labelWidth);
- GUILayout.Label ("Landscape max: " + _maxBounds, labelStyle, labelWidth);
- GUILayout.Space (10);
- return base.DrawWizardGUI ();
- }
- private void OnWizardCreate ()
- {
- if (_minBounds == null || _maxBounds == null)
- {
- helpString = "No CityGen3D Landscape objects in scene.";
- throw new ArgumentException (helpString);
- }
- if (string.IsNullOrEmpty (saveFileName))
- {
- helpString = "No save filename provided.";
- throw new ArgumentException (helpString);
- }
- EditorCoroutineUtility.StartCoroutine (DoProcessing (), this);
- }
- private IEnumerator DoProcessing ()
- {
- EditorUtility.ClearProgressBar ();
- EditorUtility.DisplayProgressBar ("Fetching OSM data", "Fetching OSM data", 0.1f);
- Map.Instance.data.UpdateQuery ();
- UnityWebRequest www = UnityWebRequest.Get (Map.Instance.data.query);
- yield return www.SendWebRequest ();
- if (www.result == (UnityWebRequest.Result) 2 || www.result == (UnityWebRequest.Result) 3)
- {
- EditorUtility.ClearProgressBar ();
- helpString = "Download failed.";
- throw new ArgumentException (helpString);
- }
- EditorUtility.DisplayProgressBar ("Processing OSM data", "Processing OSM data into EasyRoads3D compatible format", 0.5f);
- // Hack for adding bounds in, that query result doesn't include
- string result = www.downloadHandler.text.Replace (
- "<note>",
- "<bounds minlat=\"" + _minBounds.latitude + "\" " +
- "minlon=\"" + _minBounds.longitude + "\" " +
- "maxlat=\"" + _maxBounds.latitude + "\" " +
- "maxlon=\"" + _maxBounds.longitude + "\"/>\n<note>");
- EditorUtility.DisplayProgressBar ("Saving OSM data", "Saving OSM data", 0.9f);
- string saveFilePath = Path.Combine (Application.dataPath, "Database", saveFileName);
- File.WriteAllText (saveFilePath, result);
- EditorUtility.ClearProgressBar ();
- Scene currentScene = SceneManager.GetActiveScene ();
- EditorSceneManager.MarkSceneDirty (currentScene);
- }
- }
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement