[Fix] Board Optimization, Fix Screen Loading

This commit is contained in:
2026-06-07 00:57:20 +07:00
parent e487795e6f
commit 285c11597a
9 changed files with 117 additions and 94 deletions
@@ -1,4 +1,3 @@
using Minesweeper.Config;
using Minesweeper.Presentation.Views;
using UnityEngine;
@@ -11,7 +10,12 @@ namespace Minesweeper.Presentation.Factories
private const string PausePanelName = "PausePanel";
private const string ResultPanelName = "ResultPanel";
public MinesweeperScreenRefs Spawn(Transform contentRoot, MinesweeperScreenCatalog catalog)
public MinesweeperScreenRefs Spawn(
Transform contentRoot,
MainMenuView mainMenuPrefab,
BoardView boardPrefab,
PauseView pausePrefab,
ResultView resultPrefab)
{
if (contentRoot == null)
{
@@ -19,47 +23,42 @@ namespace Minesweeper.Presentation.Factories
return default;
}
if (catalog == null || !catalog.IsValid)
if (mainMenuPrefab == null || boardPrefab == null || pausePrefab == null || resultPrefab == null)
{
Debug.LogError("Minesweeper screen bootstrap failed: screen catalog prefab references are incomplete.");
Debug.LogError("Minesweeper screen bootstrap failed: screen prefab references are incomplete.");
return default;
}
ClearContent(contentRoot);
var mainMenu = SpawnScreen(catalog.MainMenuPanelPrefab, contentRoot, MainMenuPanelName, 0);
var board = SpawnScreen(catalog.BoardGridPrefab, contentRoot, BoardGridName, 1);
var pause = SpawnScreen(catalog.PausePanelPrefab, contentRoot, PausePanelName, 2);
var result = SpawnScreen(catalog.ResultPanelPrefab, contentRoot, ResultPanelName, 3);
var mainMenuView = SpawnScreen(mainMenuPrefab, contentRoot, MainMenuPanelName, 0);
var boardView = SpawnScreen(boardPrefab, contentRoot, BoardGridName, 1);
var pauseView = SpawnScreen(pausePrefab, contentRoot, PausePanelName, 2);
var resultView = SpawnScreen(resultPrefab, contentRoot, ResultPanelName, 3);
var mainMenuView = RequireComponent<MainMenuView>(mainMenu.transform, MainMenuPanelName);
if (mainMenuView != null)
{
mainMenuView.BindRoot(mainMenu);
mainMenuView.BindRoot(mainMenuView.gameObject);
}
var boardView = RequireComponent<BoardView>(board.transform, BoardGridName);
var pauseView = RequireComponent<PauseView>(pause.transform, PausePanelName);
var resultView = RequireComponent<ResultView>(result.transform, ResultPanelName);
var refs = new MinesweeperScreenRefs(
mainMenuView,
boardView,
pauseView,
resultView);
mainMenu.SetActive(false);
board.SetActive(false);
pause.SetActive(false);
result.SetActive(false);
mainMenuView.gameObject.SetActive(false);
boardView.gameObject.SetActive(false);
pauseView.gameObject.SetActive(false);
resultView.gameObject.SetActive(false);
return refs;
}
private static GameObject SpawnScreen(GameObject prefab, Transform parent, string expectedName, int siblingIndex)
private static T SpawnScreen<T>(T prefab, Transform parent, string expectedName, int siblingIndex) where T : Component
{
var instance = Object.Instantiate(prefab, parent, false);
instance.name = expectedName;
instance.gameObject.name = expectedName;
instance.transform.SetSiblingIndex(siblingIndex);
Stretch(instance.GetComponent<RectTransform>());
return instance;
@@ -75,17 +74,6 @@ namespace Minesweeper.Presentation.Factories
}
}
private static T RequireComponent<T>(Transform root, string ownerName) where T : Component
{
var component = root.GetComponent<T>();
if (component == null)
{
Debug.LogError($"Minesweeper screen bootstrap failed: '{ownerName}' is missing {typeof(T).Name}.");
}
return component;
}
private static void Stretch(RectTransform rectTransform)
{
if (rectTransform == null)