RealWorldTerrainCurrentLatLon.cs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /* INFINITY CODE 2013-2019 */
  2. /* http://www.infinity-code.com */
  3. using UnityEditor;
  4. using UnityEngine;
  5. namespace InfinityCode.RealWorldTerrain.Tools
  6. {
  7. public class RealWorldTerrainCurrentLatLon : EditorWindow
  8. {
  9. private RealWorldTerrainContainer rwt;
  10. private Vector3 lastCursorPosition;
  11. private static RealWorldTerrainCurrentLatLon wnd;
  12. private void OnDestroy()
  13. {
  14. EditorApplication.update -= OnUpdate;
  15. #if UNITY_2019_1_OR_NEWER
  16. SceneView.duringSceneGui -= OnSceneGUI;
  17. #else
  18. SceneView.onSceneGUIDelegate -= OnSceneGUI;
  19. #endif
  20. wnd = null;
  21. }
  22. private void OnEnable()
  23. {
  24. OnDestroy();
  25. wnd = this;
  26. EditorApplication.update += OnUpdate;
  27. #if UNITY_2019_1_OR_NEWER
  28. SceneView.duringSceneGui += OnSceneGUI;
  29. #else
  30. SceneView.onSceneGUIDelegate += OnSceneGUI;
  31. #endif
  32. }
  33. private void OnGUI()
  34. {
  35. rwt = (RealWorldTerrainContainer)EditorGUILayout.ObjectField("Real World Terrain", rwt, typeof(RealWorldTerrainContainer), true);
  36. if (rwt == null) return;
  37. SceneView view = SceneView.lastActiveSceneView;
  38. if (view == null) return;
  39. Vector3 cp = view.camera.transform.position;
  40. double longitude, latitude, altitude;
  41. rwt.GetCoordinatesByWorldPosition(cp, out longitude, out latitude, out altitude);
  42. EditorGUILayout.LabelField("Scene camera latitude: " + latitude);
  43. EditorGUILayout.LabelField("Scene camera longitude: " + longitude);
  44. EditorGUILayout.LabelField("Scene camera altitude: " + altitude);
  45. if (lastCursorPosition == Vector3.zero) return;
  46. rwt.GetCoordinatesByWorldPosition(lastCursorPosition, out longitude, out latitude, out altitude);
  47. EditorGUILayout.LabelField("Scene cursor latitude: " + latitude);
  48. EditorGUILayout.LabelField("Scene cursor longitude: " + longitude);
  49. EditorGUILayout.LabelField("Scene cursor altitude: " + altitude.ToString("F2") + " meters");
  50. }
  51. private void OnSceneGUI(SceneView view)
  52. {
  53. RaycastHit hit;
  54. Ray ray = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition);
  55. if (Physics.Raycast(ray.origin, ray.direction, out hit)) lastCursorPosition = hit.point;
  56. else lastCursorPosition = Vector3.zero;
  57. }
  58. private void OnUpdate()
  59. {
  60. Repaint();
  61. }
  62. [MenuItem("Window/Infinity Code/Real World Terrain/Tools/Current Position")]
  63. public static void OpenWindow()
  64. {
  65. if (wnd != null) wnd.Close();
  66. wnd = GetWindow<RealWorldTerrainCurrentLatLon>(false, "Current Position");
  67. wnd.rwt = FindObjectOfType<RealWorldTerrainContainer>();
  68. }
  69. public static void OpenWindow(RealWorldTerrainContainer container)
  70. {
  71. if (wnd != null) wnd.Close();
  72. wnd = GetWindow<RealWorldTerrainCurrentLatLon>(false, "Current Position");
  73. wnd.rwt = container;
  74. }
  75. }
  76. }