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