Addressables in process

This commit is contained in:
ThomasCharester committed 2025-11-02 23:29:29 +03:00
1 parent 1ad87e7432
commit 6b1f24b72e
13 files changed
+122 -77

No files matched your search

@@ -5,7 +5,6 @@ using Code.Input;
using Code.Scenes; using Code.Scenes;
using Code.StateMachine; using Code.StateMachine;
using Code.UI; using Code.UI;
using UnityEngine;
using UnityEngine.InputSystem; using UnityEngine.InputSystem;
namespace Code.App.AppStates namespace Code.App.AppStates
@@ -15,6 +14,8 @@ namespace Code.App.AppStates
private PlayerHealth _playerHealth; private PlayerHealth _playerHealth;
private PlayerMoney _playerMoney; private PlayerMoney _playerMoney;
private bool _uiLoaded, _sceneLoaded;
public GameplayState(AppStateMachineContext context) : base(context) public GameplayState(AppStateMachineContext context) : base(context)
{ {
} }
@@ -23,12 +24,9 @@ namespace Code.App.AppStates
{ {
Context.UIService.LoadingScreen.Show(); Context.UIService.LoadingScreen.Show();
Context.SceneLoaderService.LoadScene((int)SceneIds.MovementDev, SceneLoaded); // TODO Фабрика говна Nada Context.SceneLoaderService.LoadSceneAsync((int)SceneIds.MovementDev, callBack: OnSceneLoaded); // TODO Фабрика говна Nada
Context.InputService.EnableMap(ActionMappings.Gameplay);
Context.UIService.SwitchPanelTo(UIPanel.Gameplay, callback: PostUILoaded);
Context.UIService.SwitchPanelTo(UIPanel.Gameplay);
Context.CameraStateMachine.SwitchState(CameraMode.FirstPerson);
_playerMoney = Context.PlayerFactory.CreatePlayerMoney(); _playerMoney = Context.PlayerFactory.CreatePlayerMoney();
_playerHealth = Context.PlayerFactory.CreatePlayerHealth(); _playerHealth = Context.PlayerFactory.CreatePlayerHealth();
@@ -40,16 +38,32 @@ namespace Code.App.AppStates
Context.InputService.DebugBindAction((int)ActionMappings.Debug, (int)DebugActions.LooseHealth, LooseHealth); Context.InputService.DebugBindAction((int)ActionMappings.Debug, (int)DebugActions.LooseHealth, LooseHealth);
Context.InputService.DebugBindAction((int)ActionMappings.Debug, (int)DebugActions.LooseMoney, LooseMoney); Context.InputService.DebugBindAction((int)ActionMappings.Debug, (int)DebugActions.LooseMoney, LooseMoney);
} }
private void OnSceneLoaded()
{
_sceneLoaded = true;
CheckIfLoaded();
}
private void SceneLoaded() private void PostUILoaded()
{ {
((GameplayPanel)(Context.UIService.CurrentPanel)).Initialize(_playerMoney, _playerHealth); ((GameplayPanel)(Context.UIService.CurrentPanel)).Initialize(_playerMoney, _playerHealth);
Context.UIService.LoadingScreen.Hide(); Context.UIService.LoadingScreen.Hide();
_uiLoaded = true;
base.EnterState(); CheckIfLoaded();
} }
private void CheckIfLoaded()
{
if(!_uiLoaded || !_sceneLoaded) return;
Context.UIService.LoadingScreen.Hide();
Context.CameraStateMachine.SwitchState(CameraMode.FirstPerson);
Context.InputService.EnableMap(ActionMappings.Gameplay);
}
// Debug // Debug
private void GiveHealth(InputAction.CallbackContext context) private void GiveHealth(InputAction.CallbackContext context)
{ {
@@ -14,28 +14,37 @@ namespace Code.App.AppStates
{ {
public class MainMenuState : BaseState<AppStateMachineContext> public class MainMenuState : BaseState<AppStateMachineContext>
{ {
private bool _uiLoaded, _sceneLoaded;
public MainMenuState(AppStateMachineContext context) : base(context) public MainMenuState(AppStateMachineContext context) : base(context)
{ {
} }
public override void EnterState() public override void EnterState()
{ {
_uiLoaded = false;
_sceneLoaded = false;
Context.UIService.LoadingScreen.Show(); Context.UIService.LoadingScreen.Show();
Context.SceneLoaderService.LoadScene((int)SceneIds.MainMenu, SceneLoaded); Context.SceneLoaderService.LoadSceneAsync((int)SceneIds.MainMenu, callBack: SceneLoaded);
Context.InputService.EnableMap(ActionMappings.MainMenu);
Context.UIService.SwitchPanelTo(UIPanel.MainMenu, PostUILoaded);
Context.UIService.SwitchPanelTo(UIPanel.MainMenu);
((MainMenuPanel)Context.UIService.CurrentPanel).startButton.onClick.AddListener(StartGame);
((MainMenuPanel)Context.UIService.CurrentPanel).exitButton.onClick.AddListener(ExitGame);
Context.CameraStateMachine.SwitchState(CameraMode.Static); Context.CameraStateMachine.SwitchState(CameraMode.Static);
} }
private void PostUILoaded()
{
((MainMenuPanel)Context.UIService.CurrentPanel).startButton.onClick.AddListener(StartGame);
((MainMenuPanel)Context.UIService.CurrentPanel).exitButton.onClick.AddListener(ExitGame);
_uiLoaded = true;
CheckIfLoaded();
}
private void SceneLoaded() private void SceneLoaded()
{ {
Context.UIService.LoadingScreen.Hide();
var hui = ((MainMenuEnvironment)SceneManager.GetActiveScene() var hui = ((MainMenuEnvironment)SceneManager.GetActiveScene()
.GetSceneContainer().Resolve<CurrentSceneEnvironment>()); .GetSceneContainer().Resolve<CurrentSceneEnvironment>());
@@ -45,8 +54,21 @@ namespace Code.App.AppStates
((BaseCameraState)Context.CameraStateMachine.CurrentBaseState).SetFollowTransform(hui.cameraPosition); ((BaseCameraState)Context.CameraStateMachine.CurrentBaseState).SetFollowTransform(hui.cameraPosition);
base.EnterState(); base.EnterState();
_sceneLoaded = true;
CheckIfLoaded();
} }
private void CheckIfLoaded()
{
if(!_uiLoaded || !_sceneLoaded) return;
Context.UIService.LoadingScreen.Hide();
Context.InputService.EnableMap(ActionMappings.MainMenu);
}
public override void Tick() public override void Tick()
{ {
} }
@@ -15,7 +15,7 @@ namespace Code.App.AppStates
public override void EnterState() public override void EnterState()
{ {
Context.SceneLoaderService.LoadScene((int)SceneIds.MovementDev, SceneLoaded); // Context.SceneLoaderService.LoadScene((int)SceneIds.MovementDev, SceneLoaded);
Context.InputService.EnableMap(ActionMappings.Gameplay); Context.InputService.EnableMap(ActionMappings.Gameplay);
Context.CameraStateMachine.SwitchState(CameraMode.FirstPerson); Context.CameraStateMachine.SwitchState(CameraMode.FirstPerson);
@@ -9,7 +9,6 @@ using Code.UI;
using Reflex.Core; using Reflex.Core;
using Reflex.Injectors; using Reflex.Injectors;
using UnityEngine; using UnityEngine;
using UnityEngine.InputSystem;
namespace Code.App namespace Code.App
{ {
@@ -62,12 +61,13 @@ namespace Code.App
staticDataService.Load(); staticDataService.Load();
containerBuilder.AddSingleton(staticDataService, typeof(IStaticDataService)); 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.CameraConfig, typeof(CameraConfig));
containerBuilder.AddSingleton(staticDataService.PlayerConfig, typeof(PlayerConfig)); containerBuilder.AddSingleton(staticDataService.PlayerConfig, typeof(PlayerConfig));
containerBuilder.AddSingleton(staticDataService.CharacterConfig, typeof(CharacterConfig)); containerBuilder.AddSingleton(staticDataService.CharacterConfig, typeof(CharacterConfig));
containerBuilder.AddSingleton(staticDataService.UIConfig.canvasPrefab, typeof(Canvas)); // containerBuilder.AddSingleton(staticDataService.UIConfig.canvasPrefab, typeof(Canvas));
containerBuilder.AddSingleton(staticDataService.UIConfig.uiElementsPrefabs, typeof(UIElement[])); // containerBuilder.AddSingleton(staticDataService.UIConfig.uiElementsPrefabs, typeof(UIElement[]));
} }
private void Bootstrap(Container container) private void Bootstrap(Container container)
@@ -1,5 +1,6 @@
using Code.UI; using Code.UI;
using UnityEngine; using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.Serialization; using UnityEngine.Serialization;
namespace Code.Configs namespace Code.Configs
@@ -7,7 +8,7 @@ namespace Code.Configs
[CreateAssetMenu(menuName = "Config/UI", fileName = "UIConfig")] [CreateAssetMenu(menuName = "Config/UI", fileName = "UIConfig")]
public class UIConfig : ScriptableObject public class UIConfig : ScriptableObject
{ {
[FormerlySerializedAs("uiElements")] public UIElement[] uiElementsPrefabs; public AssetReference[] uiPanelsAssetReferences;
[FormerlySerializedAs("canvas")] public Canvas canvasPrefab; public AssetReference canvasAssetReference;
} }
} }
@@ -1,10 +1,11 @@
using System; using System;
using Cysharp.Threading.Tasks;
namespace Code.Scenes namespace Code.Scenes
{ {
public interface ISceneLoaderService public interface ISceneLoaderService
{ {
void LoadScene(string sceneName, Action callback = null, float waitFor = 0f); UniTaskVoid LoadSceneAsync(string sceneName, float waitFor = 0f, Action callBack = null);
void LoadScene(int sceneId, Action callback = null, float waitFor = 0f); UniTaskVoid LoadSceneAsync(int sceneId, float waitFor = 0f, Action callBack = null);
} }
} }
@@ -1,6 +1,7 @@
using System; using System;
using System.Collections; using System.Collections;
using Code.App; using Code.App;
using Cysharp.Threading.Tasks;
using Reflex.Attributes; using Reflex.Attributes;
using UnityEngine; using UnityEngine;
using UnityEngine.SceneManagement; using UnityEngine.SceneManagement;
@@ -9,38 +10,25 @@ namespace Code.Scenes
{ {
public class SceneLoaderService : ISceneLoaderService public class SceneLoaderService : ISceneLoaderService
{ {
[Inject] private ICoroutineRunner _coroutineRunner; public async UniTaskVoid LoadSceneAsync(string sceneName, float waitFor = 0f, Action callBack = null)
public void LoadScene(string sceneName, Action callback = null, float waitFor = 0f) =>
_coroutineRunner.StartCoroutine(LoadSceneAsync(sceneName, callback, waitFor));
public void LoadScene(int sceneId, Action callback = null, float waitFor = 0f) =>
_coroutineRunner.StartCoroutine(LoadSceneAsync(sceneId, callback, waitFor));
private IEnumerator LoadSceneAsync(string sceneName, Action callback = null, float waitFor = 0f)
{ {
if (waitFor > 0f) // if (waitFor > 0f)
yield return new WaitForSeconds(waitFor); // yield return new WaitForSeconds(waitFor);
AsyncOperation waitNextScene = SceneManager.LoadSceneAsync(sceneName, LoadSceneMode.Single); // AsyncOperation waitNextScene = SceneManager.LoadSceneAsync(sceneName, LoadSceneMode.Single);
while (!waitNextScene.isDone) // while (!waitNextScene.isDone)
yield return null; // yield return null;
callback?.Invoke(); // callback?.Invoke();
await UniTask.Delay(TimeSpan.FromSeconds(waitFor));
await SceneManager.LoadSceneAsync(sceneName, LoadSceneMode.Single);
} }
private IEnumerator LoadSceneAsync(int sceneId, Action callback = null, float waitFor = 0f) public async UniTaskVoid LoadSceneAsync(int sceneId, float waitFor = 0f, Action callBack = null)
{ {
if (waitFor > 0f) await UniTask.Delay(TimeSpan.FromSeconds(waitFor));
yield return new WaitForSeconds(waitFor); await SceneManager.LoadSceneAsync(sceneId, LoadSceneMode.Single);
AsyncOperation waitNextScene = SceneManager.LoadSceneAsync(sceneId, LoadSceneMode.Single);
while (!waitNextScene.isDone)
yield return null;
callback?.Invoke();
} }
} }
} }
@@ -37,11 +37,9 @@ namespace Code.StateMachine
{ {
if (!CheckTransitionAllowed(nextState)) if (!CheckTransitionAllowed(nextState))
{ {
Debug.LogError("TransitionNotAllowed");
return null; return null;
} }
Debug.Log("Entered " + nextState);
ResetAllowedTransitions(); ResetAllowedTransitions();
_previousBaseState = CurrentBaseState; _previousBaseState = CurrentBaseState;
@@ -1,6 +1,6 @@
using Code.Configs; using Code.Configs;
using Code.Constants; using Code.Constants;
using UnityEngine; using UnityEngine;
namespace Code.StaticData namespace Code.StaticData
{ {
@@ -12,7 +12,7 @@ namespace Code.StaticData
public CharacterConfig CharacterConfig { get; private set; } public CharacterConfig CharacterConfig { get; private set; }
public UIConfig UIConfig { get; private set; } public UIConfig UIConfig { get; private set; }
public PlayerConfig PlayerConfig { get; private set; } public PlayerConfig PlayerConfig { get; private set; }
public void Load() public void Load()
{ {
InputConfig = Resources.Load<InputConfig>(string.Format(StringPaths.ConfigSingle, "App", "InputConfig")); InputConfig = Resources.Load<InputConfig>(string.Format(StringPaths.ConfigSingle, "App", "InputConfig"));
+3 -2
View File
@@ -1,10 +1,11 @@
using Cysharp.Threading.Tasks;
using UnityEngine; using UnityEngine;
namespace Code.UI namespace Code.UI
{ {
public interface IUIFactory public interface IUIFactory
{ {
Canvas CreateCanvas(); UniTask<Canvas> CreateCanvas();
UIElement CreatePanel(Canvas canvas, UIPanel uiPanel); UniTask<UIElement> CreatePanel(Canvas canvas, UIPanel uiPanel);
} }
} }
+3 -3
View File
@@ -1,10 +1,10 @@
using System;
namespace Code.UI namespace Code.UI
{ {
public interface IUIService public interface IUIService
{ {
void EnablePanel(); void SwitchPanelTo(UIPanel uiPanel, Action callback = null);
void DisablePanel();
void SwitchPanelTo(UIPanel uiPanel);
UIElement CurrentPanel { get; } UIElement CurrentPanel { get; }
LoadingPanel LoadingScreen { get; } LoadingPanel LoadingScreen { get; }
} }
+17 -6
View File
@@ -1,24 +1,35 @@
using Code.Configs;
using Cysharp.Threading.Tasks;
using Reflex.Attributes; using Reflex.Attributes;
using UnityEngine; using UnityEngine;
using UnityEngine.AddressableAssets;
using Object = UnityEngine.Object;
namespace Code.UI namespace Code.UI
{ {
public class UIFactory : IUIFactory public class UIFactory : IUIFactory
{ {
[Inject] private UIElement[] _uiPanelsPrefabs; private AssetReference _canvasAssetReference;
[Inject] private Canvas _canvasPrefab; private AssetReference[] _uiPanelsAssetReferences;
public Canvas CreateCanvas() public UIFactory(UIConfig uiConfig)
{ {
Canvas canvas = Object.Instantiate(_canvasPrefab); _canvasAssetReference = uiConfig.canvasAssetReference;
_uiPanelsAssetReferences = uiConfig.uiPanelsAssetReferences;
}
public async UniTask<Canvas> CreateCanvas()
{
// Canvas canvasPrefab = await Addressables.LoadAssetAsync<Canvas>(_canvasAssetReference);
Canvas canvas = (await Addressables.InstantiateAsync(_canvasAssetReference).ToUniTask()).GetComponent<Canvas>();
Object.DontDestroyOnLoad(canvas); Object.DontDestroyOnLoad(canvas);
return canvas; return canvas;
} }
public UIElement CreatePanel(Canvas canvas, UIPanel uiPanel) public async UniTask<UIElement> CreatePanel(Canvas canvas, UIPanel uiPanel)
{ {
return Object.Instantiate(_uiPanelsPrefabs[(int)uiPanel], canvas.transform); return (await Addressables.InstantiateAsync(_uiPanelsAssetReferences[(int)uiPanel], canvas.transform).ToUniTask()).GetComponent<UIElement>();
} }
} }
} }
+16 -7
View File
@@ -1,4 +1,7 @@
using System;
using Cysharp.Threading.Tasks;
using UnityEngine; using UnityEngine;
using Object = UnityEngine.Object;
namespace Code.UI namespace Code.UI
{ {
@@ -13,19 +16,25 @@ namespace Code.UI
public UIService(IUIFactory factory) public UIService(IUIFactory factory)
{ {
_factory = factory; _factory = factory;
_canvas = _factory.CreateCanvas(); Initialize();
}
private async void Initialize()
{
_canvas = await _factory.CreateCanvas();
LoadingScreen = (LoadingPanel) _factory.CreatePanel(_canvas,UIPanel.Loading); LoadingScreen = (LoadingPanel) await _factory.CreatePanel(_canvas,UIPanel.Loading);
} }
public void EnablePanel() => CurrentPanel.gameObject.SetActive(true); public async void SwitchPanelTo(UIPanel uiPanel, Action callback = null)
public void DisablePanel() => CurrentPanel.gameObject.SetActive(false);
public void SwitchPanelTo(UIPanel uiPanel)
{ {
if (!_canvas) await UniTask.WaitUntilValueChanged(_canvas, _ => !_canvas);
if(CurrentPanel) Object.Destroy(CurrentPanel.gameObject); if(CurrentPanel) Object.Destroy(CurrentPanel.gameObject);
CurrentPanel = _factory.CreatePanel(_canvas, uiPanel); CurrentPanel = await _factory.CreatePanel(_canvas, uiPanel);
callback?.Invoke();
} }
} }
} }