using UnityEngine; using DuloGames.UI.Tweens; namespace DuloGames.UI { public class Test_FadeInOut : MonoBehaviour { [SerializeField] private float m_Duration = 4f; [SerializeField] private TweenEasing m_Easing = TweenEasing.InOutQuint; private CanvasGroup m_Group; // Tween controls [System.NonSerialized] private readonly TweenRunner m_FloatTweenRunner; protected Test_FadeInOut() { if (this.m_FloatTweenRunner == null) this.m_FloatTweenRunner = new TweenRunner(); this.m_FloatTweenRunner.Init(this); } protected void OnEnable() { this.m_Group = this.GetComponent(); if (this.m_Group == null) this.m_Group = this.gameObject.AddComponent(); this.StartAlphaTween(0f, this.m_Duration, true); } /// /// Tweens the canvas group alpha. /// /// Target alpha. /// Duration. /// If set to true ignore time scale. private void StartAlphaTween(float targetAlpha, float duration, bool ignoreTimeScale) { if (this.m_Group == null) return; float currentAlpha = this.m_Group.alpha; if (currentAlpha.Equals(targetAlpha)) return; var floatTween = new FloatTween { duration = duration, startFloat = currentAlpha, targetFloat = targetAlpha }; floatTween.AddOnChangedCallback(SetAlpha); floatTween.AddOnFinishCallback(OnTweenFinished); floatTween.ignoreTimeScale = ignoreTimeScale; floatTween.easing = this.m_Easing; this.m_FloatTweenRunner.StartTween(floatTween); } /// /// Sets the list alpha. /// /// Alpha. private void SetAlpha(float alpha) { if (this.m_Group == null) return; // Set the alpha this.m_Group.alpha = alpha; } /// /// Raises the list tween finished event. /// protected virtual void OnTweenFinished() { if (this.m_Group == null) return; this.StartAlphaTween((this.m_Group.alpha == 1f ? 0f : 1f), this.m_Duration, true); } } }