PositionExporterEditor.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using UnityEditor;
  2. using UnityEngine;
  3. using Newtonsoft.Json;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Drawing.Drawing2D;
  7. using System.Linq;
  8. using System.Text.RegularExpressions;
  9. public class PositionExporterEditor : EditorWindow
  10. {
  11. [MenuItem("Tools/Export Positions to JSON")]
  12. public static void ShowWindow()
  13. {
  14. GetWindow<PositionExporterEditor>("Export Positions to JSON");
  15. }
  16. private GameObject[] objectsToProcess;
  17. private int ExtractNumber(string name)
  18. {
  19. // Extract the first number from the name
  20. var match = Regex.Match(name, @"\d+");
  21. return match.Success ? int.Parse(match.Value) : 0;
  22. }
  23. private void OnGUI()
  24. {
  25. GUILayout.Label("Export Positions to JSON", EditorStyles.boldLabel);
  26. if (GUILayout.Button("Select GameObjects"))
  27. {
  28. objectsToProcess = Selection.gameObjects;
  29. // Sort the objects by numeric value in their names
  30. if (objectsToProcess != null)
  31. {
  32. objectsToProcess = objectsToProcess
  33. .OrderBy(go => ExtractNumber(go.name))
  34. .ToArray();
  35. }
  36. }
  37. if (objectsToProcess != null && objectsToProcess.Length > 0)
  38. {
  39. GUILayout.Label("Selected GameObjects:");
  40. foreach (var obj in objectsToProcess)
  41. {
  42. GUILayout.Label(obj.name);
  43. }
  44. if (GUILayout.Button("Export to JSON"))
  45. {
  46. ExportPositionsToJson();
  47. }
  48. }
  49. else
  50. {
  51. GUILayout.Label("No GameObjects selected.");
  52. }
  53. }
  54. [System.Serializable]
  55. public class PathPoint {
  56. public PathPoint(double l1,double l2) {
  57. longitude = l1;
  58. latitude = l2;
  59. }
  60. public double longitude;
  61. public double latitude;
  62. }
  63. [System.Serializable]
  64. public class ObjPos {
  65. public string objName;
  66. public List<PathPoint> paths = new List<PathPoint>();
  67. }
  68. private void ExportPositionsToJson()
  69. {
  70. if (objectsToProcess == null || objectsToProcess.Length == 0)
  71. {
  72. EditorUtility.DisplayDialog("Error", "No GameObjects selected.", "OK");
  73. return;
  74. }
  75. List<ObjPos> objPos = new List<ObjPos>();
  76. foreach (var obj in objectsToProcess)
  77. {
  78. ObjPos obj1 = new ObjPos();
  79. obj1.objName = obj.name;
  80. foreach (Transform child in obj.transform)
  81. {
  82. double longitude = (child.localPosition.x - 0.5 + 8607.658) / 75.642;
  83. double latitude = (child.localPosition.y + 6.7 + 2640.548) / 87.990;
  84. obj1.paths.Add(new PathPoint(longitude,latitude));
  85. }
  86. objPos.Add(obj1);
  87. }
  88. string json = JsonConvert.SerializeObject(objPos);
  89. string path = EditorUtility.SaveFilePanel("Save JSON file", "", "positions.json", "json");
  90. if (!string.IsNullOrEmpty(path))
  91. {
  92. File.WriteAllText(path, json);
  93. EditorUtility.DisplayDialog("Success", "Positions exported successfully.", "OK");
  94. }
  95. }
  96. }