using System; using System.Collections; using System.Collections.Generic; using Framework; using UnityEngine; using Sirenix.OdinInspector; using Sirenix.OdinInspector.Editor; using TGS; using Debug = DebugUtil; using Object = UnityEngine.Object; using UnityEditor; [ExecuteAlways] public class MapSceneEditor : MonoBehaviour { private const float UPP = 100; private TerrainGridSystem _tgs; private GridEditor _gridEditor; [SerializeField] [HideInInspector] private string _mapDataJson = ""; private MapData _mapData = new(); public MapData mapData => _mapData; //[BoxGroup("地图编辑器/Split/Right/地图基本结构")] [TableColumnWidth(57, Resizable = false)] [PreviewField(Alignment = ObjectFieldAlignment.Center)] [AssetList(Path = (Constants.MAP_TEXTURE_PATH_WITHOUT_ASSET))] //[EnableIf("canRebuild")] [OnValueChanged("OnMapMaterialChanged")] [LabelText("地面图")] [Indent] public Material groundMaterial; [LabelText("地图尺寸")] [LabelWidth(100)] [Indent] [ReadOnly] public Vector2 MapScale; private void OnMapMaterialChanged() { if (groundMaterial != null&&groundMaterial.mainTexture!=null) { _tgs.GetComponent().sharedMaterial = groundMaterial; MapScale = (new Vector2(groundMaterial.mainTexture.width, groundMaterial.mainTexture.height) / UPP); _tgs.transform.localScale = new Vector3(MapScale.x,MapScale.y,1) ; _tgs.regularHexagons = false; var width = GetHexCount(MapScale.x, _tgs.regularHexagonsWidth, false); var height = GetHexCount(MapScale.y, _tgs.regularHexagonsWidth, true); _tgs.rowCount = height; _tgs.columnCount = width; _tgs.regularHexagons = true; } } private int GetHexCount(float totalLength, float hexWidth, bool isDiagonal) { if (isDiagonal) return Mathf.FloorToInt((4 * totalLength) / (3 * hexWidth) - 1 / 3f); else { var sqrt3 = Mathf.Sqrt(3); var countInFloat = (totalLength - (sqrt3 / 4 * hexWidth)) / (Mathf.Sqrt(3) / 2 * hexWidth); return Mathf.FloorToInt(countInFloat); } } private void Awake() { _tgs = Object.FindObjectOfType(); if (_tgs == null) { Debug.LogError("Terrain Grid System Is Null!"); } _gridEditor = Object.FindObjectOfType(); if (_gridEditor == null) { Debug.LogError("Grid Editor is null!"); } } }