UI service frame
This commit is contained in:
1 parent
12a27f85c5
commit
e2d0974b3c
45 files changed
+1138
-927
No files matched your search
@@ -4,6 +4,7 @@ using System.Linq;
|
||||
using Code.Infrastructure.Configs;
|
||||
using Code.Infrastructure.EventBus;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
using UnityEngine.AddressableAssets;
|
||||
using UnityEngine.ResourceManagement.AsyncOperations;
|
||||
|
||||
@@ -18,22 +19,27 @@ namespace Code.Infrastructure.UI
|
||||
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 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]).ToUniTask()).GetComponent<UIPanel>();
|
||||
var panel = (await Addressables.InstantiateAsync(_configs[panelType].Prefab, _canvas.transform).ToUniTask()).GetComponent<UIPanel>();
|
||||
|
||||
_eventBus.RaiseEvent<IUIPanelLoadedHandle>(ui => ui.UIPanelLoaded(panel, panelType));
|
||||
}
|
||||
@@ -46,16 +52,25 @@ namespace Code.Infrastructure.UI
|
||||
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);
|
||||
|
||||
_eventBus.RaiseEvent<IUIInitializedHandle>(ui => ui.UIInitialized());
|
||||
}
|
||||
public void Dispose()
|
||||
{
|
||||
Addressables.Release(_configsHandle);
|
||||
Addressables.Release(_canvasSettingsHandle);
|
||||
Addressables.ReleaseInstance(_canvas);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user