using UnityEditor; using UnityEngine; using Newtonsoft.Json; using System.Collections.Generic; using System.IO; using System.Drawing.Drawing2D; using System.Linq; using System.Text.RegularExpressions; public class PositionExporterEditor : EditorWindow { [MenuItem("Tools/Export Positions to JSON")] public static void ShowWindow() { GetWindow("Export Positions to JSON"); } private GameObject[] objectsToProcess; private int ExtractNumber(string name) { // Extract the first number from the name var match = Regex.Match(name, @"\d+"); return match.Success ? int.Parse(match.Value) : 0; } private void OnGUI() { GUILayout.Label("Export Positions to JSON", EditorStyles.boldLabel); if (GUILayout.Button("Select GameObjects")) { objectsToProcess = Selection.gameObjects; // Sort the objects by numeric value in their names if (objectsToProcess != null) { objectsToProcess = objectsToProcess .OrderBy(go => ExtractNumber(go.name)) .ToArray(); } } if (objectsToProcess != null && objectsToProcess.Length > 0) { GUILayout.Label("Selected GameObjects:"); foreach (var obj in objectsToProcess) { GUILayout.Label(obj.name); } if (GUILayout.Button("Export to JSON")) { ExportPositionsToJson(); } } else { GUILayout.Label("No GameObjects selected."); } } [System.Serializable] public class PathPoint { public PathPoint(double l1,double l2) { longitude = l1; latitude = l2; } public double longitude; public double latitude; } [System.Serializable] public class ObjPos { public string objName; public List paths = new List(); } private void ExportPositionsToJson() { if (objectsToProcess == null || objectsToProcess.Length == 0) { EditorUtility.DisplayDialog("Error", "No GameObjects selected.", "OK"); return; } List objPos = new List(); foreach (var obj in objectsToProcess) { ObjPos obj1 = new ObjPos(); obj1.objName = obj.name; foreach (Transform child in obj.transform) { double longitude = (child.localPosition.x - 0.5 + 8607.658) / 75.642; double latitude = (child.localPosition.y + 6.7 + 2640.548) / 87.990; obj1.paths.Add(new PathPoint(longitude,latitude)); } objPos.Add(obj1); } string json = JsonConvert.SerializeObject(objPos); string path = EditorUtility.SaveFilePanel("Save JSON file", "", "positions.json", "json"); if (!string.IsNullOrEmpty(path)) { File.WriteAllText(path, json); EditorUtility.DisplayDialog("Success", "Positions exported successfully.", "OK"); } } }