using UnityEngine;
using UnityEditor;
using UnityEngine.Tilemaps;
using System.IO;
using EerieVillage.Map.Data;
namespace EerieVillage.Map.EditorScripts
{
///
/// BT13-Dev-Map — placeholder asset 일회 자동 생성 + Catalog 자동 link.
/// PD 지시 (2026-05-19): 4 신규 자동 link — backgroundSprite·playerPrefab·groundCenter·platformShort·platformLong·monsters.
/// ground.png·AirGround_Type*.png 의 spriteMode=Multiple 을 Single·FullRect 으로 자동 정정 후 Tile asset 자동 생성.
/// 이전 본문 백업: 공유/개발팀_백업/EerieVillage/BT13-Dev-Map_20260519/MapAssetBootstrap.bak_20260519_0050.cs
/// 메뉴: EerieVillage/Map/Bootstrap Placeholder Assets
///
public static class MapAssetBootstrap
{
const string RESOURCES_MAP_PATH = "Assets/Resources/Map";
const string TILES_AUTO_PATH = "Assets/Tiles/Auto";
[MenuItem("EerieVillage/Map/Bootstrap Placeholder Assets")]
public static void Bootstrap()
{
EnsureFolder(RESOURCES_MAP_PATH);
EnsureFolder(TILES_AUTO_PATH);
// 1. MapResourceCatalog (1) — PD 가 Inspector 또는 Registry 에서 Tile·Prefab 등록
var catalogAsset = CreateAssetIfMissing($"{RESOURCES_MAP_PATH}/MapResourceCatalog.asset");
// 1-A. 전체 자동 link (PD 2026-05-19 단순화 6 항목)
// Bootstrap 1회로 모든 link 자동 등록 → PD 의 Inspector 수동 작업 불요.
bool dirty = false;
// (1) backgroundSprite ← BgImg_1.png (맵 최하위 레이어 반복 배치)
if (catalogAsset.backgroundSprite == null)
{
var s = AssetDatabase.LoadAssetAtPath($"{RESOURCES_MAP_PATH}/BgImg_1.png");
if (s != null) { catalogAsset.backgroundSprite = s; dirty = true; Debug.Log("[Bootstrap] backgroundSprite ← BgImg_1.png"); }
}
// (2) playerPrefab ← Player.prefab
if (catalogAsset.playerPrefab == null)
{
var p = AssetDatabase.LoadAssetAtPath("Assets/Prefabs/Player.prefab");
if (p != null) { catalogAsset.playerPrefab = p; dirty = true; Debug.Log("[Bootstrap] playerPrefab ← Player.prefab"); }
}
// (3) 9-slice ground + variant ← ground_Tileset.png sub-sprite (PD 2026-05-19 자연 연결)
// ground_Tileset.png (spriteMode=Multiple) 의 23 sub-sprite 영역 role 별 Tile asset 자동 생성.
// 기존 ground.png 단일 link 영역 폐기 — 9-slice 자동 분기 영역.
BootstrapGround9SliceFromTileset(catalogAsset, ref dirty);
// (4) platformShort ← AirGround_Type1.png 의 Sprite → Tile 자동 생성
if (catalogAsset.platformShort == null)
{
var t = EnsureSingleSpriteTile($"{RESOURCES_MAP_PATH}/AirGround_Type1.png", $"{TILES_AUTO_PATH}/Platform_Short.asset");
if (t != null) { catalogAsset.platformShort = t; dirty = true; Debug.Log("[Bootstrap] platformShort ← Platform_Short.asset (AirGround_Type1.png)"); }
}
// (5) platformLong ← AirGround_Type2.png 의 Sprite → Tile 자동 생성
if (catalogAsset.platformLong == null)
{
var t = EnsureSingleSpriteTile($"{RESOURCES_MAP_PATH}/AirGround_Type2.png", $"{TILES_AUTO_PATH}/Platform_Long.asset");
if (t != null) { catalogAsset.platformLong = t; dirty = true; Debug.Log("[Bootstrap] platformLong ← Platform_Long.asset (AirGround_Type2.png)"); }
}
// (6) monsters 리스트 — 비어있을 때 Enemy.prefab 자동 append
if (catalogAsset.monsters == null || catalogAsset.monsters.Count == 0)
{
var p = AssetDatabase.LoadAssetAtPath("Assets/Prefabs/Enemy.prefab");
if (p != null)
{
if (catalogAsset.monsters == null) catalogAsset.monsters = new System.Collections.Generic.List();
catalogAsset.monsters.Add(p);
dirty = true;
Debug.Log("[Bootstrap] monsters[0] ← Enemy.prefab");
}
}
// (7) bossPrefab = null 유지 (보스 미정의 시 spawn skip · MapApplier 처리)
if (dirty) EditorUtility.SetDirty(catalogAsset);
// 2. SpawnPointConfig (1) — 기본값 SO
CreateAssetIfMissing($"{RESOURCES_MAP_PATH}/SpawnPointConfig_Default.asset");
// 3. ChunkTemplate 3 (Intro·Mid·BossPre)
CreateChunkTemplate("Intro", ChunkTemplate.ChunkRole.Intro, 0.6f, 0.5f);
CreateChunkTemplate("Mid", ChunkTemplate.ChunkRole.Mid, 1.2f, 1.0f);
CreateChunkTemplate("BossPre", ChunkTemplate.ChunkRole.BossPre, 0.8f, 1.5f);
// 4. PlatformPattern (기본 1)
CreateAssetIfMissing($"{RESOURCES_MAP_PATH}/PlatformPattern_Default.asset");
// 5. 5 stage MapGenerationConfig (기획 SOT 정합 — Stage1~5·마을)
CreateStageConfig("Stage0_Village", 80, 0.3f, 0.3f);
CreateStageConfig("Stage1_DuskVillageOutskirts", 120, 0.4f, 0.5f);
CreateStageConfig("Stage2_AbandonedMountainTemple", 140, 0.5f, 0.55f);
CreateStageConfig("Stage3_BoneRockValley", 160, 0.55f, 0.6f);
CreateStageConfig("Stage4_AbandonedHouseBasement", 160, 0.6f, 0.65f);
CreateStageConfig("Stage5_GraveyardDepths", 180, 0.65f, 0.7f);
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
Debug.Log("[MapAssetBootstrap] Placeholder asset 생성 + Catalog 자동 link 완료 (PD 2026-05-19 단순화 6 항목).\n" +
"PD: Bootstrap 1회 실행 후 즉시 Generate·Apply 가능.");
}
///
/// PD 2026-05-19 — ground_Tileset.png (spriteMode=Multiple) 의 23 sub-sprite 자동 link.
/// AssetDatabase.LoadAllAssetsAtPath → Sprite[] → name parsing → Tile asset 생성 → Catalog 9 slot + variant 채움.
/// sub-sprite name 형식: ground_Tileset__ (예: ground_Tileset_7_Center → role=Center).
///
static void BootstrapGround9SliceFromTileset(MapResourceCatalog catalog, ref bool dirty)
{
const string tilesetPath = RESOURCES_MAP_PATH + "/ground_Tileset.png";
if (!File.Exists(tilesetPath))
{
Debug.LogWarning($"[Bootstrap] ground_Tileset.png 미발견 — {tilesetPath}");
return;
}
var allAssets = AssetDatabase.LoadAllAssetsAtPath(tilesetPath);
var spriteByRole = new System.Collections.Generic.Dictionary();
foreach (var a in allAssets)
{
if (a is Sprite spr)
{
// ground_Tileset__ → role 추출 (_ 2회 split 후 나머지 결합)
var parts = spr.name.Split('_');
if (parts.Length >= 4)
{
string role = string.Join("_", parts, 3, parts.Length - 3);
spriteByRole[role] = spr;
}
}
}
if (spriteByRole.Count == 0)
{
Debug.LogWarning($"[Bootstrap] ground_Tileset.png sub-sprite 미발견 — Reimport 필요 (spriteMode=Multiple)");
return;
}
// 9 slot 자동 link
if (catalog.groundTopLeft == null) { var t = CreateTileFromSprite(spriteByRole, "TopLeft"); if (t != null) { catalog.groundTopLeft = t; dirty = true; } }
if (catalog.groundTopRight == null) { var t = CreateTileFromSprite(spriteByRole, "TopRight"); if (t != null) { catalog.groundTopRight = t; dirty = true; } }
if (catalog.groundTop == null) { var t = CreateTileFromSprite(spriteByRole, "Top_A"); if (t != null) { catalog.groundTop = t; dirty = true; } }
if (catalog.groundLeft == null) { var t = CreateTileFromSprite(spriteByRole, "Left"); if (t != null) { catalog.groundLeft = t; dirty = true; } }
if (catalog.groundCenter == null) { var t = CreateTileFromSprite(spriteByRole, "Center"); if (t != null) { catalog.groundCenter = t; dirty = true; } }
if (catalog.groundRight == null) { var t = CreateTileFromSprite(spriteByRole, "Right"); if (t != null) { catalog.groundRight = t; dirty = true; } }
if (catalog.groundBottomLeft == null) { var t = CreateTileFromSprite(spriteByRole, "BottomLeft"); if (t != null) { catalog.groundBottomLeft = t; dirty = true; } }
if (catalog.groundBottom == null) { var t = CreateTileFromSprite(spriteByRole, "Bottom_A"); if (t != null) { catalog.groundBottom = t; dirty = true; } }
if (catalog.groundBottomRight == null) { var t = CreateTileFromSprite(spriteByRole, "BottomRight"); if (t != null) { catalog.groundBottomRight = t; dirty = true; } }
// Top variant random (Top_A·Top_B·Top_C)
if (catalog.groundTopVariants == null || catalog.groundTopVariants.Length == 0)
{
var arr = new TileBase[3];
arr[0] = CreateTileFromSprite(spriteByRole, "Top_A");
arr[1] = CreateTileFromSprite(spriteByRole, "Top_B");
arr[2] = CreateTileFromSprite(spriteByRole, "Top_C");
if (arr[0] != null || arr[1] != null || arr[2] != null)
{
catalog.groundTopVariants = arr;
dirty = true;
}
}
// Bottom variant random (Bottom_A·Bottom_B·Bottom_C)
if (catalog.groundBottomVariants == null || catalog.groundBottomVariants.Length == 0)
{
var arr = new TileBase[3];
arr[0] = CreateTileFromSprite(spriteByRole, "Bottom_A");
arr[1] = CreateTileFromSprite(spriteByRole, "Bottom_B");
arr[2] = CreateTileFromSprite(spriteByRole, "Bottom_C");
if (arr[0] != null || arr[1] != null || arr[2] != null)
{
catalog.groundBottomVariants = arr;
dirty = true;
}
}
Debug.Log($"[Bootstrap] ground_Tileset 9-slice link 완료 — role count={spriteByRole.Count}, variant Top={catalog.groundTopVariants?.Length ?? 0}, Bottom={catalog.groundBottomVariants?.Length ?? 0}");
}
/// role 기반 Tile asset 생성 (Tiles/Auto/Ground_.asset).
static TileBase CreateTileFromSprite(System.Collections.Generic.Dictionary spriteByRole, string role)
{
if (!spriteByRole.TryGetValue(role, out var spr) || spr == null) return null;
string tilePath = $"{TILES_AUTO_PATH}/Ground_{role}.asset";
var existing = AssetDatabase.LoadAssetAtPath(tilePath);
if (existing is Tile et)
{
if (et.sprite != spr || et.colliderType != Tile.ColliderType.Sprite)
{
et.sprite = spr;
et.colliderType = Tile.ColliderType.Sprite;
EditorUtility.SetDirty(et);
}
return et;
}
var tile = ScriptableObject.CreateInstance();
tile.sprite = spr;
tile.colliderType = Tile.ColliderType.Sprite;
AssetDatabase.CreateAsset(tile, tilePath);
return tile;
}
///
/// PNG 의 spriteMode 를 Single·FullRect 로 정정 후 Sprite 를 로드, Tile asset 자동 생성·갱신.
/// PD 자산 (AirGround_Type1·AirGround_Type2) 이 spriteMode=Multiple 인 경우 자동 정정.
/// (ground.png 단일 link 폐기 — BootstrapGround9SliceFromTileset 영역 대체.)
///
static TileBase EnsureSingleSpriteTile(string pngPath, string tilePath)
{
// 1. PNG 존재 검증
if (!File.Exists(pngPath))
{
Debug.LogWarning($"[Bootstrap] PNG 미발견 — {pngPath}");
return null;
}
// 2. TextureImporter 검사 — spriteMode=Single·FullRect 강제 (TextureImporterSettings 영역)
var importer = AssetImporter.GetAtPath(pngPath) as TextureImporter;
if (importer != null)
{
var settings = new TextureImporterSettings();
importer.ReadTextureSettings(settings);
bool importerDirty = false;
if (settings.textureType != TextureImporterType.Sprite)
{
settings.textureType = TextureImporterType.Sprite;
importerDirty = true;
}
if (settings.spriteMode != (int)SpriteImportMode.Single)
{
settings.spriteMode = (int)SpriteImportMode.Single;
importerDirty = true;
}
if (settings.spriteMeshType != SpriteMeshType.FullRect)
{
settings.spriteMeshType = SpriteMeshType.FullRect;
importerDirty = true;
}
if (importerDirty)
{
importer.SetTextureSettings(settings);
importer.SaveAndReimport();
Debug.Log($"[Bootstrap] {pngPath} → textureType=Sprite · spriteMode=Single · FullRect 자동 정정");
}
}
// 3. Sprite 로드
var sprite = AssetDatabase.LoadAssetAtPath(pngPath);
if (sprite == null)
{
Debug.LogWarning($"[Bootstrap] Sprite 로드 실패 — {pngPath}");
return null;
}
// 4. Tile asset 생성 또는 갱신 (m_ColliderType=Sprite)
var existing = AssetDatabase.LoadAssetAtPath(tilePath);
Tile tile;
if (existing is Tile et)
{
tile = et;
if (tile.sprite != sprite || tile.colliderType != Tile.ColliderType.Sprite)
{
tile.sprite = sprite;
tile.colliderType = Tile.ColliderType.Sprite;
EditorUtility.SetDirty(tile);
Debug.Log($"[Bootstrap] Tile 갱신 — {tilePath}");
}
}
else
{
tile = ScriptableObject.CreateInstance();
tile.sprite = sprite;
tile.colliderType = Tile.ColliderType.Sprite;
AssetDatabase.CreateAsset(tile, tilePath);
Debug.Log($"[Bootstrap] Tile 생성 — {tilePath} (sprite={sprite.name} · m_ColliderType=Sprite)");
}
return tile;
}
static void EnsureFolder(string path)
{
if (AssetDatabase.IsValidFolder(path)) return;
string parent = Path.GetDirectoryName(path).Replace('\\', '/');
string folderName = Path.GetFileName(path);
if (!AssetDatabase.IsValidFolder(parent)) EnsureFolder(parent);
AssetDatabase.CreateFolder(parent, folderName);
}
static T CreateAssetIfMissing(string path) where T : ScriptableObject
{
var existing = AssetDatabase.LoadAssetAtPath(path);
if (existing != null) return existing;
var asset = ScriptableObject.CreateInstance();
AssetDatabase.CreateAsset(asset, path);
Debug.Log($"[MapAssetBootstrap] Created {path}");
return asset;
}
static void CreateChunkTemplate(string name, ChunkTemplate.ChunkRole role, float platMul, float spawnMul)
{
string path = $"{RESOURCES_MAP_PATH}/Chunk_{name}.asset";
if (AssetDatabase.LoadAssetAtPath(path) != null) return;
var asset = ScriptableObject.CreateInstance();
asset.role = role;
asset.platformDensityMultiplier = platMul;
asset.spawnDensityMultiplier = spawnMul;
asset.chunkSizeTiles = 20;
AssetDatabase.CreateAsset(asset, path);
Debug.Log($"[MapAssetBootstrap] Created {path}");
}
static void CreateStageConfig(string stageName, int length, float spawnDensity, float platformDensity)
{
string path = $"{RESOURCES_MAP_PATH}/MapConfig_{stageName}.asset";
if (AssetDatabase.LoadAssetAtPath(path) != null) return;
var asset = ScriptableObject.CreateInstance();
asset.lengthTiles = length;
asset.spawnDensity = spawnDensity;
asset.platformDensity = platformDensity;
asset.minSpawnSpacingTiles = 12;
asset.maxSpawnPerMap = 16;
asset.minPlatformGapTiles = 3;
asset.maxPlatformGapTiles = 10;
asset.minPlatformWidthTiles = 2;
asset.maxPlatformWidthTiles = 6;
asset.minPlatformHeight = 2;
asset.maxPlatformHeight = 10;
asset.startMarginTiles = 4;
asset.goalMarginTiles = 4;
asset.chunkSizeTiles = 20;
asset.introPlatformDensityMultiplier = 0.6f;
asset.midPlatformDensityMultiplier = 1.2f;
asset.bossPrePlatformDensityMultiplier = 0.8f;
asset.groundBaseY = 0;
asset.groundPerlinAmplitude = 2;
asset.groundPerlinScale = 0.1f;
asset.flyingEnemyRatio = 0.15f;
asset.allowFloatingSpawnPoints = true;
asset.respawnTimer = 5f;
asset.respawnMaxCount = -1;
asset.bossRoomEnabled = true;
asset.bossRoomLengthTiles = 20;
var catalog = AssetDatabase.LoadAssetAtPath($"{RESOURCES_MAP_PATH}/MapResourceCatalog.asset");
if (catalog != null) asset.catalog = catalog;
AssetDatabase.CreateAsset(asset, path);
Debug.Log($"[MapAssetBootstrap] Created {path}");
}
}
}