BITKit/Packages/Runtime/UX/BlackScreen/BlackScreen.cs

50 lines
1.4 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
using DG.Tweening;
using UnityEngine.Events;
using Cysharp.Threading.Tasks;
namespace BITKit.UX
{
public class BlackScreen : MonoBehaviour
{
[Header(Constant.Header.Components)]
public UIDocument document;
VisualElement root;
void Start()
{
root = document.rootVisualElement;
Data.AddListener<UnityAction>("BlackScreen", x =>
{
CrossFade(x, 0.32f);
});
root.SetOpacity(1);
root.SetActive(false);
}
void CrossFade(UnityAction action, float duration = 1f)
{
Sequence sequence = DOTween.Sequence();
var enterTween = DOTween.To(root.GetOpacity, root.SetOpacity, 1, duration);
var exitTween = DOTween.To(root.GetOpacity, root.SetOpacity, 0, duration);
sequence.Append(enterTween);
sequence.AppendInterval(0.1f);
sequence.Append(exitTween);
enterTween.onComplete += () =>
{
action.Invoke();
};
sequence.onComplete += () =>
{
root.SetActive(false);
};
root.SetActive(true);
root.SetOpacity(0);
sequence.Play();
}
}
}