160 lines
5.5 KiB
C#
160 lines
5.5 KiB
C#
using System.Collections;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
namespace Beyond
|
|
{
|
|
public class HUDSubtitle : MonoBehaviour
|
|
{
|
|
string m_lastSubtitile = "";
|
|
public float m_showTime = 4f;
|
|
public float m_fadeTime = 1f;
|
|
float m_timer = 0f;
|
|
TextMeshProUGUI m_textMesh;
|
|
public GameObject m_imageObject; // Referencja do obiektu z obrazem
|
|
public AudioSource m_audioSource; // Referencja do komponentu AudioSource
|
|
public AudioClip m_showSound; // Dźwięk, który ma być odtwarzany podczas pokazywania napisów
|
|
public AudioClip m_hideSound; // Dźwięk, który ma być odtwarzany podczas ukrywania napisów
|
|
|
|
enum State { OFF, FADE_IN, SHOW, FADE_OUT };
|
|
State m_state;
|
|
|
|
void Start()
|
|
{
|
|
m_textMesh = GetComponent<TextMeshProUGUI>();
|
|
if (m_textMesh == null)
|
|
{
|
|
Debug.LogError("TextMeshPro not found! exiting");
|
|
Destroy(this);
|
|
}
|
|
|
|
if (m_imageObject == null)
|
|
{
|
|
Debug.LogError("Image GameObject not assigned in inspector! exiting");
|
|
Destroy(this);
|
|
}
|
|
|
|
if (m_audioSource == null)
|
|
{
|
|
Debug.LogError("AudioSource not assigned in inspector! exiting");
|
|
Destroy(this);
|
|
}
|
|
|
|
SetState(State.OFF); // Ustawienie stanu OFF na starcie
|
|
}
|
|
|
|
void SetState(State state)
|
|
{
|
|
switch (state)
|
|
{
|
|
case State.OFF:
|
|
m_textMesh.enabled = false; // Wyłącz tekst
|
|
if (m_imageObject != null)
|
|
{
|
|
m_imageObject.SetActive(false); // Wyłącz cały obiekt obrazu
|
|
}
|
|
break;
|
|
case State.FADE_IN:
|
|
m_textMesh.enabled = true; // Włącz tekst
|
|
m_textMesh.alpha = 0f;
|
|
if (m_imageObject != null)
|
|
{
|
|
m_imageObject.SetActive(true); // Włącz cały obiekt obrazu
|
|
Image img = m_imageObject.GetComponent<Image>();
|
|
if (img != null)
|
|
{
|
|
img.color = new Color(img.color.r, img.color.g, img.color.b, 0f); // Ustaw przezroczystość obrazu
|
|
}
|
|
}
|
|
PlaySound(m_showSound); // Odtwórz dźwięk pokazywania napisów
|
|
break;
|
|
case State.SHOW:
|
|
m_textMesh.alpha = 1f;
|
|
if (m_imageObject != null)
|
|
{
|
|
Image img = m_imageObject.GetComponent<Image>();
|
|
if (img != null)
|
|
{
|
|
img.color = new Color(img.color.r, img.color.g, img.color.b, 1f); // Obraz w pełni widoczny
|
|
}
|
|
}
|
|
break;
|
|
case State.FADE_OUT:
|
|
PlaySound(m_hideSound); // Odtwórz dźwięk ukrywania napisów
|
|
break;
|
|
}
|
|
m_state = state;
|
|
m_timer = 0f;
|
|
}
|
|
|
|
public bool ShowSubitle(string subtitle)
|
|
{
|
|
if (m_lastSubtitile.CompareTo(subtitle) == 0)
|
|
return false;
|
|
m_lastSubtitile = subtitle;
|
|
m_textMesh.text = subtitle;
|
|
SetState(State.FADE_IN);
|
|
return true;
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
float a = 0f;
|
|
switch (m_state)
|
|
{
|
|
case State.FADE_IN:
|
|
if (m_timer > m_fadeTime)
|
|
{
|
|
SetState(State.SHOW);
|
|
break;
|
|
}
|
|
a = Mathf.SmoothStep(0f, 1f, m_timer / m_fadeTime);
|
|
m_textMesh.alpha = a;
|
|
if (m_imageObject != null)
|
|
{
|
|
Image img = m_imageObject.GetComponent<Image>();
|
|
if (img != null)
|
|
{
|
|
img.color = new Color(img.color.r, img.color.g, img.color.b, a); // Fading obrazu
|
|
}
|
|
}
|
|
break;
|
|
case State.FADE_OUT:
|
|
if (m_timer > m_fadeTime)
|
|
{
|
|
SetState(State.OFF);
|
|
break;
|
|
}
|
|
a = Mathf.SmoothStep(0f, 1f, 1.0f - m_timer / m_fadeTime);
|
|
m_textMesh.alpha = a;
|
|
if (m_imageObject != null)
|
|
{
|
|
Image img = m_imageObject.GetComponent<Image>();
|
|
if (img != null)
|
|
{
|
|
img.color = new Color(img.color.r, img.color.g, img.color.b, a); // Zanikanie obrazu
|
|
}
|
|
}
|
|
break;
|
|
case State.SHOW:
|
|
if (m_timer > m_showTime)
|
|
{
|
|
SetState(State.FADE_OUT);
|
|
break;
|
|
}
|
|
break;
|
|
}
|
|
m_timer += Time.deltaTime;
|
|
}
|
|
|
|
void PlaySound(AudioClip clip)
|
|
{
|
|
if (clip != null && m_audioSource != null)
|
|
{
|
|
m_audioSource.PlayOneShot(clip); // Odtwarzanie dźwięku
|
|
}
|
|
}
|
|
}
|
|
}
|