77 lines
1.9 KiB
C#
77 lines
1.9 KiB
C#
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
/// <summary>
|
|
/// Плашка подсказки взаимодействия (например "[E] ПРОЧЕСТЬ ЗАПИСКУ"),
|
|
/// отображаемая по центру экрана под прицелом, когда игрок подходит к интерактивному объекту.
|
|
/// </summary>
|
|
public class InteractionPromptUI : MonoBehaviour
|
|
{
|
|
private static InteractionPromptUI _instance;
|
|
public static InteractionPromptUI Instance
|
|
{
|
|
get
|
|
{
|
|
if (_instance == null)
|
|
{
|
|
_instance = Object.FindAnyObjectByType<InteractionPromptUI>(FindObjectsInactive.Include);
|
|
}
|
|
return _instance;
|
|
}
|
|
}
|
|
|
|
[SerializeField] private GameObject promptRoot;
|
|
[SerializeField] private Text promptText;
|
|
|
|
private float hideTimer = 0f;
|
|
|
|
private void Awake()
|
|
{
|
|
_instance = this;
|
|
if (promptRoot == null)
|
|
{
|
|
promptRoot = transform.childCount > 0 ? transform.GetChild(0).gameObject : gameObject;
|
|
}
|
|
HidePromptImmediate();
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (hideTimer > 0f)
|
|
{
|
|
hideTimer -= Time.unscaledDeltaTime;
|
|
if (hideTimer <= 0f)
|
|
{
|
|
HidePromptImmediate();
|
|
}
|
|
}
|
|
}
|
|
|
|
public void ShowPrompt(string message, float duration = 0.25f)
|
|
{
|
|
if (promptRoot != null)
|
|
{
|
|
if (!promptRoot.activeSelf) promptRoot.SetActive(true);
|
|
}
|
|
else
|
|
{
|
|
if (!gameObject.activeSelf) gameObject.SetActive(true);
|
|
}
|
|
|
|
if (promptText != null)
|
|
{
|
|
promptText.text = message;
|
|
}
|
|
hideTimer = duration;
|
|
}
|
|
|
|
public void HidePromptImmediate()
|
|
{
|
|
if (promptRoot != null && promptRoot != gameObject)
|
|
{
|
|
promptRoot.SetActive(false);
|
|
}
|
|
hideTimer = 0f;
|
|
}
|
|
}
|