NLDClient-yudde/ProjectNLD/Assets/Code/Scripts/Gameplay/Map/MapSceneEditor.cs

118 lines
3.2 KiB
C#
Raw Normal View History

2023-11-15 13:30:13 +08:00
using System;
using Framework;
2023-11-15 18:19:17 +08:00
using LC.Newtonsoft.Json;
2023-11-15 13:30:13 +08:00
using UnityEngine;
using Sirenix.OdinInspector;
using TGS;
using Debug = DebugUtil;
2023-11-16 18:27:03 +08:00
using JsonSerializer = Newtonsoft.Json.JsonSerializer;
2023-11-15 13:30:13 +08:00
using Object = UnityEngine.Object;
2023-11-16 18:27:03 +08:00
#if UNITY_EDITOR
2023-11-15 13:30:13 +08:00
using UnityEditor;
2023-11-16 18:27:03 +08:00
using UnityEditor.SceneManagement;
#endif
2023-11-15 13:30:13 +08:00
[ExecuteAlways]
public class MapSceneEditor : MonoBehaviour
{
2023-11-15 16:24:53 +08:00
private const float UPP = 100;
2023-11-16 18:27:03 +08:00
2023-11-15 13:30:13 +08:00
private TerrainGridSystem _tgs;
2023-11-16 18:27:03 +08:00
2023-11-15 18:19:17 +08:00
[ReadOnly]
2023-11-16 18:27:03 +08:00
[HideInInspector]
public TextAsset mapDataJson;
2023-11-15 13:30:13 +08:00
2023-11-16 18:27:03 +08:00
[BoxGroup("地图基本信息")]
[Space]
[HideLabel]
public MapData mapData;
2023-11-15 13:30:13 +08:00
2023-11-16 18:27:03 +08:00
[BoxGroup("地图基本信息")]
2023-11-15 16:24:53 +08:00
[TableColumnWidth(57, Resizable = false)]
[PreviewField(Alignment = ObjectFieldAlignment.Center)]
[AssetList(Path = (Constants.MAP_TEXTURE_PATH_WITHOUT_ASSET))]
2023-11-16 18:27:03 +08:00
[BoxGroup("地图基本信息")]
2023-11-15 16:24:53 +08:00
[OnValueChanged("OnMapMaterialChanged")]
[LabelText("地面图")]
public Material groundMaterial;
[LabelText("地图尺寸")]
[LabelWidth(100)]
[Indent]
[ReadOnly]
public Vector2 MapScale;
2023-11-16 18:27:03 +08:00
2023-11-15 16:24:53 +08:00
private void OnMapMaterialChanged()
2023-11-15 13:30:13 +08:00
{
2023-11-16 18:27:03 +08:00
if (groundMaterial != null && groundMaterial.mainTexture != null)
2023-11-15 13:30:13 +08:00
{
2023-11-15 16:24:53 +08:00
_tgs.GetComponent<MeshRenderer>().sharedMaterial = groundMaterial;
MapScale = (new Vector2(groundMaterial.mainTexture.width,
groundMaterial.mainTexture.height) / UPP);
2023-11-16 18:27:03 +08:00
_tgs.transform.localScale = new Vector3(MapScale.x, MapScale.y, 1);
2023-11-15 16:24:53 +08:00
_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;
2023-11-15 13:30:13 +08:00
}
2023-11-15 16:24:53 +08:00
}
2023-11-16 18:27:03 +08:00
2023-11-15 16:24:53 +08:00
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);
}
}
2023-11-16 18:27:03 +08:00
2023-11-15 16:24:53 +08:00
private void Awake()
{
2023-11-15 13:30:13 +08:00
_tgs = Object.FindObjectOfType<TerrainGridSystem>();
if (_tgs == null)
{
Debug.LogError("Terrain Grid System Is Null!");
}
2023-11-16 18:27:03 +08:00
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();
2023-11-15 13:30:13 +08:00
}
2023-11-16 18:27:03 +08:00
#endif
2023-11-15 13:30:13 +08:00
}