Created
February 25, 2018 22:34
-
-
Save jirevwe/9c2787ea0577bc52ec1f71dd930737eb to your computer and use it in GitHub Desktop.
ScreenBase is a base class for Unity UI Panels. It handles transitions,
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment