UI Started

This commit is contained in:
ThomasCharester committed 2026-01-21 10:58:05 +03:00
1 parent a3cd3e86a1
commit 12a27f85c5
26 files changed
+478 -24

No files matched your search

@@ -0,0 +1,70 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Code.Infrastructure.Configs;
using Code.Infrastructure.EventBus;
using Cysharp.Threading.Tasks;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;
namespace Code.Infrastructure.UI
{
public interface IUIFactory
{
UniTaskVoid LoadPanel(UIPanelType panelType);
void ReleasePanel(UIPanel panel);
}
public class UIFactory : IDisposable, IUIFactory
{
private Dictionary<UIPanelType, UIPanelSettingsConfig> _configs;
private string _configsLabel;
private EventBus.EventBus _eventBus;
private AsyncOperationHandle<IList<UIPanelSettingsConfig>> _configsHandle;
public UIFactory(IConfigLocator configLocator, EventBus.EventBus eventBus)
{
_eventBus = eventBus;
_configsLabel = configLocator.TryGetManyConfigAssetReferences<UIPanelSettingsConfig>();
LoadConfigs().Forget();
}
public async UniTaskVoid LoadPanel(UIPanelType panelType)
{
var panel = (await Addressables.InstantiateAsync(_configs[panelType]).ToUniTask()).GetComponent<UIPanel>();
_eventBus.RaiseEvent<IUIPanelLoadedHandle>(ui => ui.UIPanelLoaded(panel, panelType));
}
public void ReleasePanel(UIPanel panel)
{
Addressables.ReleaseInstance(panel.gameObject);
}
private async UniTaskVoid LoadConfigs()
{
_configsHandle = Addressables.LoadAssetsAsync<UIPanelSettingsConfig>(_configsLabel);
await _configsHandle.ToUniTask();
_configs = _configsHandle.Result.ToDictionary(c => c.UIPanelType);
_eventBus.RaiseEvent<IUIInitializedHandle>(ui => ui.UIInitialized());
}
public void Dispose()
{
Addressables.Release(_configsHandle);
}
}
public interface IUIInitializedHandle : IGlobalSubscriber
{
void UIInitialized();
}
public interface IUIPanelLoadedHandle : IGlobalSubscriber
{
void UIPanelLoaded(UIPanel panel, UIPanelType panelType);
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 8447b81247b24f1b869b0067aeeff0b8
timeCreated: 1768979324
+13
View File
@@ -0,0 +1,13 @@
using System.ComponentModel;
using UnityEngine;
namespace Code.Infrastructure.UI
{
public abstract class UIPanel : MonoBehaviour
{
[field: Category("UIPanel")] public bool InjectMeSenpai { get; protected set; }
public abstract void Open();
public abstract void Close();
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 4464e0158e044f5985c45cb73250cf43
timeCreated: 1768980188
@@ -0,0 +1,12 @@
using Code.Infrastructure.Generators;
using UnityEngine.AddressableAssets;
namespace Code.Infrastructure.UI
{
[Config("Configs/UI/UIPanelSettings", true)]
public class UIPanelSettings
{
[ToConfig] public AssetReference Prefab;
[ToConfig] public UIPanelType UIPanelType;
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 996c214bb8624a64b6c563f0c13320ac
timeCreated: 1768979468
@@ -0,0 +1,9 @@
namespace Code.Infrastructure.UI
{
public enum UIPanelType
{
Logo,
Loading,
DebugMenu
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: e33d3007130a4cbbac8a5e962d5c4806
timeCreated: 1768979170
@@ -0,0 +1,66 @@
using System.Collections.Generic;
using Reflex.Core;
using Reflex.Extensions;
using Reflex.Injectors;
using UnityEngine.SceneManagement;
namespace Code.Infrastructure.UI
{
public interface IUIService
{
void OpenPanel(UIPanelType uiPanelType);
void ClosePanel(UIPanelType uiPanelType);
void TryLoadPanel(UIPanelType uiPanelType);
void TryRemovePanel(UIPanelType panelType);
}
public class UIService : IUIPanelLoadedHandle, IUIService
{
private Dictionary<UIPanelType, UIPanel> _loadedPanels = new();
private EventBus.EventBus _eventBus;
private IUIFactory _factory;
public UIService(EventBus.EventBus eventBus, IUIFactory factory)
{
_eventBus = eventBus;
_factory = factory;
eventBus.Subscribe(this);
}
public void OpenPanel(UIPanelType uiPanelType)
{
_loadedPanels[uiPanelType].gameObject.SetActive(true); // TODO Братуха реально реши надо вкл\выкл или это очередная хренооптимизация
_loadedPanels[uiPanelType].Open();
}
public void ClosePanel(UIPanelType uiPanelType)
{
_loadedPanels[uiPanelType].gameObject.SetActive(false);
_loadedPanels[uiPanelType].Close();
}
public void TryLoadPanel(UIPanelType uiPanelType) => _factory.LoadPanel(uiPanelType);
public void UIPanelLoaded(UIPanel panel, UIPanelType panelType)
{
_loadedPanels.Add(panelType, panel);
if(panel.InjectMeSenpai)
GameObjectInjector.InjectRecursive(panel.gameObject, Container.RootContainer);
// GameObjectInjector.InjectRecursive(panel.gameObject, SceneManager.GetActiveScene().GetSceneContainer()); // Скорее всего будут нужны компоненты текущей сцены
}
public void TryRemovePanel(UIPanelType panelType)
{
//
RemovePanel(panelType);
}
private void RemovePanel(UIPanelType panelType)
{
_factory.ReleasePanel(_loadedPanels[panelType]);
_loadedPanels.Remove(panelType);
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 0bcb777d18014c428f24113d73787e38
timeCreated: 1768980594