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
53 lines
1.6 KiB
C#
53 lines
1.6 KiB
C#
using UnityEditor;
|
|
using UnityEngine.Tilemaps;
|
|
using UnityEngine;
|
|
using System.IO;
|
|
|
|
namespace GatherAndDefend.LevelEditor
|
|
{
|
|
[CustomEditor(typeof(LevelEditor))]
|
|
public class LevelEditorEditor : Editor
|
|
{
|
|
const string defaultName = "Level";
|
|
const string extension = ".asset";
|
|
string Path => "Assets/";
|
|
public override void OnInspectorGUI()
|
|
{
|
|
DrawDefaultInspector();
|
|
|
|
var targ = (LevelEditor)target;
|
|
if (GUILayout.Button("Save"))
|
|
{
|
|
string name;
|
|
if (targ.Level) name = targ.Level.name;
|
|
else name = defaultName;
|
|
if (File.Exists(Path + name + extension))
|
|
{
|
|
File.Delete(Path + name + extension);
|
|
}
|
|
|
|
var map = CreateInstance<Map>();
|
|
map.name = name;
|
|
|
|
foreach (Tilemap tilemap in targ.GetComponentsInChildren<Tilemap>())
|
|
{
|
|
map.Write(tilemap);
|
|
}
|
|
|
|
AssetDatabase.CreateAsset(map, Path + name + extension);
|
|
targ.Level = map;
|
|
AssetDatabase.ImportAsset(Path + name + extension);
|
|
}
|
|
if (GUILayout.Button("Load"))
|
|
{
|
|
if (!targ.Level) return;
|
|
|
|
foreach (Tilemap tilemap in targ.GetComponentsInChildren<Tilemap>())
|
|
{
|
|
tilemap.ClearAllTiles();
|
|
targ.Level.Read(tilemap);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} |