118 lines
3.2 KiB
C#
118 lines
3.2 KiB
C#
using System;
|
|
using Framework;
|
|
using LC.Newtonsoft.Json;
|
|
using UnityEngine;
|
|
using Sirenix.OdinInspector;
|
|
using TGS;
|
|
using Debug = DebugUtil;
|
|
using JsonSerializer = Newtonsoft.Json.JsonSerializer;
|
|
using Object = UnityEngine.Object;
|
|
#if UNITY_EDITOR
|
|
using UnityEditor;
|
|
using UnityEditor.SceneManagement;
|
|
#endif
|
|
|
|
[ExecuteAlways]
|
|
public class MapSceneEditor : MonoBehaviour
|
|
{
|
|
private const float UPP = 100;
|
|
|
|
private TerrainGridSystem _tgs;
|
|
|
|
[ReadOnly]
|
|
[HideInInspector]
|
|
public TextAsset mapDataJson;
|
|
|
|
[BoxGroup("地图基本信息")]
|
|
[Space]
|
|
[HideLabel]
|
|
public MapData mapData;
|
|
|
|
[BoxGroup("地图基本信息")]
|
|
[TableColumnWidth(57, Resizable = false)]
|
|
[PreviewField(Alignment = ObjectFieldAlignment.Center)]
|
|
[AssetList(Path = (Constants.MAP_TEXTURE_PATH_WITHOUT_ASSET))]
|
|
[BoxGroup("地图基本信息")]
|
|
[OnValueChanged("OnMapMaterialChanged")]
|
|
[LabelText("地面图")]
|
|
public Material groundMaterial;
|
|
|
|
[LabelText("地图尺寸")]
|
|
[LabelWidth(100)]
|
|
[Indent]
|
|
[ReadOnly]
|
|
public Vector2 MapScale;
|
|
|
|
private void OnMapMaterialChanged()
|
|
{
|
|
if (groundMaterial != null && groundMaterial.mainTexture != null)
|
|
{
|
|
_tgs.GetComponent<MeshRenderer>().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<TerrainGridSystem>();
|
|
if (_tgs == null)
|
|
{
|
|
Debug.LogError("Terrain Grid System Is Null!");
|
|
}
|
|
|
|
if (mapDataJson != null)
|
|
{
|
|
var jsonStr = mapDataJson.text;
|
|
if (string.IsNullOrEmpty(jsonStr))
|
|
{
|
|
mapData = new();
|
|
}
|
|
else
|
|
{
|
|
mapData = JsonConvert.DeserializeObject<MapData>(jsonStr);
|
|
}
|
|
}
|
|
}
|
|
|
|
[Button("保存")]
|
|
public void SaveMapData()
|
|
{
|
|
#if UNITY_EDITOR
|
|
if (mapDataJson != null)
|
|
{
|
|
var path = AssetDatabase.GetAssetPath(mapDataJson);
|
|
JsonHelper.SaveJson(path, mapData);
|
|
Debug.Log($"$path:{path}");
|
|
EditorSceneManager.SaveOpenScenes();
|
|
}
|
|
#endif
|
|
}
|
|
|
|
|
|
#if UNITY_EDITOR
|
|
private void OnDestroy()
|
|
{
|
|
SaveMapData();
|
|
}
|
|
#endif
|
|
} |