ComponentList 완료, Context 메뉴 추가

This commit is contained in:
김형인
2025-08-09 01:39:24 +09:00
parent 165c3a709f
commit bf10b6f94a
22 changed files with 1428 additions and 42 deletions

View File

@@ -0,0 +1,46 @@
#nullable enable
using System;
using UVC.UI.Commands;
namespace UVC.UI.Menu
{
/// <summary>
/// 컨텍스트 메뉴의 각 항목에 대한 정보를 담는 데이터 구조체입니다.
/// 인스펙터에서 메뉴 항목을 쉽게 설정할 수 있도록 Serializable 특성을 가집니다.
/// </summary>
[Serializable]
public class ContextMenuItemData
{
/// <summary>
/// 메뉴 아이템의 고유 식별자입니다.
/// </summary>
public string ItemId { get; private set; }
/// <summary>
/// UI에 표시될 메뉴 아이템의 이름입니다. 다국어 키도 가능합니다.
/// 이 키를 사용하여 실제 표시될 텍스트를 가져옵니다.
/// </summary>
public string DisplayName { get; private set; }
/// <summary>
/// 메뉴 아이템이 선택되었을 때 실행될 명령입니다.
/// `ICommand` 인터페이스를 구현하는 객체여야 합니다.
/// 실행할 동작이 없는 경우 null일 수 있습니다.
/// </summary>
public ICommand Command { get; private set; }
/// <summary>
/// <see cref="Command"/> 실행 시 전달될 파라미터입니다.
/// 이 파라미터는 <see cref="ICommand.Execute"/> 호출 시 사용됩니다.
/// </summary>
public object? CommandParameter { get; set; }
public ContextMenuItemData(string ItemId, string DisplayName, ICommand Command, string CommandParameter = null)
{
this.ItemId = ItemId;
this.DisplayName = DisplayName;
this.Command = Command;
this.CommandParameter = CommandParameter;
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 26a2492ee29d41140ac2268b2cd2195b

View File

@@ -0,0 +1,259 @@
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
using UVC.Core; // SingletonScene을 사용하기 위함
using UVC.Locale;
using UVC.Pool; // GameObjectPool을 사용하기 위함
namespace UVC.UI.Menu
{
/// <summary>
/// 컨텍스트 메뉴의 표시와 관리를 총괄하는 싱글톤 클래스입니다.
/// SingletonScene을 상속받아 해당 씬 내에서 유일한 인스턴스를 보장합니다.
/// 이 매니저는 코드 기반으로 동적으로 메뉴를 생성하고 표시하는 데 사용됩니다.
/// </summary>
/// <example>
/// <b>사용 예시: 플레이어 캐릭터를 클릭했을 때 상호작용 메뉴를 표시하는 경우</b>
/// <code>
/// // 1. 메뉴 항목을 클릭했을 때 실행될 동작을 정의하는 Command 클래스를 구현합니다.
/// public class LogCommand : ICommand
/// {
/// public void Execute(object parameter)
/// {
/// if (parameter != null)
/// {
/// Debug.Log("명령 실행: " + parameter.ToString());
/// }
/// else
/// {
/// Debug.Log("명령이 파라미터 없이 실행되었습니다.");
/// }
/// }
/// }
///
/// // 2. 메뉴를 표시할 스크립트(예: PlayerInteraction.cs)를 작성합니다.
/// public class PlayerInteraction : MonoBehaviour
/// {
/// void Update()
/// {
/// // 이 게임 오브젝트가 마우스 오른쪽 버튼으로 클릭되면 메뉴를 표시합니다.
/// if (Input.GetMouseButtonDown(1))
/// {
/// // Raycast 등으로 이 오브젝트가 클릭되었는지 확인하는 로직이 필요합니다.
/// // 여기서는 설명을 위해 바로 호출합니다.
/// ShowPlayerMenu(Input.mousePosition);
/// }
/// }
///
/// public void ShowPlayerMenu(Vector2 screenPosition)
/// {
/// // 3. 메뉴에 표시할 항목 리스트를 동적으로 생성합니다.
/// var menuItems = new List<ContextMenuItemData>
/// {
/// // ContextMenuItemData 생성자를 사용하여 각 메뉴 항목을 정의합니다.
/// // 생성자: (itemId, displayName, command, commandParameter)
/// new ContextMenuItemData("player_attack", "공격", new LogCommand(), "플레이어가 '공격'을 선택했습니다."),
/// new ContextMenuItemData("player_talk", "대화", new LogCommand(), "플레이어가 '대화'를 선택했습니다."),
/// new ContextMenuItemData("player_inspect", "조사", new LogCommand(), "플레이어가 '조사'를 선택했습니다.")
/// };
///
/// // 4. ContextMenuManager 싱글톤 인스턴스를 통해 메뉴를 표시합니다.
/// ContextMenuManager.Instance.ShowMenu(menuItems, screenPosition);
/// }
/// }
/// </code>
/// <b>참고:</b>
/// - `ContextMenuTrigger` 컴포넌트를 사용하면 Inspector에서 정적으로 메뉴를 설정할 수도 있습니다.
/// - `DisplayName`에는 다국어 처리를 위한 키 값을 사용할 수 있습니다. (예: "menu_attack")
/// </example>
public class ContextMenuManager : SingletonScene<ContextMenuManager>
{
[Header("프리팹 설정")]
[SerializeField]
[Tooltip("컨텍스트 메뉴의 배경이 되는 Panel 프리팹입니다.")]
private GameObject contextMenuPrefab;
[SerializeField]
[Tooltip("컨텍스트 메뉴의 각 항목으로 사용될 Button 프리팹입니다.")]
private GameObject contextMenuItemPrefab;
[Header("캔버스 설정")]
[SerializeField]
[Tooltip("UI가 그려질 최상위 Canvas입니다. 메뉴가 다른 UI 위에 그려지도록 합니다.")]
private Canvas mainCanvas;
// 현재 활성화된 컨텍스트 메뉴의 RectTransform
private RectTransform _activeMenuRect;
// 메뉴 항목들을 담을 부모 객체
private Transform _menuItemContainer;
// 메뉴 항목 버튼들을 관리하기 위한 오브젝트 풀
private ItemPool<Button> _menuItemPool;
private LocalizationManager _locManager; // 다국어 처리를 위한 LocalizationManager 인스턴스
/// <summary>
/// 싱글톤이 초기화될 때 호출됩니다.
/// </summary>
protected override void Init()
{
base.Init();
_locManager = LocalizationManager.Instance;
if (_locManager == null)
{
// LocalizationManager가 필수적인 경우, 여기서 에러를 발생시키거나 게임을 중단시킬 수 있습니다.
// 여기서는 경고만 기록하고 진행합니다.
Debug.LogWarning("LocalizationManager 인스턴스를 찾을 수 없습니다. 메뉴 텍스트가 제대로 표시되지 않을 수 있습니다.");
}
// 메뉴 항목을 담을 컨테이너를 생성하고 비활성화해 둡니다.
var itemContainerGo = new GameObject("ContextMenuItemContainer");
itemContainerGo.transform.SetParent(transform);
_menuItemContainer = itemContainerGo.transform;
itemContainerGo.SetActive(false);
// 메뉴 항목 프리팹을 사용하여 오브젝트 풀을 초기화합니다.
// [성능 최적화]
// 메뉴 항목을 반복적으로 생성/파괴하는 대신 풀을 사용하여 재활용합니다.
// 이는 가비지 컬렉션을 줄여 성능을 향상시킵니다.
_menuItemPool = new ItemPool<Button>(contextMenuItemPrefab, _menuItemContainer);
}
/// <summary>
/// 컨텍스트 메뉴를 화면에 표시합니다.
/// </summary>
/// <param name="items">표시할 메뉴 항목 데이터 리스트입니다.</param>
/// <param name="position">메뉴가 표시될 화면 좌표입니다.</param>
public void ShowMenu(List<ContextMenuItemData> items, Vector2 position)
{
// 기존 메뉴가 열려있으면 닫습니다.
HideMenu();
// 메뉴 프리팹으로부터 인스턴스를 생성하고 Canvas 자식으로 설정합니다.
var menuObject = Instantiate(contextMenuPrefab, mainCanvas.transform);
_activeMenuRect = menuObject.GetComponent<RectTransform>();
// 메뉴 위치 설정
_activeMenuRect.position = position;
// 메뉴 항목 채우기
PopulateMenu(items);
// 메뉴가 화면 밖으로 나가지 않도록 위치를 조정합니다.
AdjustMenuPosition();
}
/// <summary>
/// 현재 열려있는 컨텍스트 메뉴를 닫습니다.
/// </summary>
public void HideMenu()
{
if (_activeMenuRect != null)
{
// 사용했던 모든 메뉴 항목 버튼을 풀에 반환합니다.
_menuItemPool.ReturnAll(true);
// 활성화된 메뉴 오브젝트를 파괴합니다.
Destroy(_activeMenuRect.gameObject);
_activeMenuRect = null;
}
}
/// <summary>
/// 전달받은 데이터로 메뉴 항목들을 생성합니다.
/// </summary>
/// <param name="items">메뉴 항목 데이터 리스트</param>
private void PopulateMenu(List<ContextMenuItemData> items)
{
foreach (var itemData in items)
{
// 풀에서 메뉴 항목 버튼을 가져옵니다.
Button menuItemButton = _menuItemPool.GetItem(true, _activeMenuRect);
// 버튼의 텍스트를 설정합니다.
TMP_Text buttonText = menuItemButton.GetComponentInChildren<TMP_Text>();
if (buttonText != null)
{
if (_locManager != null)
{
buttonText.text = _locManager.GetString(itemData.DisplayName);
}
else
{
buttonText.text = itemData.DisplayName;
}
}
// 버튼 클릭 이벤트를 설정합니다.
// 기존 리스너를 모두 제거한 후 새로 추가하여 중복 호출을 방지합니다.
menuItemButton.onClick.RemoveAllListeners();
menuItemButton.onClick.AddListener(() =>
{
// 항목을 클릭하면 메뉴를 닫고, 설정된 액션을 실행합니다.
HideMenu();
itemData.Command.Execute(itemData.CommandParameter);
});
}
}
/// <summary>
/// 메뉴가 화면 경계를 벗어나지 않도록 위치를 조정합니다.
/// </summary>
private void AdjustMenuPosition()
{
// 레이아웃이 업데이트되도록 한 프레임 강제로 기다립니다.
Canvas.ForceUpdateCanvases();
Vector3[] corners = new Vector3[4];
_activeMenuRect.GetWorldCorners(corners);
float menuWidth = corners[2].x - corners[0].x;
float menuHeight = corners[1].y - corners[3].y;
Vector2 anchoredPosition = _activeMenuRect.anchoredPosition;
// 오른쪽 경계 확인
if (_activeMenuRect.position.x + menuWidth > Screen.width)
{
anchoredPosition.x -= menuWidth;
}
// 아래쪽 경계 확인
if (_activeMenuRect.position.y - menuHeight < 0)
{
anchoredPosition.y += menuHeight;
}
_activeMenuRect.anchoredPosition = anchoredPosition;
}
void Update()
{
// 메뉴가 열려있지 않으면 아무 작업도 수행하지 않습니다.
if (_activeMenuRect == null)
{
return;
}
// ESC 키를 누르면 메뉴를 닫습니다.
if (Input.GetKeyDown(KeyCode.Escape))
{
HideMenu();
return; // 메뉴가 닫혔으므로 아래 로직은 실행할 필요가 없습니다.
}
// 메뉴가 열려있고, 마우스 왼쪽/오른쪽 버튼이 눌렸을 때
// 커서가 메뉴 위에 있지 않다면 메뉴를 닫습니다.
if (_activeMenuRect != null && (Input.GetMouseButtonDown(0) || Input.GetMouseButtonDown(1)))
{
if (!RectTransformUtility.RectangleContainsScreenPoint(_activeMenuRect, Input.mousePosition, mainCanvas.worldCamera))
{
HideMenu();
}
}
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 6b5b3bd4b972b154d97b0d27797d1dd3

View File

@@ -0,0 +1,35 @@
#nullable enable
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
namespace UVC.UI.Menu
{
/// <summary>
/// UI 요소에 붙여서 왼쪽 또는 오른쪽 클릭 시 컨텍스트 메뉴를 표시하는 역할을 합니다.
/// IPointerClickHandler 인터페이스를 구현하여 클릭 이벤트를 감지합니다.
/// </summary>
public class ContextMenuTrigger : MonoBehaviour, IPointerClickHandler
{
/// <summary>
/// 인스펙터에서 이 트리거에 연결할 메뉴 항목들을 설정합니다.
/// </summary>
[Tooltip("이 트리거가 표시할 컨텍스트 메뉴 항목 리스트입니다.")]
public List<ContextMenuItemData> menuItems = new List<ContextMenuItemData>();
/// <summary>
/// 포인터(마우스/터치)가 이 UI 요소 위에서 클릭되었을 때 호출됩니다.
/// </summary>
/// <param name="eventData">클릭 이벤트에 대한 데이터입니다.</param>
public void OnPointerClick(PointerEventData eventData)
{
// 왼쪽 클릭 또는 오른쪽 클릭 시 메뉴를 표시합니다.
if (eventData.button == PointerEventData.InputButton.Left || eventData.button == PointerEventData.InputButton.Right)
{
// ContextMenuManager 싱글톤 인스턴스를 통해 메뉴를 표시하도록 요청합니다.
// 메뉴 항목 데이터와 현재 마우스 위치를 전달합니다.
ContextMenuManager.Instance.ShowMenu(menuItems, eventData.position);
}
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 3207fc8fdfbc11542a55f86e3f66ab51

View File

@@ -120,17 +120,17 @@ namespace UVC.UI.Menu
{
// LocalizationManager가 필수적인 경우, 여기서 에러를 발생시키거나 게임을 중단시킬 수 있습니다.
// 여기서는 경고만 기록하고 진행합니다.
Debug.LogError("LocalizationManager 인스턴스를 찾을 수 없습니다. 메뉴 텍스트가 제대로 표시되지 않을 수 있습니다.");
Debug.LogWarning("LocalizationManager 인스턴스를 찾을 수 없습니다. 메뉴 텍스트가 제대로 표시되지 않을 수 있습니다.");
}
// Inspector에서 할당된 참조 확인
if (menuItemPrefab == null)
{
Debug.LogError("menuItemPrefab이 Inspector에서 할당되지 않았습니다.", this);
Debug.LogWarning("menuItemPrefab이 Inspector에서 할당되지 않았습니다.", this);
}
if (subMenuItemPrefab == null)
{
Debug.LogError("subMenuItemPrefab이 Inspector에서 할당되지 않았습니다.", this);
Debug.LogWarning("subMenuItemPrefab이 Inspector에서 할당되지 않았습니다.", this);
}
if (menuSeparatorPrefab == null)
{
@@ -141,7 +141,7 @@ namespace UVC.UI.Menu
// 메뉴 컨테이너 확인
if (menuContainer == null)
{
Debug.LogError("menuContainer가 Inspector에서 할당되지 않았습니다. Inspector에서 참조를 설정해주세요.", this);
Debug.LogWarning("menuContainer가 Inspector에서 할당되지 않았습니다. Inspector에서 참조를 설정해주세요.", this);
}
else
{