Теперь Addressables и UniTask в InstantiateAsync и прочих процессах

This commit is contained in:
ThomasCharester committed 2025-11-03 12:31:26 +03:00
1 parent 6b1f24b72e
commit f940cbffe0
45 files changed
+811 -189

No files matched your search

@@ -0,0 +1,12 @@
using Code.App.StateMachine;
using Code.StateMachine;
namespace Code.App.AppStates
{
public class BootstrapState : BaseState<AppStateMachineContext>
{
public BootstrapState(AppStateMachineContext context) : base(context)
{
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 0183e73a08c64332babded3df3d06b95
timeCreated: 1762122962
@@ -22,17 +22,19 @@ namespace Code.App.AppStates
public override void EnterState()
{
_uiLoaded = _sceneLoaded = false;
Context.UIService.LoadingScreen.Show();
Context.SceneLoaderService.LoadSceneAsync((int)SceneIds.MovementDev, callBack: OnSceneLoaded); // TODO Фабрика говна Nada
_playerMoney = Context.PlayerFactory.CreatePlayerMoney();
_playerHealth = Context.PlayerFactory.CreatePlayerHealth();
_playerHealth.OnDeath += EndGame;
Context.SceneLoaderService.LoadSceneAsync((int)SceneIds.MovementDev, callBack: OnSceneLoaded); // TODO Фабрика игрока Nada
Context.UIService.SwitchPanelTo(UIPanel.Gameplay, callback: PostUILoaded);
_playerMoney = Context.PlayerFactory.CreatePlayerMoney();
_playerHealth = Context.PlayerFactory.CreatePlayerHealth();
_playerHealth.OnDeath += EndGame;
Context.InputService.DebugBindAction((int)ActionMappings.Debug, (int)DebugActions.GiveHealth, GiveHealth);
Context.InputService.DebugBindAction((int)ActionMappings.Debug, (int)DebugActions.GiveMoney, GiveMoney);
Context.InputService.DebugBindAction((int)ActionMappings.Debug, (int)DebugActions.LooseHealth, LooseHealth);
@@ -47,8 +49,7 @@ namespace Code.App.AppStates
private void PostUILoaded()
{
((GameplayPanel)(Context.UIService.CurrentPanel)).Initialize(_playerMoney, _playerHealth);
Context.UIService.LoadingScreen.Hide();
((GameplayPanel)Context.UIService.CurrentPanel).Initialize(_playerMoney, _playerHealth);
_uiLoaded = true;
CheckIfLoaded();
}
@@ -21,8 +21,7 @@ namespace Code.App.AppStates
public override void EnterState()
{
_uiLoaded = false;
_sceneLoaded = false;
_uiLoaded = _sceneLoaded = false;
Context.UIService.LoadingScreen.Show();
@@ -1,20 +1,25 @@
using System.Threading.Tasks;
using Code.App.StateMachine;
using Code.Configs;
using Code.Constants;
using Code.Gameplay.Camera;
using Code.Gameplay.Player;
using Code.Input;
using Code.Scenes;
using Code.StaticData;
using Code.UI;
using Code.UI.Panels;
using Cysharp.Threading.Tasks;
using Reflex.Core;
using Reflex.Injectors;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.InputSystem;
namespace Code.App
{
public class BootstrapInstaller : MonoBehaviour, IInstaller
{
public void InstallBindings(ContainerBuilder containerBuilder)
{
InstallStaticData(containerBuilder, out IStaticDataService staticDataService);
@@ -37,8 +42,10 @@ namespace Code.App
// CameraService cameraService = Instantiate(staticDataService.CameraConfig.cameraPrefab);
// containerBuilder.AddSingleton(cameraService);
// DontDestroyOnLoad(cameraService);
Camera camera = Instantiate(staticDataService.CameraConfig.cameraPrefab);
// var task = staticDataService.InstantiateAsset<Camera>(staticDataService.CameraConfig.cameraAssetReference);
Camera camera = Instantiate(staticDataService.CameraConfig.cameraPrefab);
// Camera camera = task.Result;
DontDestroyOnLoad(camera);
containerBuilder.AddSingleton(camera);
}
@@ -50,7 +57,7 @@ namespace Code.App
containerBuilder.AddSingleton(infrastructureUnit);
containerBuilder.AddSingleton(infrastructureUnit.AddComponent<CoroutineRunner>(),
typeof(ICoroutineRunner));
typeof(ICoroutineRunner));
containerBuilder.AddSingleton(infrastructureUnit.AddComponent<AppStateMachine>());
containerBuilder.AddSingleton(infrastructureUnit.AddComponent<CameraStateMachine>());
}
@@ -61,29 +68,42 @@ namespace Code.App
staticDataService.Load();
containerBuilder.AddSingleton(staticDataService, typeof(IStaticDataService));
// containerBuilder.AddSingleton(staticDataService.InputConfig.inputActionAsset, typeof(InputActionAsset));
containerBuilder.AddSingleton(staticDataService.InputConfig.inputActionAsset, typeof(InputActionAsset));
containerBuilder.AddSingleton(staticDataService.InputConfig, typeof(InputConfig));
containerBuilder.AddSingleton(staticDataService.CameraConfig, typeof(CameraConfig));
containerBuilder.AddSingleton(staticDataService.PlayerConfig, typeof(PlayerConfig));
containerBuilder.AddSingleton(staticDataService.CharacterConfig, typeof(CharacterConfig));
containerBuilder.AddSingleton(staticDataService.UIConfig, typeof(UIConfig));
// containerBuilder.AddSingleton(staticDataService.UIConfig.canvasPrefab, typeof(Canvas));
// containerBuilder.AddSingleton(staticDataService.UIConfig.uiElementsPrefabs, typeof(UIElement[]));
}
private bool _onUiLoaded;
private AppStateMachine _appStateMachine;
private void Bootstrap(Container container)
{
AppStateMachine appStateMachine = container.Resolve<AppStateMachine>();
appStateMachine.Initialize();
AttributeInjector.Inject(appStateMachine.StateMachineContext, container);
_appStateMachine = container.Resolve<AppStateMachine>();
_appStateMachine.Initialize();
AttributeInjector.Inject(_appStateMachine.StateMachineContext, container);
CameraStateMachine cameraStateMachine = container.Resolve<CameraStateMachine>();
AttributeInjector.Inject(cameraStateMachine.StateMachineContext, container);
cameraStateMachine.Initialize();
var loadingScreen = GameObject.FindWithTag(Tags.LoadingScreen);
DontDestroyOnLoad(loadingScreen);
IUIService uiService = container.Resolve<IUIService>();
uiService.Initialize(loadingScreen.GetComponent<LoadingScreen>());
TryStartGame();
}
private void TryStartGame()
{
// TODO: поменяно для теста
appStateMachine
_appStateMachine
.SwitchState(AppState.MainMenu)
.AllowStateTransition(AppState.Gameplay);
}
@@ -7,6 +7,7 @@ namespace Code.App.StateMachine
{
public override void Initialize()
{
States.Add(AppState.Bootstrap, new BootstrapState(StateMachineContext));
States.Add(AppState.MainMenu, new MainMenuState(StateMachineContext));
States.Add(AppState.Gameplay, new GameplayState(StateMachineContext));
States.Add(AppState.GameOver, new GameOverState(StateMachineContext));
@@ -16,6 +17,8 @@ namespace Code.App.StateMachine
AllowedStates |= AppState.MainMenu;
AllowedStates |= AppState.Gameplay;
AllowedStates |= AppState.MovementDev;
CurrentBaseState = States[AppState.Bootstrap];
}
protected override bool CheckTransitionAllowed(AppState nextState) => AllowedStates.HasFlag(nextState);
@@ -1,4 +1,5 @@
using UnityEngine;
using UnityEngine.AddressableAssets;
namespace Code.Configs
{
@@ -1,7 +1,5 @@
using Code.UI;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.Serialization;
namespace Code.Configs
{
@@ -6,4 +6,8 @@ namespace Code.Constants
public const string ConfigMultiple = "Configs/{0}";
}
public static class Tags
{
public const string LoadingScreen = "LoadingScreen";
}
}
@@ -12,6 +12,8 @@ namespace Code.Gameplay.Camera
States.Add(CameraMode.Static, new StaticState(StateMachineContext));
States.Add(CameraMode.FirstPerson, new FirstPersonState(StateMachineContext));
States.Add(CameraMode.ThirdPerson, new ThirdPersonState(StateMachineContext));
CurrentBaseState = States[CameraMode.Static];
}
@@ -1,9 +1,5 @@
using System;
using System.Collections;
using Code.App;
using Cysharp.Threading.Tasks;
using Reflex.Attributes;
using UnityEngine;
using UnityEngine.SceneManagement;
namespace Code.Scenes
@@ -13,22 +9,25 @@ namespace Code.Scenes
public async UniTaskVoid LoadSceneAsync(string sceneName, float waitFor = 0f, Action callBack = null)
{
// if (waitFor > 0f)
// yield return new WaitForSeconds(waitFor);
// yield return new WaitForSeconds(waitFor);
// AsyncOperation waitNextScene = SceneManager.LoadSceneAsync(sceneName, LoadSceneMode.Single);
// while (!waitNextScene.isDone)
// yield return null;
// yield return null;
// callback?.Invoke();
await UniTask.Delay(TimeSpan.FromSeconds(waitFor));
await SceneManager.LoadSceneAsync(sceneName, LoadSceneMode.Single);
await SceneManager.LoadSceneAsync(sceneName, LoadSceneMode.Single).ToUniTask();
callBack?.Invoke();
}
public async UniTaskVoid LoadSceneAsync(int sceneId, float waitFor = 0f, Action callBack = null)
public async UniTaskVoid LoadSceneAsync(int sceneId, float waitFor = 0f, Action callBack = null)
{
await UniTask.Delay(TimeSpan.FromSeconds(waitFor));
await SceneManager.LoadSceneAsync(sceneId, LoadSceneMode.Single);
await UniTask.WhenAll(
UniTask.Delay(TimeSpan.FromSeconds(waitFor)),
SceneManager.LoadSceneAsync(sceneId, LoadSceneMode.Single).ToUniTask());
callBack?.Invoke();
}
}
}
}
@@ -8,8 +8,8 @@ namespace Code.StateMachine
: MonoBehaviour
where TStateMachineContext : new()
{
public TStateEnum CurrentState { get; private set; }
public BaseState<TStateMachineContext> CurrentBaseState { get; private set; }
public TStateEnum CurrentState { get; protected set; }
public BaseState<TStateMachineContext> CurrentBaseState { get; protected set; }
private BaseState<TStateMachineContext> _previousBaseState;
protected Dictionary<TStateEnum, BaseState<TStateMachineContext>> States = new();
@@ -1,6 +1,10 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Code.Configs;
using Cysharp.Threading.Tasks;
using UnityEngine;
using UnityEngine.AddressableAssets;
namespace Code.StaticData
{
@@ -13,5 +17,6 @@ namespace Code.StaticData
CharacterConfig CharacterConfig { get; }
UIConfig UIConfig { get; }
PlayerConfig PlayerConfig { get; }
Task<T> InstantiateAsset<T>(AssetReference assetReference) where T : Behaviour;
}
}
@@ -1,6 +1,9 @@
using System.Threading.Tasks;
using Code.Configs;
using Code.Constants;
using UnityEngine;
using Code.Constants;
using Cysharp.Threading.Tasks;
using UnityEngine;
using UnityEngine.AddressableAssets;
namespace Code.StaticData
{
@@ -22,5 +25,10 @@ namespace Code.StaticData
UIConfig = Resources.Load<UIConfig>(string.Format(StringPaths.ConfigSingle, "App", "UIConfig"));
PlayerConfig = Resources.Load<PlayerConfig>(string.Format(StringPaths.ConfigSingle, "App", "PlayerConfig"));
}
public async Task<T> InstantiateAsset<T>(AssetReference assetReference) where T : Behaviour
{
return (await Addressables.InstantiateAsync(assetReference).ToUniTask()).GetComponent<T>();
}
}
}
+3 -1
View File
@@ -1,4 +1,5 @@
using System;
using Code.UI.Panels;
namespace Code.UI
{
@@ -6,6 +7,7 @@ namespace Code.UI
{
void SwitchPanelTo(UIPanel uiPanel, Action callback = null);
UIElement CurrentPanel { get; }
LoadingPanel LoadingScreen { get; }
LoadingScreen LoadingScreen { get; }
void Initialize(LoadingScreen loadingScreen);
}
}
@@ -1,18 +0,0 @@
using UnityEngine;
using UnityEngine.UI;
namespace Code.UI
{
public class LoadingPanel : UIElement
{
[SerializeField] private Image _background;
public override void Hide()
{
_background.enabled = false;
}
public override void Show()
{
_background.enabled = true;
}
}
}
@@ -0,0 +1,22 @@
using TMPro;
using UnityEngine;
using UnityEngine.UI;
namespace Code.UI.Panels
{
public class LoadingScreen : UIElement
{
[SerializeField] private Image background;
[SerializeField] private TMP_Text loadingTitle;
public override void Hide()
{
background.enabled = false;
loadingTitle.enabled = false;
}
public override void Show()
{
background.enabled = true;
loadingTitle.enabled = true;
}
}
}
+8 -6
View File
@@ -1,4 +1,6 @@
using System;
using System.Threading;
using Code.UI.Panels;
using Cysharp.Threading.Tasks;
using UnityEngine;
using Object = UnityEngine.Object;
@@ -11,24 +13,24 @@ namespace Code.UI
private IUIFactory _factory;
public UIElement CurrentPanel { get; private set; }
public LoadingPanel LoadingScreen { get; private set; }
public LoadingScreen LoadingScreen { get; private set; }
public UIService(IUIFactory factory)
{
_factory = factory;
Initialize();
}
private async void Initialize()
public async void Initialize(LoadingScreen loadingScreen)
{
_canvas = await _factory.CreateCanvas();
LoadingScreen = loadingScreen;
LoadingScreen.Show();
LoadingScreen = (LoadingPanel) await _factory.CreatePanel(_canvas,UIPanel.Loading);
_canvas = await _factory.CreateCanvas();
}
public async void SwitchPanelTo(UIPanel uiPanel, Action callback = null)
{
if (!_canvas) await UniTask.WaitUntilValueChanged(_canvas, _ => !_canvas);
if (!_canvas) await UniTask.WaitUntil(() => _canvas );
if(CurrentPanel) Object.Destroy(CurrentPanel.gameObject);