Ui 기본 구조 추가

This commit is contained in:
NTG_Lenovo 2025-07-21 16:53:39 +09:00
parent 689a4fe095
commit 35dfe1a080
6 changed files with 299 additions and 0 deletions

View File

@ -0,0 +1,26 @@
using UnityEngine;
namespace DDD
{
public abstract class BaseUi : MonoBehaviour
{
public virtual bool IsBlockingTime => false;
public virtual bool IsOpen => gameObject.activeSelf;
protected virtual void Start()
{
TryRegister();
}
protected virtual void OnDestroy()
{
TryUnregister();
}
protected virtual void TryRegister() { }
protected virtual void TryUnregister() { }
public virtual void Open() => gameObject.SetActive(true);
public virtual void Close() => gameObject.SetActive(false);
}
}

View File

@ -0,0 +1,51 @@
using DG.Tweening;
using UnityEngine;
namespace DDD
{
public class FadeUi : MonoBehaviour, IEventHandler<FadeInEvent>, IEventHandler<FadeOutEvent>
{
private CanvasGroup _canvasGroup;
private void Awake()
{
_canvasGroup = GetComponent<CanvasGroup>();
_canvasGroup.alpha = 0f;
_canvasGroup.gameObject.SetActive(false);
EventBus.Register<FadeInEvent>(this);
EventBus.Register<FadeOutEvent>(this);
}
private void OnDestroy()
{
EventBus.Unregister<FadeInEvent>(this);
EventBus.Unregister<FadeOutEvent>(this);
}
public async void Invoke(FadeInEvent evt)
{
await _canvasGroup.DOFade(0f, evt.Duration)
.SetUpdate(true)
.AsyncWaitForCompletion();
_canvasGroup.blocksRaycasts = false;
_canvasGroup.gameObject.SetActive(false);
evt.CompletionSource.SetResult(true);
}
public async void Invoke(FadeOutEvent evt)
{
_canvasGroup.gameObject.SetActive(true);
_canvasGroup.blocksRaycasts = true;
await _canvasGroup.DOFade(1f, evt.Duration)
.SetUpdate(true)
.AsyncWaitForCompletion();
evt.CompletionSource.SetResult(true);
}
}
}

View File

@ -0,0 +1,68 @@
using System.Collections.Generic;
using DG.Tweening;
using TMPro;
using UnityEngine;
namespace DDD
{
public class GlobalMessageUi : BaseUi, IEventHandler<ShowGlobalMessageEvent>
{
private CanvasGroup _canvasGroup;
private TextMeshProUGUI _messageText;
private Tween _fadeTween;
private readonly Queue<ShowGlobalMessageEvent> _messageQueue = new();
private bool _isDisplayingMessage = false;
private void Awake()
{
_canvasGroup = GetComponent<CanvasGroup>();
_messageText = GetComponentInChildren<TextMeshProUGUI>();
_canvasGroup.alpha = 0;
_messageText.text = null;
EventBus.Register(this);
}
protected override void OnDestroy()
{
base.OnDestroy();
EventBus.Unregister(this);
_fadeTween?.Kill();
}
public void Invoke(ShowGlobalMessageEvent evt)
{
_messageQueue.Enqueue(evt);
TryDisplayNext();
}
private void TryDisplayNext()
{
if (_isDisplayingMessage || _messageQueue.Count == 0) return;
var evt = _messageQueue.Dequeue();
_isDisplayingMessage = true;
_messageText.text = LocalizationManager.GetString(TableName.Global_Message, evt.NewMessageKey);
Open();
_fadeTween?.Kill();
_fadeTween = DOTween.Sequence()
.Append(_canvasGroup.DOFade(1f, evt.FadeDuration))
.AppendInterval(evt.ShowDuration)
.Append(_canvasGroup.DOFade(0f, evt.FadeDuration))
.OnComplete(() =>
{
Close();
_fadeTween = null;
_isDisplayingMessage = false;
// 다음 메시지 처리
TryDisplayNext();
});
}
}
}

View File

@ -0,0 +1,7 @@
namespace DDD
{
public class PopupUi : BaseUi
{
}
}

View File

@ -0,0 +1,7 @@
namespace DDD
{
public class ScreenUi : BaseUi
{
}
}

View File

@ -0,0 +1,140 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace DDD
{
public class UiManager : Singleton<UiManager>, IManager, IEventHandler<OpenScreenUiEvent>, IEventHandler<CloseScreenUiEvent>,
IEventHandler<OpenPopupUiEvent>, IEventHandler<ClosePopupUiEvent>
{
private readonly Dictionary<Type, ScreenUi> _screenUIs = new();
private readonly Dictionary<Type, PopupUi> _popupUIs = new();
private readonly object _uiPauseRequester = new();
public void PreInit()
{
EventBus.Register<OpenScreenUiEvent>(this);
EventBus.Register<CloseScreenUiEvent>(this);
EventBus.Register<OpenPopupUiEvent>(this);
EventBus.Register<ClosePopupUiEvent>(this);
}
public Task Init()
{
return Task.CompletedTask;
}
public void PostInit()
{
}
private void OnDestroy()
{
EventBus.Unregister<OpenScreenUiEvent>(this);
EventBus.Unregister<CloseScreenUiEvent>(this);
EventBus.Unregister<OpenPopupUiEvent>(this);
EventBus.Unregister<ClosePopupUiEvent>(this);
}
public void RegisterScreenUI(ScreenUi ui)
{
var type = ui.GetType();
_screenUIs.TryAdd(type, ui);
}
public void UnregisterScreenUI(ScreenUi ui)
{
var type = ui.GetType();
if (_screenUIs.TryGetValue(type, out var value) && value == ui)
{
_screenUIs.Remove(type);
}
}
public void RegisterPopupUI(PopupUi ui)
{
var type = ui.GetType();
_popupUIs.TryAdd(type, ui);
}
public void UnregisterPopupUI(PopupUi ui)
{
var type = ui.GetType();
if (_popupUIs.TryGetValue(type, out var registered) && registered == ui)
{
_popupUIs.Remove(type);
}
}
private void CloseAllScreenUIs()
{
foreach (var screen in _screenUIs.Values)
{
if (screen.IsOpen)
{
screen.Close();
if (screen.IsBlockingTime)
{
EventBus.Broadcast(new TimeScaleChangeEvent(_uiPauseRequester, 1f));
}
}
}
}
public void Invoke(OpenScreenUiEvent evt)
{
if (_screenUIs.TryGetValue(evt.UiType, out var screen))
{
CloseAllScreenUIs();
screen.Open();
if (screen.IsBlockingTime)
{
EventBus.Broadcast(new TimeScaleChangeEvent(screen, 0f));
}
}
}
public void Invoke(CloseScreenUiEvent evt)
{
if (_screenUIs.TryGetValue(evt.UiType, out var screen))
{
screen.Close();
if (screen.IsBlockingTime)
{
EventBus.Broadcast(new TimeScaleChangeEvent(screen, 1f));
}
}
}
public void Invoke(OpenPopupUiEvent evt)
{
if (_popupUIs.TryGetValue(evt.UiType, out var popup))
{
popup.Open();
if (popup.IsBlockingTime)
{
EventBus.Broadcast(new TimeScaleChangeEvent(popup, 0f));
}
}
}
public void Invoke(ClosePopupUiEvent evt)
{
if (_screenUIs.TryGetValue(evt.UiType, out var popUp))
{
popUp.Close();
if (popUp.IsBlockingTime)
{
EventBus.Broadcast(new TimeScaleChangeEvent(popUp, 1f));
}
}
}
}
}