csharp ScreenBase是Unity UI面板的基类。它处理转换,在屏幕显示和延迟之前和之后添加任务。它德
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp ScreenBase是Unity UI面板的基类。它处理转换,在屏幕显示和延迟之前和之后添加任务。它德相关的知识,希望对你有一定的参考价值。
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
以上是关于csharp ScreenBase是Unity UI面板的基类。它处理转换,在屏幕显示和延迟之前和之后添加任务。它德的主要内容,如果未能解决你的问题,请参考以下文章