RealWorldTerrainGeocodingWindow.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /* INFINITY CODE */
  2. /* https://infinity-code.com */
  3. using InfinityCode.RealWorldTerrain.Webservices;
  4. using InfinityCode.RealWorldTerrain.Webservices.Results;
  5. using UnityEditor;
  6. using UnityEngine;
  7. namespace InfinityCode.RealWorldTerrain.Tools
  8. {
  9. public class RealWorldTerrainGeocodingWindow : EditorWindow
  10. {
  11. private static RealWorldTerrainGeocodingWindow wnd;
  12. private RealWorldTerrainMonoBase target;
  13. private string response;
  14. private Vector2 scrollPosition;
  15. private string address;
  16. private string languageCode = "en";
  17. private string key;
  18. private GameObject resultGameObject;
  19. private void OnDestroy()
  20. {
  21. wnd = null;
  22. }
  23. private void OnGUI()
  24. {
  25. target = EditorGUILayout.ObjectField("Container", target, typeof(RealWorldTerrainContainer), true) as RealWorldTerrainContainer;
  26. address = EditorGUILayout.TextField("Location Name", address);
  27. key = EditorGUILayout.TextField("Google API key", key);
  28. EditorGUILayout.BeginHorizontal();
  29. languageCode = EditorGUILayout.TextField("Language Code", languageCode);
  30. RealWorldTerrainWindowUI.DrawHelpButton("List of Languages", "https://developers.google.com/maps/faq?hl=en#languagesupport");
  31. EditorGUILayout.EndHorizontal();
  32. if (GUILayout.Button("Search"))
  33. {
  34. RealWorldTerrainGoogleGeocoding.Find(
  35. new RealWorldTerrainGoogleGeocoding.GeocodingParams(address)
  36. {
  37. language = languageCode,
  38. key = key
  39. }
  40. ).OnComplete += OnRequestComplete;
  41. }
  42. EditorGUILayout.Space();
  43. if (!string.IsNullOrEmpty(response))
  44. {
  45. GUILayout.Label("Full Response: ");
  46. scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition);
  47. EditorGUILayout.TextArea(response);
  48. EditorGUILayout.EndScrollView();
  49. }
  50. }
  51. private void OnRequestComplete(string response)
  52. {
  53. this.response = response;
  54. try
  55. {
  56. RealWorldTerrainGoogleGeocodingResult[] result = RealWorldTerrainGoogleGeocoding.GetResults(response);
  57. if (result.Length > 0)
  58. {
  59. Vector3 pos;
  60. target.GetWorldPosition(result[0].geometry_location, out pos);
  61. if (pos != default(Vector3))
  62. {
  63. resultGameObject = GameObject.CreatePrimitive(PrimitiveType.Cube);
  64. resultGameObject.name = "Geocoding Result";
  65. resultGameObject.transform.position = pos;
  66. resultGameObject.GetComponent<MeshRenderer>().sharedMaterial.color = Color.red;
  67. RealWorldTerrainGeocodingObject geocodingObject = resultGameObject.AddComponent<RealWorldTerrainGeocodingObject>();
  68. geocodingObject.info = result[0];
  69. #if UNITY_2019_1_OR_NEWER
  70. SceneView.duringSceneGui += OnSceneGUI;
  71. #else
  72. SceneView.onSceneGUIDelegate += OnSceneGUI;
  73. #endif
  74. }
  75. }
  76. }
  77. catch
  78. {
  79. }
  80. Repaint();
  81. }
  82. private void OnSceneGUI(SceneView view)
  83. {
  84. view.LookAt(resultGameObject.transform.position, view.camera.transform.rotation, 100);
  85. #if UNITY_2019_1_OR_NEWER
  86. SceneView.duringSceneGui -= OnSceneGUI;
  87. #else
  88. SceneView.onSceneGUIDelegate -= OnSceneGUI;
  89. #endif
  90. }
  91. [MenuItem("Window/Infinity Code/Real World Terrain/Tools/Geocoder")]
  92. public static void OpenWindow()
  93. {
  94. OpenWindow(null);
  95. }
  96. public static void OpenWindow(RealWorldTerrainMonoBase target)
  97. {
  98. if (wnd != null) wnd.Close();
  99. wnd = GetWindow<RealWorldTerrainGeocodingWindow>(true, "Geocoder");
  100. if (target == null) wnd.target = FindObjectOfType<RealWorldTerrainContainer>();
  101. else wnd.target = target;
  102. }
  103. }
  104. }