123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- 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<PositionExporterEditor>("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<PathPoint> paths = new List<PathPoint>();
- }
- private void ExportPositionsToJson()
- {
- if (objectsToProcess == null || objectsToProcess.Length == 0)
- {
- EditorUtility.DisplayDialog("Error", "No GameObjects selected.", "OK");
- return;
- }
- List<ObjPos> objPos = new List<ObjPos>();
- 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");
- }
- }
- }
|