2021-02-19 17:33:02 +01:00
|
|
|
using System;
|
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using TMPro;
|
|
|
|
using UnityEngine;
|
|
|
|
using UnityEngine.UI;
|
|
|
|
|
|
|
|
#if UNITY_EDITOR
|
|
|
|
using UnityEditor;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// updates visible subtitle text and a character portrait of the speaker
|
|
|
|
public class DialogueUI : MonoBehaviour
|
|
|
|
{
|
|
|
|
[SerializeField] private DialogueSystem _dialogue;
|
2021-03-11 13:02:34 +01:00
|
|
|
|
|
|
|
[SerializeField] private float _fadeTime;
|
|
|
|
[SerializeField] private CanvasGroup _dialogueGroup;
|
2021-02-19 17:33:02 +01:00
|
|
|
[SerializeField] private TMP_Text _text;
|
2021-03-11 12:48:17 +01:00
|
|
|
[SerializeField] private GameObject _portrait;
|
2021-02-19 17:33:02 +01:00
|
|
|
|
|
|
|
private bool _dismissed;
|
|
|
|
|
|
|
|
private void OnEnable()
|
|
|
|
{
|
|
|
|
_dialogue.onDialogueLine += (_, dl) => ShowLine(dl);
|
|
|
|
|
|
|
|
_text.text = string.Empty;
|
2021-03-11 12:48:17 +01:00
|
|
|
_portrait.SetActive(false);
|
2021-02-19 17:33:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void Dismiss()
|
|
|
|
{
|
|
|
|
_dismissed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private IEnumerator _subtitleRoutine;
|
|
|
|
|
|
|
|
private void ShowLine(DialogueLine line)
|
|
|
|
{
|
|
|
|
// there is a line currently being shown, abort its coroutine and hide ui elements
|
|
|
|
if (_subtitleRoutine != null && _text.text != null)
|
|
|
|
{
|
|
|
|
HideSubtitle();
|
|
|
|
StopCoroutine(_subtitleRoutine);
|
|
|
|
}
|
|
|
|
|
|
|
|
_subtitleRoutine = ShowLineCR(line.text, line.duration);
|
|
|
|
StartCoroutine(_subtitleRoutine);
|
|
|
|
}
|
|
|
|
|
|
|
|
private IEnumerator ShowLineCR(string text, float duration)
|
|
|
|
{
|
|
|
|
// update text and show portrait
|
2021-03-11 13:02:34 +01:00
|
|
|
float a = 0f;
|
|
|
|
_dialogueGroup.alpha = a;
|
2021-02-19 17:33:02 +01:00
|
|
|
_text.text = text;
|
2021-03-11 12:48:17 +01:00
|
|
|
_portrait.SetActive( true );
|
2021-02-19 17:33:02 +01:00
|
|
|
|
2021-03-11 13:02:34 +01:00
|
|
|
while ( a < 1f )
|
|
|
|
{
|
|
|
|
yield return null;
|
|
|
|
a += Time.deltaTime / _fadeTime;
|
|
|
|
_dialogueGroup.alpha = a;
|
|
|
|
}
|
|
|
|
|
|
|
|
_dialogueGroup.alpha = 1f;
|
|
|
|
|
2021-02-19 17:33:02 +01:00
|
|
|
// wait until timeout of dismissal
|
|
|
|
var timer = 0f;
|
|
|
|
while (!_dismissed && timer < duration)
|
|
|
|
{
|
|
|
|
timer += Time.deltaTime;
|
|
|
|
|
|
|
|
// wait for next frame
|
|
|
|
yield return null;
|
|
|
|
}
|
|
|
|
|
2021-03-11 13:02:34 +01:00
|
|
|
while ( a > 0f )
|
|
|
|
{
|
|
|
|
yield return null;
|
|
|
|
a -= Time.deltaTime / _fadeTime;
|
|
|
|
_dialogueGroup.alpha = a;
|
|
|
|
}
|
|
|
|
|
|
|
|
_dialogueGroup.alpha = 0f;
|
|
|
|
|
2021-02-19 17:33:02 +01:00
|
|
|
// hide ui elements
|
|
|
|
HideSubtitle();
|
|
|
|
|
|
|
|
_dismissed = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void HideSubtitle()
|
|
|
|
{
|
|
|
|
_text.text = string.Empty;
|
2021-03-11 12:48:17 +01:00
|
|
|
_portrait.SetActive( false );
|
2021-02-19 17:33:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#region Editor
|
|
|
|
#if UNITY_EDITOR
|
|
|
|
|
|
|
|
[CustomEditor(typeof(DialogueUI))]
|
|
|
|
public class DialogueUIEditor : Editor
|
|
|
|
{
|
|
|
|
private DialogueUI _dialogueUI;
|
|
|
|
|
|
|
|
private void OnEnable()
|
|
|
|
{
|
|
|
|
_dialogueUI = target as DialogueUI;
|
|
|
|
}
|
|
|
|
|
|
|
|
public override void OnInspectorGUI()
|
|
|
|
{
|
|
|
|
base.OnInspectorGUI();
|
|
|
|
|
|
|
|
if (!Application.isPlaying) return;
|
|
|
|
|
|
|
|
if (GUILayout.Button("Dismiss"))
|
|
|
|
{
|
|
|
|
_dialogueUI.Dismiss();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
#endregion
|