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; /// /// Show a Message on the HUD /// /// message to show /// time to stay showing /// time to fade out /// /// 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(); } /// /// Clear message text display /// /// public void Clear() { message.text = ""; message.CrossFadeAlpha(0, 0, false); } } }