117 lines
2.9 KiB
C#
117 lines
2.9 KiB
C#
using DG.Tweening;
|
|
using UnityEngine;
|
|
|
|
[DisallowMultipleComponent]
|
|
public class UI_SettingsPanelEnterAnim : MonoBehaviour
|
|
{
|
|
[SerializeField] private RectTransform moveTarget;
|
|
[SerializeField] private CanvasGroup fadeTarget;
|
|
[SerializeField] private bool playOnEnable = true;
|
|
[SerializeField] private float enterDuration = 0.3f;
|
|
[SerializeField] private float fromOffsetY = 32f;
|
|
[SerializeField] private Ease enterEase = Ease.OutCubic;
|
|
[SerializeField] private bool useUnscaledTime = true;
|
|
|
|
private Sequence enterSequence;
|
|
private bool basePosCached;
|
|
private Vector2 basePos;
|
|
|
|
private void Awake()
|
|
{
|
|
AutoWire();
|
|
CacheBasePos();
|
|
}
|
|
|
|
private void OnEnable()
|
|
{
|
|
if (playOnEnable)
|
|
Play();
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
KillTween();
|
|
ResetToBase();
|
|
}
|
|
|
|
public void Play()
|
|
{
|
|
AutoWire();
|
|
CacheBasePos();
|
|
|
|
if (moveTarget == null || fadeTarget == null)
|
|
return;
|
|
|
|
KillTween();
|
|
|
|
moveTarget.anchoredPosition = basePos - new Vector2(0f, Mathf.Abs(fromOffsetY));
|
|
fadeTarget.alpha = 0f;
|
|
fadeTarget.interactable = false;
|
|
fadeTarget.blocksRaycasts = false;
|
|
|
|
enterSequence = DOTween.Sequence().SetUpdate(useUnscaledTime);
|
|
enterSequence.Join(moveTarget.DOAnchorPos(basePos, Mathf.Max(0.01f, enterDuration)).SetEase(enterEase));
|
|
enterSequence.Join(fadeTarget.DOFade(1f, Mathf.Max(0.01f, enterDuration)).SetEase(Ease.OutCubic));
|
|
enterSequence.OnComplete(() =>
|
|
{
|
|
fadeTarget.interactable = true;
|
|
fadeTarget.blocksRaycasts = true;
|
|
enterSequence = null;
|
|
});
|
|
}
|
|
|
|
private void AutoWire()
|
|
{
|
|
if (moveTarget == null)
|
|
{
|
|
moveTarget = transform as RectTransform;
|
|
if (moveTarget == null)
|
|
moveTarget = GetComponentInChildren<RectTransform>(true);
|
|
}
|
|
|
|
if (fadeTarget == null)
|
|
fadeTarget = GetComponent<CanvasGroup>();
|
|
if (fadeTarget == null)
|
|
fadeTarget = gameObject.AddComponent<CanvasGroup>();
|
|
}
|
|
|
|
private void CacheBasePos()
|
|
{
|
|
if (moveTarget == null)
|
|
return;
|
|
|
|
if (!basePosCached)
|
|
{
|
|
basePos = moveTarget.anchoredPosition;
|
|
basePosCached = true;
|
|
}
|
|
}
|
|
|
|
private void ResetToBase()
|
|
{
|
|
if (moveTarget != null && basePosCached)
|
|
moveTarget.anchoredPosition = basePos;
|
|
|
|
if (fadeTarget != null)
|
|
{
|
|
fadeTarget.alpha = 1f;
|
|
fadeTarget.interactable = true;
|
|
fadeTarget.blocksRaycasts = true;
|
|
}
|
|
}
|
|
|
|
private void KillTween()
|
|
{
|
|
if (enterSequence != null)
|
|
{
|
|
enterSequence.Kill();
|
|
enterSequence = null;
|
|
}
|
|
|
|
if (moveTarget != null)
|
|
moveTarget.DOKill();
|
|
if (fadeTarget != null)
|
|
fadeTarget.DOKill();
|
|
}
|
|
}
|