first commit
This commit is contained in:
@@ -0,0 +1,111 @@
|
||||
using System.Collections;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
public class DialogueHUD : MonoBehaviour
|
||||
{
|
||||
public static DialogueHUD Instance { get; private set; }
|
||||
|
||||
[Header("UI")]
|
||||
public Text hintText;
|
||||
public Text statusText;
|
||||
public Text inventoryText;
|
||||
|
||||
[Header("Data")]
|
||||
public Inventory inventory;
|
||||
public float messageDuration = 2.5f;
|
||||
|
||||
private Coroutine messageRoutine;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
if (Instance != null && Instance != this)
|
||||
{
|
||||
Destroy(gameObject);
|
||||
return;
|
||||
}
|
||||
|
||||
Instance = this;
|
||||
}
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
if (inventory != null)
|
||||
inventory.OnInventoryChanged += UpdateInventoryText;
|
||||
|
||||
HideHint();
|
||||
UpdateInventoryText();
|
||||
}
|
||||
|
||||
private void OnDisable()
|
||||
{
|
||||
if (inventory != null)
|
||||
inventory.OnInventoryChanged -= UpdateInventoryText;
|
||||
}
|
||||
|
||||
public void ShowHint(string message)
|
||||
{
|
||||
if (hintText == null)
|
||||
return;
|
||||
|
||||
hintText.text = message;
|
||||
SetTextRootActive(hintText, !string.IsNullOrEmpty(message));
|
||||
}
|
||||
|
||||
public void HideHint()
|
||||
{
|
||||
if (hintText == null)
|
||||
return;
|
||||
|
||||
hintText.text = string.Empty;
|
||||
SetTextRootActive(hintText, false);
|
||||
}
|
||||
|
||||
public void SetStatus(string message)
|
||||
{
|
||||
if (statusText == null)
|
||||
return;
|
||||
|
||||
statusText.text = message;
|
||||
SetTextRootActive(statusText, !string.IsNullOrEmpty(message));
|
||||
|
||||
if (messageRoutine != null)
|
||||
StopCoroutine(messageRoutine);
|
||||
|
||||
if (!string.IsNullOrEmpty(message) && gameObject.activeInHierarchy)
|
||||
messageRoutine = StartCoroutine(ClearStatusAfterDelay());
|
||||
}
|
||||
|
||||
public void UpdateInventoryText()
|
||||
{
|
||||
if (inventoryText == null)
|
||||
return;
|
||||
|
||||
inventoryText.text = inventory != null ? inventory.GetDebugText() : "Инвентарь: недоступен";
|
||||
}
|
||||
|
||||
private IEnumerator ClearStatusAfterDelay()
|
||||
{
|
||||
yield return new WaitForSeconds(messageDuration);
|
||||
|
||||
if (statusText != null)
|
||||
{
|
||||
statusText.text = string.Empty;
|
||||
SetTextRootActive(statusText, false);
|
||||
}
|
||||
|
||||
messageRoutine = null;
|
||||
}
|
||||
|
||||
private static void SetTextRootActive(Text text, bool active)
|
||||
{
|
||||
if (text == null)
|
||||
return;
|
||||
|
||||
Transform parent = text.transform.parent;
|
||||
if (parent != null && parent.GetComponent<Image>() != null)
|
||||
parent.gameObject.SetActive(active);
|
||||
else
|
||||
text.gameObject.SetActive(active);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user