jirevwe
2/25/2018 - 10:34 PM

ScreenBase is a base class for Unity UI Panels. It handles transitions,

ScreenBase is a base class for Unity UI Panels. It handles transitions,

using UnityEngine;
using System.Collections;
using DG.Tweening;
using System;

[RequireComponent(typeof(CanvasGroup))]
public abstract class ScreenBase : MonoBehaviour
{
    CanvasGroup canvasGroup;

    public void Start()
    {
        canvasGroup = GetComponent<CanvasGroup>();
        canvasGroup.blocksRaycasts = false;
        canvasGroup.interactable = false;
        canvasGroup.alpha = 0.0f;
    }

    [HideInInspector]
    public bool panelClickAllowed = false;

    public abstract void OnBackClick();
    public abstract void PreScreenOpened();

    public void HideSmoothly(float time = 0.2f, float delay = 0, Action action = null)
    {
        if (delay == 0)
        {
            DOTween.To(() => canvasGroup.alpha, a => canvasGroup.alpha = a, 0.0f, time).OnComplete(() =>
            {
                HideInstantly();
                if (action != null) action();
            });
        }
        else
        {
            StartCoroutine(HideSmoothlyIE(time, delay, action));
        }
    } // HideSmoothly

    private IEnumerator HideSmoothlyIE(float time, float delay, Action action = null)
    {
        yield return new WaitForSeconds(delay);
        DOTween.To(() => canvasGroup.alpha, a => canvasGroup.alpha = a, 0.0f, time).OnComplete(() =>
        {
            HideInstantly();
            if (action != null) action();
        });
    } // HideSmoothly

    public void ShowSmoothly(float time = 0.2f, float delay = 0.0f, Action actionAfterOpen = null, Action actionBeforeOpen = null)
    {
        if (delay == 0)
        {
            PreScreenOpened();
            if (actionBeforeOpen != null) actionBeforeOpen();
            
            DOTween.To(() => canvasGroup.alpha, a => canvasGroup.alpha = a, 1.0f, time).OnComplete(() =>
            {
                ShowInstantly();
                if (actionAfterOpen != null) actionAfterOpen();
            });
        }
        else
        {
            StartCoroutine(ShowSmoothlyIE(time, delay, actionAfterOpen));
        }
    } // ShowSmoothly

    private IEnumerator ShowSmoothlyIE(float time, float delay, Action action = null)
    {
        yield return new WaitForSeconds(delay);
        DOTween.To(() => canvasGroup.alpha, a => canvasGroup.alpha = a, 1.0f, time).OnComplete(() =>
        {
            ShowInstantly();
            if (action != null) action();
        });
    } // ShowSmoothly

    public void HideInstantly()
    {
        canvasGroup.interactable = false;
        canvasGroup.alpha = 0.0f;
        canvasGroup.blocksRaycasts = false;
        ScreenClosed();
    } // HideInstantly

    public void ShowInstantly()
    {
        canvasGroup.alpha = 1.0f;
        canvasGroup.blocksRaycasts = true;
        canvasGroup.interactable = true;
        ScreenOpened();
    } // ShowInstantly

    public virtual void ScreenOpened()
    {
        StartCoroutine(AllowClick(0.1f));
    } // ScreenOpened

    public virtual void ScreenClosed()
    {
        DoNotAllowClick();
    } // ScreenClosed

    protected void DoNotAllowClick()
    {
        panelClickAllowed = false;
        canvasGroup.interactable = false;
    } // AllowClick

    protected IEnumerator AllowClick(float time)
    {
        yield return new WaitForSeconds(time);
        panelClickAllowed = true;
        canvasGroup.interactable = true;
    } // AllowClick
} // PanelBase