48 lines
1.2 KiB
C#
48 lines
1.2 KiB
C#
using DG.Tweening;
|
|
using UnityEngine;
|
|
|
|
public class BackgroundDimmer : MonoBehaviour
|
|
{
|
|
[SerializeField] private CanvasGroup _canvasGroup;
|
|
[SerializeField] private CanvasGroup _menuBackground;
|
|
[SerializeField, Range(0f, 1f)] private float _gameplayAlpha = 0.3f;
|
|
[SerializeField, Min(0f)] private float _fadeDuration = 1f;
|
|
|
|
private Tween _dimmerFadeTween;
|
|
private Tween _menuBackgroundFadeTween;
|
|
|
|
private void Awake()
|
|
{
|
|
_canvasGroup.alpha = 0f;
|
|
_menuBackground.alpha = 1f;
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
_dimmerFadeTween?.Kill();
|
|
_menuBackgroundFadeTween?.Kill();
|
|
}
|
|
|
|
public void ShowGameplay()
|
|
=> FadeTo(_gameplayAlpha, 0f);
|
|
|
|
public void ShowMenu()
|
|
=> FadeTo(0f, 1f);
|
|
|
|
private void FadeTo(float dimmerAlpha, float menuBackgroundAlpha)
|
|
{
|
|
_dimmerFadeTween?.Kill();
|
|
_menuBackgroundFadeTween?.Kill();
|
|
|
|
_dimmerFadeTween = _canvasGroup
|
|
.DOFade(dimmerAlpha, _fadeDuration)
|
|
.SetUpdate(true);
|
|
_menuBackgroundFadeTween = _menuBackground
|
|
.DOFade(menuBackgroundAlpha, _fadeDuration)
|
|
.SetUpdate(true);
|
|
|
|
_dimmerFadeTween.PlayForward();
|
|
_menuBackgroundFadeTween.PlayForward();
|
|
}
|
|
}
|