using System.Collections; using UnityEngine; namespace Crosstales.UI { /// Controls a UI group (hint). public class UIHint : MonoBehaviour { #region Variables /// Group to fade. [Tooltip("Group to fade.")] public CanvasGroup Group; /// Delay in seconds before fading (default: 2). [Tooltip("Delay in seconds before fading (default: 2).")] public float Delay = 2f; /// Fade time in seconds (default: 2). [Tooltip("Fade time in seconds (default: 2).")] public float FadeTime = 2f; /// Disable UI element after the fade (default: true). [Tooltip("Disable UI element after the fade (default: true).")] public bool Disable = true; /// Fade at Start (default: true). [Tooltip("Fade at Start (default: true).")] public bool FadeAtStart = true; #endregion #region MonoBehaviour methods private void Start() { if (FadeAtStart) FadeDown(); } #endregion #region Public methods public void FadeUp() { StartCoroutine(lerpAlphaUp(0f, 1f, FadeTime, Delay, Group)); } public void FadeDown() { StartCoroutine(lerpAlphaDown(1f, 0f, FadeTime, Delay, Group)); } #endregion #region Private methods private IEnumerator lerpAlphaDown(float startAlphaValue, float endAlphaValue, float time, float delay, Component gameObjectToFade) { gameObjectToFade.gameObject.SetActive(true); Group.alpha = Mathf.Clamp01(startAlphaValue); endAlphaValue = Mathf.Clamp01(endAlphaValue); if (delay > 0) yield return new WaitForSeconds(delay); while (Group.alpha >= endAlphaValue + 0.01f) { Group.alpha -= (1f - endAlphaValue) / time * Time.deltaTime; yield return null; } gameObjectToFade.gameObject.SetActive(!Disable); } private IEnumerator lerpAlphaUp(float startAlphaValue, float endAlphaValue, float time, float delay, Component gameObjectToFade) { if (gameObjectToFade != null) { gameObjectToFade.gameObject.SetActive(true); Group.alpha = Mathf.Clamp01(startAlphaValue); endAlphaValue = Mathf.Clamp01(endAlphaValue); if (delay > 0) yield return new WaitForSeconds(delay); while (Group.alpha <= endAlphaValue - 0.01f) { Group.alpha += endAlphaValue / time * Time.deltaTime; yield return null; } gameObjectToFade.gameObject.SetActive(!Disable); } } #endregion } } // © 2018-2023 crosstales LLC (https://www.crosstales.com)