A survival game may need a main menu, an in-game display, a pause menu, and a game-over menu. Building them requires more than switching screens: buttons need functions, game values need updating, and some elements change with player actions. Separating dynamic UI elements from static artwork lets the person assembling a Unity prefab identify what code must access. A fixed title can stay in the asset; a Start button needs a function assigned to it. section 8.0 section 8.1
Rather than design one reusable menu architecture before knowing future requirements, implement each menu for the data it actually needs. Share code only when it can be a static function that takes data, transforms it, and returns a result. This keeps a change to one menu from unexpectedly affecting another. section 8.1
A small state machine coordinates the menu scripts:
- Load saved metadata, initialize each menu visual with the data it needs, then select the initial main-menu state. section 8.6.1
- When switching, save the old state and set the new state in MetaData through Logic.SetMenuState(). section 8.6.1
- Hide the old menu and show the new one, handling board visibility separately where needed. Entering pause hides the board; entering game over leaves it visible. section 8.6.1
Listing 8.26 Method to set the menu state in Game
public void SetMenuState(MENU_STATE newMenuState)
{
MENU_STATE oldMenuState = m_metaData.MenuState;
Logic.SetMenuState(m_metaData, newMenuState);
if (oldMenuState == MENU_STATE.MAIN_MENU)
{
m_mainMenuVisual.Hide();
}
else if (oldMenuState == MENU_STATE.GAME_OVER)
{
Board.Hide();
m_gameOverVisual.Hide();
}
else if (oldMenuState == MENU_STATE.IN_GAME && newMenuState == MENU_STATE.PAUSE_MENU)
{
Board.Hide();
}
else if (oldMenuState == MENU_STATE.PAUSE_MENU)
{
m_pauseMenuVisual.Hide();
}
if (newMenuState == MENU_STATE.MAIN_MENU)
{
m_mainMenuVisual.Show();
}
else if (newMenuState == MENU_STATE.GAME_OVER)
{
m_gameOverVisual.Show();
}
else if (newMenuState == MENU_STATE.PAUSE_MENU)
{
m_pauseMenuVisual.Show();
}
else if (newMenuState == MENU_STATE.IN_GAME)
{
Board.Show();
}
}SetMenuState() uses explicit state checks to coordinate visual changes. The branches may look repetitive, but they make transition-specific behavior easy to see. section 8.6.1
Starting versus continuing
Both actions enter the IN_GAME menu state. Starting prepares a new game; continuing loads saved game data first. While that state is active, Update() ticks the board. The menu transition is shared, while the game data determines what play resumes. section 8.6.1
