Files
beyond/Assets/Scripts/InvectorDerivatives/bItemCollectionTextHUD.cs
2024-11-20 15:21:28 +01:00

85 lines
2.4 KiB
C#

using System;
using System.Collections;
using Invector;
using TMPro;
using UnityEngine;
using UnityEngine.Serialization;
using UnityEngine.UI;
namespace Beyond
{
[vClassHeader("Item Collection HUD", helpBoxText = "Contains all behaviour to show messages sended")]
public class bItemCollectionTextHUD : MonoBehaviour
{
[SerializeField]
private TMP_Text message;
/// <summary>
/// Show a Message on the HUD
/// </summary>
/// <param name="message">message to show</param>
/// <param name="timeToStay">time to stay showing</param>
/// <param name="timeToFadeOut">time to fade out</param>
///
///
private Action textsToShowOnEnabe;
private bool hasStartedTimer = false;
private void OnEnable()
{
if (textsToShowOnEnabe != null)
{
textsToShowOnEnabe.Invoke();
}
else if (hasStartedTimer)
{
Destroy(gameObject);
}
}
public void Show(string message, float timeToStay = 1, float timeToFadeOut = 1)
{
this.message.text = message;
if (gameObject.activeInHierarchy)
{
StartCoroutine(TimerCoroutine(timeToStay, timeToFadeOut));
}
else
{
textsToShowOnEnabe = () => StartTimer(timeToStay, timeToFadeOut);
}
}
private void StartTimer(float timeToStay = 1, float timeToFadeOut = 1)
{
StartCoroutine(TimerCoroutine(timeToStay, timeToFadeOut));
textsToShowOnEnabe = null;//()=> StartTimer(timeToStay, timeToFadeOut);
}
private IEnumerator TimerCoroutine(float timeToStay = 1, float timeToFadeOut = 1)
{
hasStartedTimer = true;
message.CrossFadeAlpha(1, 0.5f, false);
yield return new WaitForSeconds(timeToStay);
message.CrossFadeAlpha(0, timeToFadeOut, false);
yield return new WaitForSeconds(timeToFadeOut + 0.1f);
Destroy(gameObject);
}
private void Awake()
{
Clear();
}
/// <summary>
/// Clear message text display
/// </summary>
///
public void Clear()
{
message.text = "";
message.CrossFadeAlpha(0, 0, false);
}
}
}