sauvegarder et charger des niveaux à partir d'un tilemap un scriptable object contient les informations pour le niveau un script avec un custom editor permet de sauvegarder un ensemble de tilemaps, ou de charger l'information contenue dans un scriptable object
47 lines
1.3 KiB
C#
47 lines
1.3 KiB
C#
using System.Collections.Generic;
|
|
using UnityEngine.Tilemaps;
|
|
using UnityEngine;
|
|
using System;
|
|
|
|
namespace GatherAndDefend.LevelEditor
|
|
{
|
|
[Serializable]
|
|
public class TilemapData
|
|
{
|
|
[SerializeField]
|
|
private string _key;
|
|
[SerializeField]
|
|
private List<TileData> tiles;
|
|
|
|
public string Key => _key;
|
|
|
|
public void Read(Tilemap reference)
|
|
{
|
|
foreach (TileData data in tiles)
|
|
{
|
|
reference.SetTile(data.Position, data.Tile);
|
|
}
|
|
}
|
|
public void Write(Tilemap reference)
|
|
{
|
|
_key = reference.name;
|
|
tiles = new List<TileData>();
|
|
BoundsInt bounds = reference.cellBounds;
|
|
for (int i = bounds.xMin; i <= bounds.xMax; i++)
|
|
{
|
|
for (int j = bounds.yMin; j <= bounds.yMax; j++)
|
|
{
|
|
Vector3Int position = new Vector3Int(i, j);
|
|
TileBase tile = reference.GetTile(position);
|
|
if (!tile) continue;
|
|
var tileData = new TileData(position, tile);
|
|
tiles.Add(tileData);
|
|
}
|
|
}
|
|
}
|
|
public TilemapData()
|
|
{
|
|
tiles = new List<TileData>();
|
|
}
|
|
}
|
|
} |