92 lines
2.6 KiB
C#
92 lines
2.6 KiB
C#
using UnityEngine;
|
|
using TMPro;
|
|
|
|
namespace Unheimlich.UI
|
|
{
|
|
public class DialogueScreenUI : MonoBehaviour
|
|
{
|
|
public static DialogueScreenUI Instance { get; private set; }
|
|
|
|
[Header("UI References")]
|
|
[SerializeField] private GameObject _dialoguePanel;
|
|
[SerializeField] private TextMeshProUGUI _speakerText;
|
|
[SerializeField] private TextMeshProUGUI _dialogueText;
|
|
|
|
[Header("Fade Settings")]
|
|
[SerializeField] private float _fadeOutDuration = 0.5f;
|
|
|
|
private CanvasGroup _canvasGroup;
|
|
|
|
private void Awake()
|
|
{
|
|
if (Instance != null && Instance != this)
|
|
{
|
|
Debug.LogWarning("[DialogueScreenUI] Multiple instances detected! Destroying duplicate.");
|
|
Destroy(gameObject);
|
|
return;
|
|
}
|
|
|
|
Instance = this;
|
|
|
|
if (_dialoguePanel != null)
|
|
{
|
|
_canvasGroup = _dialoguePanel.GetComponent<CanvasGroup>();
|
|
|
|
if (_canvasGroup == null)
|
|
_canvasGroup = _dialoguePanel.AddComponent<CanvasGroup>();
|
|
}
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
if (Instance == this)
|
|
Instance = null;
|
|
}
|
|
|
|
public void Show(string speaker, string dialogue)
|
|
{
|
|
if (_speakerText != null)
|
|
_speakerText.text = speaker;
|
|
|
|
if (_dialogueText != null)
|
|
_dialogueText.text = dialogue;
|
|
|
|
if (_dialoguePanel != null)
|
|
{
|
|
_dialoguePanel.SetActive(true);
|
|
|
|
if (_canvasGroup != null)
|
|
{
|
|
LeanTween.cancel(_dialoguePanel);
|
|
_canvasGroup.alpha = 1f;
|
|
}
|
|
}
|
|
}
|
|
|
|
public void Hide()
|
|
{
|
|
if (_dialoguePanel != null)
|
|
_dialoguePanel.SetActive(false);
|
|
}
|
|
|
|
public void FadeOutAndHide(System.Action onComplete = null)
|
|
{
|
|
if (_canvasGroup != null && _dialoguePanel != null)
|
|
{
|
|
LeanTween.cancel(_dialoguePanel);
|
|
LeanTween.alphaCanvas(_canvasGroup, 0f, _fadeOutDuration)
|
|
.setOnComplete(() =>
|
|
{
|
|
Hide();
|
|
onComplete?.Invoke();
|
|
});
|
|
}
|
|
else
|
|
{
|
|
Hide();
|
|
onComplete?.Invoke();
|
|
}
|
|
}
|
|
}
|
|
}
|