EerieVillage/Assets/Editor/Map/MapResourceRegistryWindow.cs

431 lines
18 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEditorInternal;
using UnityEngine;
using UnityEngine.Tilemaps;
using EerieVillage.Map.Data;
namespace EerieVillage.Map.EditorScripts
{
/// <summary>
/// BT13-Dev-Map — 맵 리소스 등록 EditorWindow (전면 재작성 2026-05-19).
/// PD 지시 (2026-05-19): 기존 A~F 섹션 전부 폐기. 신규 4 섹션:
/// A. 배경 이미지 등록 (Sprite ObjectField + Preview + Set/Clear)
/// B. 지면 타일 9방향 (3x3 grid · Sprite 드래그 시 Tile 자동 생성)
/// C. 공중 발판 2종 (platformShort·platformLong · Sprite 드래그 시 Tile 자동 생성)
/// D. 게임 오브젝트 (Player·Monster List ReorderableList·Boss)
/// 모든 weight·tags·이름 입력 제거.
/// 이전 본문 백업: 공유/개발팀_백업/EerieVillage/BT13-Dev-Map_20260519/MapResourceRegistryWindow.bak_20260519_0050.cs
/// 메뉴: EerieVillage/Map/Resource Registry
/// </summary>
public class MapResourceRegistryWindow : EditorWindow
{
const string CATALOG_PATH = "Assets/Resources/Map/MapResourceCatalog.asset";
const string TILES_AUTO_PATH = "Assets/Tiles/Auto";
MapResourceCatalog _catalog;
Vector2 _scrollPos;
SerializedObject _serialized;
ReorderableList _monsterList;
[MenuItem("EerieVillage/Map/Resource Registry")]
public static void Open()
{
var window = GetWindow<MapResourceRegistryWindow>("Map Resource Registry");
window.minSize = new Vector2(520, 700);
window.Show();
}
void OnEnable()
{
LoadCatalog();
}
void LoadCatalog()
{
_catalog = AssetDatabase.LoadAssetAtPath<MapResourceCatalog>(CATALOG_PATH);
if (_catalog != null)
{
_serialized = new SerializedObject(_catalog);
SetupMonsterList();
}
else
{
_serialized = null;
_monsterList = null;
}
}
void SetupMonsterList()
{
var monstersProp = _serialized.FindProperty("monsters");
_monsterList = new ReorderableList(_serialized, monstersProp, true, true, true, true);
_monsterList.drawHeaderCallback = rect =>
{
EditorGUI.LabelField(rect, "Monsters (random pick · 추가·삭제·재정렬 가능)");
};
_monsterList.drawElementCallback = (rect, index, active, focused) =>
{
var element = monstersProp.GetArrayElementAtIndex(index);
rect.y += 2;
rect.height = EditorGUIUtility.singleLineHeight;
EditorGUI.PropertyField(rect, element, new GUIContent($"[{index}]"));
};
_monsterList.elementHeight = EditorGUIUtility.singleLineHeight + 6;
}
void OnGUI()
{
_scrollPos = EditorGUILayout.BeginScrollView(_scrollPos);
DrawHeader();
if (_catalog == null)
{
EditorGUILayout.HelpBox(
"MapResourceCatalog 미발견.\n" +
"Assets/Resources/Map/MapResourceCatalog.asset 가 존재하지 않습니다.\n" +
"메뉴: EerieVillage/Map/Bootstrap Placeholder Assets 1회 실행으로 자동 생성.",
MessageType.Warning);
if (GUILayout.Button("Bootstrap 메뉴 실행 (MapAssetBootstrap)", GUILayout.Height(28)))
{
EditorApplication.ExecuteMenuItem("EerieVillage/Map/Bootstrap Placeholder Assets");
LoadCatalog();
}
if (GUILayout.Button("Catalog 다시 로드", GUILayout.Height(24)))
{
LoadCatalog();
}
EditorGUILayout.EndScrollView();
return;
}
_serialized.Update();
DrawSectionA_BackgroundSprite();
EditorGUILayout.Space(12);
DrawSectionB_GroundTiles9Grid();
EditorGUILayout.Space(12);
DrawSectionC_AirPlatforms();
EditorGUILayout.Space(12);
DrawSectionD_GameObjects();
EditorGUILayout.Space(8);
DrawFooter();
_serialized.ApplyModifiedProperties();
EditorGUILayout.EndScrollView();
}
void DrawHeader()
{
EditorGUILayout.LabelField("Map Resource Registry", EditorStyles.boldLabel);
EditorGUILayout.LabelField(
"PD 가 4 섹션 (배경 이미지·지면 9방향·공중 발판 2종·게임 오브젝트) 등록.",
EditorStyles.miniLabel);
EditorGUILayout.LabelField(
$"Catalog: {(_catalog == null ? "<미발견>" : CATALOG_PATH)}",
EditorStyles.miniLabel);
EditorGUILayout.Space(6);
}
void DrawFooter()
{
EditorGUILayout.Space(4);
if (GUILayout.Button("Catalog 다시 로드 (Refresh)", GUILayout.Height(22)))
{
LoadCatalog();
}
if (GUILayout.Button("Catalog Inspector 에서 보기", GUILayout.Height(22)))
{
Selection.activeObject = _catalog;
EditorGUIUtility.PingObject(_catalog);
}
}
// ====================================================================
// A. 배경 이미지 등록
// ====================================================================
void DrawSectionA_BackgroundSprite()
{
EditorGUILayout.LabelField("A. 배경 이미지 등록", EditorStyles.boldLabel);
using (new EditorGUILayout.VerticalScope(GUI.skin.box))
{
EditorGUILayout.HelpBox(
"맵 최하위 레이어에 SpriteRenderer.drawMode=Tiled · sortingOrder=-1000 으로 반복 배치.",
MessageType.Info);
var newSprite = (Sprite)EditorGUILayout.ObjectField(
"Background Sprite", _catalog.backgroundSprite, typeof(Sprite), false);
if (newSprite != _catalog.backgroundSprite)
{
Undo.RecordObject(_catalog, "Set backgroundSprite");
_catalog.backgroundSprite = newSprite;
EditorUtility.SetDirty(_catalog);
AssetDatabase.SaveAssets();
Debug.Log($"[MapResourceRegistry] backgroundSprite ← {(newSprite == null ? "null" : newSprite.name)}");
}
using (new EditorGUILayout.HorizontalScope())
{
if (GUILayout.Button("Clear", GUILayout.Width(80)))
{
Undo.RecordObject(_catalog, "Clear backgroundSprite");
_catalog.backgroundSprite = null;
EditorUtility.SetDirty(_catalog);
AssetDatabase.SaveAssets();
}
}
if (_catalog.backgroundSprite != null)
{
var tex = _catalog.backgroundSprite.texture;
if (tex != null)
{
EditorGUILayout.Space(2);
using (new EditorGUILayout.HorizontalScope(GUI.skin.box))
{
var previewRect = GUILayoutUtility.GetRect(120, 60, GUILayout.Width(120), GUILayout.Height(60));
EditorGUI.DrawPreviewTexture(previewRect, tex, null, ScaleMode.ScaleToFit);
using (new EditorGUILayout.VerticalScope())
{
EditorGUILayout.LabelField($"name: {_catalog.backgroundSprite.name}");
var r = _catalog.backgroundSprite.rect;
EditorGUILayout.LabelField($"size: {r.width:F0} × {r.height:F0} px");
EditorGUILayout.LabelField($"PPU: {_catalog.backgroundSprite.pixelsPerUnit:F0}");
}
}
}
}
}
}
// ====================================================================
// B. 지면 타일 9방향 (3x3 grid)
// ====================================================================
void DrawSectionB_GroundTiles9Grid()
{
EditorGUILayout.LabelField("B. 지면 타일 등록 (9방향 3x3 grid)", EditorStyles.boldLabel);
using (new EditorGUILayout.VerticalScope(GUI.skin.box))
{
EditorGUILayout.HelpBox(
"각 슬롯에 Sprite 드래그 → Tile asset 자동 생성 (Assets/Tiles/Auto/Ground_<position>.asset · m_ColliderType=Sprite) + Catalog 자동 link.\n" +
"이번 임시 작업 — 센터(Center) 1종만 사용. 향후 9-slice 자동 분기 후속 안건.",
MessageType.Info);
// Row 1
using (new EditorGUILayout.HorizontalScope())
{
DrawGroundSlot("TopLeft", "groundTopLeft");
DrawGroundSlot("Top", "groundTop");
DrawGroundSlot("TopRight", "groundTopRight");
}
// Row 2
using (new EditorGUILayout.HorizontalScope())
{
DrawGroundSlot("Left", "groundLeft");
DrawGroundSlot("Center", "groundCenter");
DrawGroundSlot("Right", "groundRight");
}
// Row 3
using (new EditorGUILayout.HorizontalScope())
{
DrawGroundSlot("BottomLeft", "groundBottomLeft");
DrawGroundSlot("Bottom", "groundBottom");
DrawGroundSlot("BottomRight", "groundBottomRight");
}
}
}
void DrawGroundSlot(string label, string propName)
{
using (new EditorGUILayout.VerticalScope(GUI.skin.box, GUILayout.Width(155)))
{
EditorGUILayout.LabelField(label, EditorStyles.miniBoldLabel);
var prop = _serialized.FindProperty(propName);
TileBase current = prop.objectReferenceValue as TileBase;
DrawSpriteOrTileSlot(prop, $"Ground_{label}", current);
}
}
// ====================================================================
// C. 공중 발판 2종
// ====================================================================
void DrawSectionC_AirPlatforms()
{
EditorGUILayout.LabelField("C. 공중 발판 등록 (2종)", EditorStyles.boldLabel);
using (new EditorGUILayout.VerticalScope(GUI.skin.box))
{
EditorGUILayout.HelpBox(
"platformShort = 짧은 발판 (AirGround_Type1 권장) · platformLong = 넓고 긴 발판 (AirGround_Type2 권장).\n" +
"Sprite 드래그 시 Tile asset 자동 생성 + Catalog link.",
MessageType.Info);
using (new EditorGUILayout.HorizontalScope())
{
using (new EditorGUILayout.VerticalScope(GUI.skin.box))
{
EditorGUILayout.LabelField("platformShort", EditorStyles.miniBoldLabel);
var prop = _serialized.FindProperty("platformShort");
DrawSpriteOrTileSlot(prop, "Platform_Short", prop.objectReferenceValue as TileBase);
}
using (new EditorGUILayout.VerticalScope(GUI.skin.box))
{
EditorGUILayout.LabelField("platformLong", EditorStyles.miniBoldLabel);
var prop = _serialized.FindProperty("platformLong");
DrawSpriteOrTileSlot(prop, "Platform_Long", prop.objectReferenceValue as TileBase);
}
}
}
}
// ====================================================================
// D. 게임 오브젝트 (Player·Monsters·Boss)
// ====================================================================
void DrawSectionD_GameObjects()
{
EditorGUILayout.LabelField("D. 게임 오브젝트 등록", EditorStyles.boldLabel);
// Player
using (new EditorGUILayout.VerticalScope(GUI.skin.box))
{
EditorGUILayout.LabelField("Player Prefab", EditorStyles.miniBoldLabel);
var prop = _serialized.FindProperty("playerPrefab");
EditorGUILayout.PropertyField(prop, GUIContent.none);
using (new EditorGUILayout.HorizontalScope())
{
if (GUILayout.Button("Clear", GUILayout.Width(80)))
{
prop.objectReferenceValue = null;
}
}
}
EditorGUILayout.Space(4);
// Monsters (ReorderableList)
using (new EditorGUILayout.VerticalScope(GUI.skin.box))
{
if (_monsterList != null)
{
_monsterList.DoLayoutList();
}
else
{
EditorGUILayout.HelpBox("monster list 초기화 실패 — Catalog 다시 로드.", MessageType.Warning);
}
}
EditorGUILayout.Space(4);
// Boss
using (new EditorGUILayout.VerticalScope(GUI.skin.box))
{
EditorGUILayout.LabelField("Boss Prefab", EditorStyles.miniBoldLabel);
var prop = _serialized.FindProperty("bossPrefab");
EditorGUILayout.PropertyField(prop, GUIContent.none);
using (new EditorGUILayout.HorizontalScope())
{
if (GUILayout.Button("Clear", GUILayout.Width(80)))
{
prop.objectReferenceValue = null;
}
}
}
}
// ====================================================================
// 공통 — Sprite 드래그 시 Tile asset 자동 생성·갱신
// ====================================================================
/// <summary>
/// 슬롯에 Sprite 또는 TileBase 드래그 가능.
/// - Sprite 드래그: Assets/Tiles/Auto/{tileBaseName}.asset 자동 생성 (m_ColliderType=Sprite) + Catalog link.
/// - TileBase 직접 드래그: 그대로 link.
/// - null: link 해제.
/// </summary>
void DrawSpriteOrTileSlot(SerializedProperty prop, string tileBaseName, TileBase currentTile)
{
// 현재 Tile 의 sprite 노출 — Sprite 드래그를 우선 받음
Sprite currentSprite = null;
if (currentTile is Tile t) currentSprite = t.sprite;
// ObjectField — Sprite·Tile 모두 받기 위해 Object 받기
var dropped = EditorGUILayout.ObjectField(
"Sprite", currentSprite, typeof(Sprite), false) as Sprite;
if (dropped != currentSprite)
{
if (dropped == null)
{
// 슬롯 비우기
prop.objectReferenceValue = null;
_serialized.ApplyModifiedProperties();
Debug.Log($"[MapResourceRegistry] {prop.name} ← null");
}
else
{
// Sprite 드래그 → Tile 자동 생성·갱신
EnsureFolder(TILES_AUTO_PATH);
string tilePath = $"{TILES_AUTO_PATH}/{tileBaseName}.asset";
var existing = AssetDatabase.LoadAssetAtPath<TileBase>(tilePath);
Tile tile;
if (existing is Tile et)
{
tile = et;
tile.sprite = dropped;
tile.colliderType = Tile.ColliderType.Sprite;
EditorUtility.SetDirty(tile);
Debug.Log($"[MapResourceRegistry] Tile 갱신 — {tilePath} (sprite={dropped.name})");
}
else
{
tile = ScriptableObject.CreateInstance<Tile>();
tile.sprite = dropped;
tile.colliderType = Tile.ColliderType.Sprite;
AssetDatabase.CreateAsset(tile, tilePath);
Debug.Log($"[MapResourceRegistry] Tile 생성 — {tilePath} (sprite={dropped.name} · m_ColliderType=Sprite)");
}
AssetDatabase.SaveAssets();
prop.objectReferenceValue = tile;
_serialized.ApplyModifiedProperties();
}
}
// 현재 link 상태 노출
if (currentTile != null)
{
EditorGUILayout.LabelField($"Tile: {currentTile.name}", EditorStyles.miniLabel);
if (currentSprite != null && currentSprite.texture != null)
{
var rect = GUILayoutUtility.GetRect(140, 60, GUILayout.Width(140), GUILayout.Height(60));
EditorGUI.DrawPreviewTexture(rect, currentSprite.texture, null, ScaleMode.ScaleToFit);
}
}
else
{
var oldColor = GUI.color;
GUI.color = new Color(1f, 0.6f, 0.4f);
EditorGUILayout.LabelField("X 미등록", EditorStyles.miniLabel);
GUI.color = oldColor;
}
}
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);
}
}
}