Init
This commit is contained in:
1 parent
a73338916a
commit
fbc1cc8d3e
207 files changed
+10327
-2
No files matched your search
@@ -0,0 +1,94 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Code.Infrastructure.Configs;
|
||||
using Code.Infrastructure.EventBus;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
using UnityEngine.AddressableAssets;
|
||||
using UnityEngine.ResourceManagement.AsyncOperations;
|
||||
|
||||
namespace Code.Infrastructure.UI
|
||||
{
|
||||
public interface IUIFactory
|
||||
{
|
||||
UniTaskVoid LoadPanel(UIPanelType panelType);
|
||||
void ReleasePanel(UIPanel panel);
|
||||
Canvas Canvas { get; }
|
||||
}
|
||||
|
||||
public class UIFactory : IDisposable, IUIFactory
|
||||
{
|
||||
private Dictionary<UIPanelType, UIPanelSettingsConfig> _configs;
|
||||
private AssetReference _canvasConfigReference;
|
||||
private string _configsLabel;
|
||||
private EventBus.EventBus _eventBus;
|
||||
|
||||
private AsyncOperationHandle<IList<UIPanelSettingsConfig>> _configsHandle;
|
||||
private AsyncOperationHandle<CanvasSettingsConfig> _canvasSettingsHandle;
|
||||
private CanvasSettingsConfig _canvasConfig;
|
||||
|
||||
private GameObject _canvas;
|
||||
|
||||
public Canvas Canvas { get; private set; }
|
||||
|
||||
public UIFactory(IConfigLocator configLocator, EventBus.EventBus eventBus)
|
||||
{
|
||||
_eventBus = eventBus;
|
||||
_configsLabel = configLocator.TryGetManyConfigAssetReferences<UIPanelSettingsConfig>();
|
||||
_canvasConfigReference = configLocator.TryGetConfigAssetReference<CanvasSettingsConfig>();
|
||||
|
||||
LoadConfigs().Forget();
|
||||
}
|
||||
|
||||
public async UniTaskVoid LoadPanel(UIPanelType panelType)
|
||||
{
|
||||
var panel = (await Addressables.InstantiateAsync(_configs[panelType].Prefab, _canvas.transform).ToUniTask())
|
||||
.GetComponent<UIPanel>();
|
||||
|
||||
UnityEngine.Debug.LogWarning($"Panel loaded {panelType}");
|
||||
_eventBus.RaiseEvent<IUIPanelLoadedHandle>(ui => ui.UIPanelLoaded(panel, panelType));
|
||||
}
|
||||
|
||||
public void ReleasePanel(UIPanel panel)
|
||||
{
|
||||
UnityEngine.Debug.LogWarning($"Panel released {panel.gameObject.name}");
|
||||
Addressables.ReleaseInstance(panel.gameObject);
|
||||
}
|
||||
|
||||
private async UniTaskVoid LoadConfigs()
|
||||
{
|
||||
_configsHandle = Addressables.LoadAssetsAsync<UIPanelSettingsConfig>(_configsLabel);
|
||||
_canvasSettingsHandle = Addressables.LoadAssetAsync<CanvasSettingsConfig>(_canvasConfigReference);
|
||||
|
||||
await _configsHandle.ToUniTask();
|
||||
await _canvasSettingsHandle.ToUniTask();
|
||||
|
||||
_canvasConfig = _canvasSettingsHandle.Result;
|
||||
|
||||
_configs = _configsHandle.Result.ToDictionary(c => c.UIPanelType);
|
||||
|
||||
_canvas = await Addressables.InstantiateAsync(_canvasConfig.Prefab).ToUniTask();
|
||||
UnityEngine.Object.DontDestroyOnLoad(_canvas);
|
||||
|
||||
Canvas = _canvas.GetComponent<Canvas>();
|
||||
|
||||
_eventBus.RaiseEvent<IUIInitializedHandle>(ui => ui.UIInitialized());
|
||||
}
|
||||
public void Dispose()
|
||||
{
|
||||
Addressables.Release(_configsHandle);
|
||||
Addressables.Release(_canvasSettingsHandle);
|
||||
Addressables.ReleaseInstance(_canvas);
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
@@ -0,0 +1,13 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Code.Infrastructure.UI
|
||||
{
|
||||
public abstract class UIPanel : MonoBehaviour
|
||||
{
|
||||
[SerializeField] protected bool injectMeSenpai;
|
||||
public bool InjectMeSenpai => injectMeSenpai;
|
||||
public abstract void Open(Action callback = null);
|
||||
public abstract void Close(Action callback = null);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4464e0158e044f5985c45cb73250cf43
|
||||
timeCreated: 1768980188
|
||||
@@ -0,0 +1,18 @@
|
||||
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;
|
||||
}
|
||||
|
||||
[Config("Configs/UI/CanvasSettings")]
|
||||
public class CanvasSettings
|
||||
{
|
||||
[ToConfig] public AssetReference Prefab;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 996c214bb8624a64b6c563f0c13320ac
|
||||
timeCreated: 1768979468
|
||||
@@ -0,0 +1,15 @@
|
||||
namespace Code.Infrastructure.UI
|
||||
{
|
||||
public enum UIPanelType
|
||||
{
|
||||
Logo,
|
||||
Loading,
|
||||
DebugSceneChooser,
|
||||
// Race
|
||||
PlayerScoreCounter,
|
||||
WinScreen,
|
||||
PlayerScores,
|
||||
//
|
||||
DebugMenu
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e33d3007130a4cbbac8a5e962d5c4806
|
||||
timeCreated: 1768979170
|
||||
@@ -0,0 +1,88 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Code.Infrastructure.EventBus;
|
||||
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 uiPanelType);
|
||||
void SetCanvasCamera(UnityEngine.Camera camera);
|
||||
UIPanel TryGetPanel(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 SetCanvasCamera(UnityEngine.Camera camera)
|
||||
{
|
||||
_factory.Canvas.worldCamera = camera;
|
||||
_factory.Canvas.planeDistance = 0.3f;
|
||||
}
|
||||
|
||||
public void OpenPanel(UIPanelType uiPanelType)
|
||||
{
|
||||
_loadedPanels[uiPanelType].gameObject.SetActive(true);
|
||||
_loadedPanels[uiPanelType].Open();
|
||||
}
|
||||
|
||||
// TODO Close event
|
||||
public void ClosePanel(UIPanelType uiPanelType)
|
||||
{
|
||||
_loadedPanels[uiPanelType].Close(() => { _loadedPanels[uiPanelType].gameObject.SetActive(false); });
|
||||
}
|
||||
|
||||
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()); // Скорее всего будут нужны компоненты текущей сцены
|
||||
|
||||
_eventBus.RaiseEvent<IUIPanelLoaded>(ui => ui.UILoaded(panelType));
|
||||
}
|
||||
|
||||
public void TryRemovePanel(UIPanelType uiPanelType)
|
||||
{
|
||||
_loadedPanels[uiPanelType].Close(() =>
|
||||
{
|
||||
_loadedPanels[uiPanelType].gameObject.SetActive(false);
|
||||
RemovePanel(uiPanelType);
|
||||
});
|
||||
}
|
||||
|
||||
private void RemovePanel(UIPanelType panelType)
|
||||
{
|
||||
_factory.ReleasePanel(_loadedPanels[panelType]);
|
||||
_loadedPanels.Remove(panelType);
|
||||
}
|
||||
|
||||
public UIPanel TryGetPanel(UIPanelType panelType) => _loadedPanels.GetValueOrDefault(panelType);
|
||||
}
|
||||
|
||||
public interface IUIPanelLoaded : IGlobalSubscriber
|
||||
{
|
||||
void UILoaded(UIPanelType uiPanelType);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0bcb777d18014c428f24113d73787e38
|
||||
timeCreated: 1768980594
|
||||
Reference in new issue
Block a user