2023-12-15 18:32:21 +08:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using Gameplay;
|
|
|
|
|
|
using LC.Newtonsoft.Json;
|
|
|
|
|
|
using PhxhSDK.Phxh;
|
|
|
|
|
|
using TGS;
|
|
|
|
|
|
#if UNITY_EDITOR
|
2023-12-18 19:42:46 +08:00
|
|
|
|
using System.IO;
|
2023-12-15 18:32:21 +08:00
|
|
|
|
using UnityEditor;
|
|
|
|
|
|
using UnityEditor.SceneManagement;
|
2023-12-18 19:42:46 +08:00
|
|
|
|
using UnityEngine.Windows;
|
2023-12-15 18:32:21 +08:00
|
|
|
|
#endif
|
|
|
|
|
|
using UnityEngine;
|
2023-12-18 19:42:46 +08:00
|
|
|
|
using File = System.IO.File;
|
2023-12-15 18:32:21 +08:00
|
|
|
|
|
|
|
|
|
|
[ExecuteAlways]
|
|
|
|
|
|
public class MapManager : MonoBehaviour
|
|
|
|
|
|
{
|
|
|
|
|
|
//[SerializeField]
|
|
|
|
|
|
//[HideInInspector]
|
|
|
|
|
|
private TerrainGridSystem _tgs;
|
|
|
|
|
|
|
|
|
|
|
|
[SerializeField]
|
|
|
|
|
|
[HideInInspector]
|
|
|
|
|
|
private TextAsset _mapDataJson;
|
|
|
|
|
|
|
|
|
|
|
|
//[SerializeField]
|
|
|
|
|
|
//[HideInInspector]
|
|
|
|
|
|
private MapData _mapData;
|
|
|
|
|
|
|
|
|
|
|
|
private Map _map;
|
|
|
|
|
|
|
|
|
|
|
|
public Map Map
|
|
|
|
|
|
{
|
|
|
|
|
|
get
|
|
|
|
|
|
{
|
|
|
|
|
|
if (_map == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
_map = MapUtils.LoadMap(TGS, MapData);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return _map;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public TerrainGridSystem TGS
|
|
|
|
|
|
{
|
|
|
|
|
|
get
|
|
|
|
|
|
{
|
|
|
|
|
|
if (_tgs == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
var allTgs = FindObjectsOfType<TerrainGridSystem>();
|
|
|
|
|
|
_tgs = allTgs.FirstOrDefault(t => t.gameObject.scene == gameObject.scene);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return _tgs;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public TextAsset MapDataJson
|
|
|
|
|
|
{
|
|
|
|
|
|
get => _mapDataJson;
|
|
|
|
|
|
set
|
|
|
|
|
|
{
|
2023-12-18 19:42:46 +08:00
|
|
|
|
if (_mapDataJson != value)
|
2023-12-15 18:32:21 +08:00
|
|
|
|
{
|
|
|
|
|
|
var data = JsonToMapData(value);
|
2023-12-18 19:42:46 +08:00
|
|
|
|
_mapDataJson = value;
|
2023-12-15 18:32:21 +08:00
|
|
|
|
if (data != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
_mapDataJson = value;
|
|
|
|
|
|
Refresh();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-12-18 19:42:46 +08:00
|
|
|
|
public void Refresh()
|
2023-12-15 18:32:21 +08:00
|
|
|
|
{
|
|
|
|
|
|
_mapData = null;
|
|
|
|
|
|
_map = null;
|
2023-12-18 19:42:46 +08:00
|
|
|
|
if(_mapDataJson == null)
|
|
|
|
|
|
return;
|
2023-12-15 18:32:21 +08:00
|
|
|
|
var mapData = MapData;
|
|
|
|
|
|
var map = Map;
|
|
|
|
|
|
TGS.showCells = true;
|
|
|
|
|
|
TGS.colorizeTerritories = true;
|
2023-12-18 19:42:46 +08:00
|
|
|
|
//TGS.TerritoryDestroyAll();
|
2023-12-15 18:32:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public MapData MapData
|
|
|
|
|
|
{
|
|
|
|
|
|
get
|
|
|
|
|
|
{
|
|
|
|
|
|
if (_mapData == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
var data = JsonToMapData(MapDataJson);
|
|
|
|
|
|
if (data != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
_mapData = data;
|
2023-12-18 19:42:46 +08:00
|
|
|
|
var width = _mapData.Width;
|
|
|
|
|
|
var height = _mapData.Height;
|
2023-12-15 18:32:21 +08:00
|
|
|
|
if (width != 0 && height != 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
CommonUtils.CorrectTGSColumnAndRow(ref width, ref height);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
width = 2;
|
|
|
|
|
|
height = 2;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TGS.columnCount = width;
|
|
|
|
|
|
TGS.rowCount = height;
|
2023-12-18 19:42:46 +08:00
|
|
|
|
_mapData.Width = width;
|
|
|
|
|
|
_mapData.Height = height;
|
|
|
|
|
|
//_mapData.ResetBlocks(TGS);
|
2023-12-15 18:32:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return _mapData;
|
|
|
|
|
|
}
|
|
|
|
|
|
// set
|
|
|
|
|
|
// {
|
|
|
|
|
|
// if (_mapData != value)
|
|
|
|
|
|
// {
|
|
|
|
|
|
// _mapData = value;
|
|
|
|
|
|
// if (value == null)
|
|
|
|
|
|
// {
|
|
|
|
|
|
// Map = null;
|
|
|
|
|
|
// }
|
|
|
|
|
|
// else
|
|
|
|
|
|
// {
|
|
|
|
|
|
// Map = MapUtils.LoadMap(TGS, _mapData);
|
|
|
|
|
|
// //MapUtils.BindCameraController(_camera, Map);
|
|
|
|
|
|
// //var cameraController = _camera.gameObject.GetComponent<CameraController>();
|
|
|
|
|
|
// //cameraController.InitializeCameraPosition(initialCameraPosition);
|
|
|
|
|
|
// }
|
|
|
|
|
|
// }
|
|
|
|
|
|
// }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private MapData JsonToMapData(TextAsset jsonText)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (jsonText != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
var jsonStr = jsonText.text;
|
|
|
|
|
|
if (string.IsNullOrEmpty(jsonStr))
|
|
|
|
|
|
{
|
|
|
|
|
|
var mapData = new MapData();
|
2023-12-18 19:42:46 +08:00
|
|
|
|
//mapData.ResetBlocks();
|
2023-12-15 18:32:21 +08:00
|
|
|
|
return mapData;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2023-12-18 19:42:46 +08:00
|
|
|
|
//newtowsoft的Json序列化JsonIgnore不起作用,先用Unity的
|
|
|
|
|
|
//return JsonConvert.DeserializeObject<MapData>(jsonStr);
|
|
|
|
|
|
return JsonUtility.FromJson<MapData>(jsonStr);
|
2023-12-15 18:32:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void SaveMapData()
|
|
|
|
|
|
{
|
|
|
|
|
|
#if UNITY_EDITOR
|
|
|
|
|
|
if (_mapDataJson != null && !Application.isPlaying)
|
|
|
|
|
|
{
|
|
|
|
|
|
var path = AssetDatabase.GetAssetPath(_mapDataJson);
|
2023-12-18 19:42:46 +08:00
|
|
|
|
//newtowsoft的Json序列化JsonIgnore不起作用,先用Unity的
|
|
|
|
|
|
//JsonHelper.SaveJson(path, _mapData);
|
|
|
|
|
|
var jsonStr = JsonUtility.ToJson(_mapData,true);
|
|
|
|
|
|
File.WriteAllText(path, jsonStr);
|
2023-12-15 18:32:21 +08:00
|
|
|
|
Debug.Log($"保存地图数据成功!");
|
|
|
|
|
|
AssetDatabase.Refresh();
|
|
|
|
|
|
}
|
|
|
|
|
|
#endif
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void Awake()
|
|
|
|
|
|
{
|
|
|
|
|
|
Refresh();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void OnDestroy()
|
|
|
|
|
|
{
|
|
|
|
|
|
SaveMapData();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|