using System; using System.Collections.Generic; using System.Runtime.Remoting.Contexts; using HexGrid.MapHelper; using UnityEngine; using HexGrid.Math; #if UNITY_EDITOR using UnityEditor; #endif namespace HexGrid { // 数据保存,不放在Manager 中处理 // [ExecuteInEditMode] public class HexMapSaver : MonoBehaviour { public Transform root; // 标准格子 [SerializeField] private GameObject standGridPrefab; private GameObject standGridGo; // private Transform rootBuilding; // public HexGridMap data; // private void Awake() { } private void SetupStandGrid() { if (standGridPrefab) { standGridGo = Instantiate(standGridPrefab, Vector3.zero, Quaternion.identity, transform); standGridGo.name = "StandGrid"; standGridGo.SetActive(false); var size = HexUtils.DetermineTileSize(standGridGo, out var bounds); HexMath.SetGridSize(size.z * 0.5f); DestroyImmediate(standGridGo); // 移除 } HexMath.SetHexMapOrigin(Vector3.zero); } // 保存建筑数据 private void SaveBuildings() { rootBuilding = root.Find("merge"); var buildingNum = rootBuilding.childCount; foreach (Transform child in rootBuilding) { // data.buildings.Add(); var building = new HexBuilding { buildingName = child.gameObject.name, buildingPosition = child.position, hasTiles = new List() }; if (building.buildingName == "Hotel") { building.hasTiles.Add(HexUtils.MakeTileId(new Vector2Int(-3, 4))); building.hasTiles.Add(HexUtils.MakeTileId(new Vector2Int(-2, 4))); } if (building.buildingName == "hill") { building.hasTiles.Add("tile:(-2,6)"); building.hasTiles.Add("tile:(-3,6)"); building.hasTiles.Add("tile:(-1,5)"); building.hasTiles.Add("tile:(-2,5)"); } data.buildings.Add(building); } } private void SaveZone() { } private static HexTile BuildTile(int id,Transform tileTransform,HexZone zone,HexSubZone subZone,TileType tileType) { var worldPos = tileTransform.position; var tileCoords = HexMath.PixelToAxial(worldPos); var tile = new HexTile { id = id, tileOriName = tileTransform.gameObject.name, tileId = HexUtils.MakeTileId(tileCoords), zoneId = zone.zoneId, zoneName = zone.zoneName, subZoneIndex = subZone?.subZoneId ?? 0, tileCoords = tileCoords, type = tileType, }; return tile; } #if UNITY_EDITOR [ContextMenu("Save Data")] void SaveData() { EditorUtility.SetDirty(data); AssetDatabase.SaveAssets(); Debug.Log("Data saved to asset file."); } [ContextMenu("Save Data2")] void SaveData2() { SetupStandGrid(); data.Clear(); SaveBuildings(); SaveZone(); var zone = new HexZone { zoneName = "zone_01", zoneId = 0, isUnlocked = true }; var zone1 = root.Find(zone.zoneName); var count = 0; var subZoneId = 0; HexSubZone subZone = null; foreach (Transform child in zone1) { if (!child.GetComponent()) { continue; } if (count % 3 == 0) { subZone = new HexSubZone(); zone.subZones.Add(subZone); subZone.subZoneId = subZoneId++; } // var worldPos = child.position; // var tileCoords = HexMath.PixelToAxial(worldPos); // Debug.Log($"tileCoords -> {tileCoords.x}, {tileCoords.y}"); // var tile = new HexTile // { // id = count, // tileOriName = child.gameObject.name, // tileId = HexUtils.MakeTileId(tileCoords), // zoneId = zone.zoneId, // zoneName = zone.zoneName, // subZoneIndex = subZone?.subZoneId ?? 0, // tileCoords = tileCoords, // type = TileType.Grass, // }; // zone.tiles.Add(BuildTile(count,child,zone,subZone,TileType.Grass)); var tile = zone.AddTile(BuildTile(count, child, zone, subZone, TileType.Grass)); subZone?.tileNames.Add(tile.tileId); count++; } data.zones.Add(zone); zone = new HexZone { zoneName = "zone_02", zoneId = 1, isUnlocked = false }; data.zones.Add(zone); var zoneTransform = root.Find(zone.zoneName); count = 0; subZone = null; subZoneId = 0; foreach (Transform child in zoneTransform) { if (!child.GetComponent()) { continue; } if (count % 3 == 0) { subZone = new HexSubZone(); zone.subZones.Add(subZone); subZone.subZoneId = subZoneId++; } var worldPos = child.position; var tileCoords = HexMath.PixelToAxial(worldPos); Debug.Log($"tileCoords -> {tileCoords.x}, {tileCoords.y}"); // var tile = new HexTile // { // id = count, // tileOriName = child.gameObject.name, // tileId = HexUtils.MakeTileId(tileCoords), // zoneId = zone.zoneId, // zoneName = zone.zoneName, // subZoneIndex = subZone?.subZoneId ?? 0, // tileCoords = tileCoords, // type = TileType.Grass, // }; // zone.tiles.Add(tile); var tile = zone.AddTile(BuildTile(count, child, zone, subZone, TileType.Grass)); subZone?.tileNames.Add(tile.tileId); count++; } SaveData(); } #endif } }