57 lines
1.8 KiB
C#
57 lines
1.8 KiB
C#
using DG.Tweening;
|
|
using UnityEngine;
|
|
|
|
public class UI_Show_Anim : MonoBehaviour
|
|
{
|
|
public float animTime_Alpha = 1f;
|
|
public float animTime_Scale = 1f;
|
|
private void OnEnable()
|
|
{
|
|
Clear();
|
|
RectTransform rect = transform as RectTransform;
|
|
bool canScale = rect == null || !UI_OrderedEntryAnimator.IsLayoutSensitive(rect);
|
|
if (canScale)
|
|
{
|
|
transform.localScale = Vector3.zero;
|
|
transform.DOScale(1, animTime_Scale).SetUpdate(true);
|
|
}
|
|
|
|
if (gameObject.Try_Get_Component(out CanvasGroup canvasGroup))
|
|
{
|
|
canvasGroup.alpha = 0;
|
|
float a = 0;
|
|
DOTween.To(() => a, (f) => { a = f; canvasGroup.alpha = f; }, 1, animTime_Alpha)
|
|
.SetUpdate(true)
|
|
.SetTarget(canvasGroup);
|
|
}
|
|
}
|
|
public void Close()
|
|
{
|
|
Clear();
|
|
RectTransform rect = transform as RectTransform;
|
|
bool canScale = rect == null || !UI_OrderedEntryAnimator.IsLayoutSensitive(rect);
|
|
if (canScale)
|
|
{
|
|
transform.DOScale(0, animTime_Scale).SetUpdate(true);
|
|
}
|
|
if (gameObject.Try_Get_Component(out CanvasGroup canvasGroup))
|
|
{
|
|
float a = 1;
|
|
DOTween.To(() => a, (f) => { a = f; canvasGroup.alpha = f; }, 0, animTime_Alpha)
|
|
.SetUpdate(true)
|
|
.SetTarget(canvasGroup);
|
|
}
|
|
}
|
|
void Clear()
|
|
{
|
|
// Only kill tweens that belong to this UI element.
|
|
// Passing a Transform into DOTween.KillAll(...) is dangerous in Unity because Transform has an implicit bool
|
|
// conversion, which ends up calling KillAll(true) and wipes tweens globally.
|
|
transform.DOKill();
|
|
if (gameObject.Try_Get_Component(out CanvasGroup canvasGroup))
|
|
{
|
|
canvasGroup.DOKill();
|
|
}
|
|
}
|
|
}
|