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.name = name; foreach (Tilemap tilemap in targ.GetComponentsInChildren()) { 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.ClearAllTiles(); targ.Level.Read(tilemap); } } } } }