using UnityEngine; using Unity.Netcode; namespace Northbound { public class InteractableModalManager : MonoBehaviour { [Header("UI Reference (Optional - auto-finds if null)")] public InteractableModal interactableModal; [Header("References")] [SerializeField] private PlayerInteraction playerInteraction; private IInteractable _currentInteractable; private IInteractable _previousUnavailableInteractable; private bool _isEnabled = false; private void Awake() { Debug.Log($"[InteractableModalManager] Awake called on {gameObject.name}"); if (playerInteraction == null) { playerInteraction = GetComponent(); Debug.Log($"[InteractableModalManager] PlayerInteraction: {(playerInteraction != null ? "Found" : "Not Found")}"); } FindModalComponent(); } private void Start() { Debug.Log($"[InteractableModalManager] Start called on {gameObject.name}"); FindModalComponent(); if (interactableModal == null) { Debug.LogError("[InteractableModalManager] InteractableModal is still null in Start!"); } // Start()에서 HideModal()을 호출하지 않음 - 첫 번째 Update에서 Modal이 표시되지 않는 문제 방지 // modalPanel과 unavailablePanel은 InteractableModal.Start()에서 이미 false로 설정됨 } private void FindModalComponent() { if (interactableModal != null) { Debug.Log($"[InteractableModalManager] InteractableModal already assigned: {interactableModal.gameObject.name}"); return; } InteractableModal[] modals = GetComponentsInChildren(true); Debug.Log($"[InteractableModalManager] Found {modals.Length} InteractableModal components in player children"); if (modals.Length > 0) { interactableModal = modals[0]; Debug.Log($"[InteractableModalManager] Using InteractableModal on: {interactableModal.gameObject.name}"); } else { Debug.LogWarning("[InteractableModalManager] InteractableModal not found in player children!"); } } private void Update() { if (playerInteraction == null) { Debug.LogWarning("[InteractableModalManager] Update: playerInteraction is null"); return; } bool isLocalPlayer = IsLocalPlayer(); if (!isLocalPlayer) { if (_isEnabled) { HideModal(); _isEnabled = false; } return; } _isEnabled = true; IInteractable detectedInteractable = GetDetectedInteractable(); IInteractable unavailableInteractable = playerInteraction.CurrentUnavailableInteractable; bool interactableChanged = detectedInteractable != _currentInteractable; bool unavailableChanged = unavailableInteractable != _previousUnavailableInteractable; if (interactableChanged || unavailableChanged) { _currentInteractable = detectedInteractable; _previousUnavailableInteractable = unavailableInteractable; if (_currentInteractable != null) { Debug.Log($"[InteractableModalManager] Show modal for interactable"); ShowModal(_currentInteractable); } else if (unavailableInteractable != null) { Debug.Log($"[InteractableModalManager] Show unavailable message"); ShowUnavailableMessageModal(unavailableInteractable); } else { Debug.Log($"[InteractableModalManager] Hide modal"); HideModal(); } } else if (_currentInteractable != null) { UpdateModalContent(); } } private bool IsLocalPlayer() { if (playerInteraction == null) return false; var networkObject = playerInteraction.GetComponent(); if (networkObject != null) { return networkObject.IsOwner; } return false; } private IInteractable GetDetectedInteractable() { var field = playerInteraction.GetType().GetField("_currentInteractable", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); if (field != null) { return field.GetValue(playerInteraction) as IInteractable; } return null; } private void ShowModal(IInteractable interactable) { if (interactableModal != null) { interactableModal.ShowModal(interactable); } } private void HideModal() { if (interactableModal != null) { interactableModal.HideModal(); } } private void ShowUnavailableMessageModal(IInteractable interactable) { if (interactableModal != null) { interactableModal.ShowUnavailableMessage(interactable); } } private void UpdateModalContent() { if (interactableModal != null) { interactableModal.UpdateModalContent(); } } } }