IndieGame/client/Assets/Scripts/Map/Logic/GridMap.cs
DOBEST\zhaoyingjie f242607587 初始化工程
2024-10-11 10:12:15 +08:00

82 lines
2.2 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.Tilemaps;
namespace Ether
{
[ExecuteInEditMode]
public class GridMap : MonoBehaviour
{
public MapData_SO mapData;
public GridType gridType;
private Tilemap tileMap;
private void OnEnable()
{
if (!Application.IsPlaying(this))
{
tileMap = GetComponent<Tilemap>();
if (mapData != null)
mapData.tileProperties.Clear();
}
}
private void OnDisable()
{
if (!Application.IsPlaying(this))
{
tileMap = GetComponent<Tilemap>();
UpdateTileProperties();
#if UNITY_EDITOR
if (mapData != null)
EditorUtility.SetDirty(mapData);
#endif
}
}
private void UpdateTileProperties()
{
tileMap.CompressBounds();
if (!Application.IsPlaying(this))
{
if (mapData != null)
{
//已绘制范围的左下角坐标
Vector3Int startPos = tileMap.cellBounds.min;
//已绘制范围的右上角坐标
Vector3Int endPos = tileMap.cellBounds.max;
for (int x = startPos.x; x < endPos.x; x++)
{
for (int y = startPos.y; y < endPos.y; y++)
{
TileBase tile = tileMap.GetTile(new Vector3Int(x, y, 0));
if (tile != null)
{
TileProperty newTile = new TileProperty
{
tileCoordinate = new Vector2Int(x, y),
gridType = this.gridType,
boolTypeValue = true
};
mapData.tileProperties.Add(newTile);
}
}
}
}
}
}
}
}