Files
PIM_Laba4/Assets/Scripts/InteractionPromptUI.cs
T
2026-05-25 23:53:50 +03:00

111 lines
2.2 KiB
C#

using UnityEngine;
using UnityEngine.UI;
public class InteractionPromptUI : MonoBehaviour
{
public static InteractionPromptUI Instance { get; private set; }
[SerializeField] private GameObject promptPanel;
[SerializeField] private Text promptText;
private Object currentOwner;
private string currentMessage = "Нажмите E";
public void Configure(GameObject panel, Text textComponent)
{
promptPanel = panel;
promptText = textComponent;
if (promptPanel != null)
{
promptPanel.SetActive(false);
}
}
private void Awake()
{
if (Instance != null && Instance != this)
{
Destroy(gameObject);
return;
}
Instance = this;
}
private void Update()
{
RefreshVisibility();
}
public void ShowPrompt(Object owner, string message)
{
if (owner == null)
{
return;
}
currentOwner = owner;
currentMessage = string.IsNullOrWhiteSpace(message) ? "Нажмите E" : message;
RefreshVisibility();
}
public void HidePrompt(Object owner)
{
if (owner == null || currentOwner != owner)
{
return;
}
currentOwner = null;
RefreshVisibility();
}
public void HideAll()
{
currentOwner = null;
RefreshVisibility();
}
private void RefreshVisibility()
{
if (promptPanel == null || promptText == null)
{
return;
}
if (!IsOwnerValid(currentOwner))
{
currentOwner = null;
}
var shouldShow = currentOwner != null && !GameUIState.IsAnyBlockingUIOpen;
promptPanel.SetActive(shouldShow);
if (shouldShow)
{
promptText.text = currentMessage;
}
}
private static bool IsOwnerValid(Object owner)
{
if (owner == null)
{
return false;
}
if (owner is Component component)
{
return component.gameObject.activeInHierarchy;
}
if (owner is GameObject gameObject)
{
return gameObject.activeInHierarchy;
}
return true;
}
}