Chapter 8: Separating asset data and logic
This introduction explains why menu development benefits from separating dynamic UI asset data from static components and treating each menu’s needs independently.
After this chapter you will be able to
- Dynamic and static menu components. Distinguish menu components that code must access from those it need not change, using the main menu title and Start button as examples.
- Asset handoff and development delays. Explain how missing UI components or code can cause repeated handoffs between art and engineering, and why identifying dynamic assets for code access supports a smoother flow.
- Menu-specific DOD implementation. Explain why the section favors implementing menus separately around their specific data requirements instead of designing a reusable menu architecture first.
- Menu data before patterns. Explain why a DOD menu design should begin by identifying the data it has and modifies, rather than automatically creating arrays of buttons and text elements.
- Main menu code-facing data. Describe which parts of the main menu need code access and which can be set in the Unity editor. Explain when the Continue button should be interactable.
- Prefab reference trade-offs. Explain why finding UI objects by name or assigning public script variables through the prefab creates problems for this menu workflow.
- Artist-to-engineer prefab handoff. Justify the desired division of work between the UI artist and engineer and why it calls for separating asset data from script logic.
- GUIRef key contract. Explain how an artist uses GUIRef to assign keys to UI objects on a prefab and how an engineer uses those keys to retrieve references. Describe why keys should normally stay stable even when UI objects are renamed.
- Small reference array. Describe how GUIRef stores GameObject key/value references and justify using an array of structs rather than a dictionary for this menu data.
- Core gameplay and meta data. Explain why the best time belongs in meta data rather than GameData, including when the game needs to read and update it.
- Menu controller independence. Explain why MainMenuVisual is separate from the main menu UI prefab rather than attached to the GameObject it controls.
- One-time menu initialization. Describe what MainMenuVisual.Init() sets up and why the frequently used UI references and button listeners are established there.
- Continue button eligibility. Explain how Show() decides whether Continue is available and what game and save-data changes make that decision possible.
- Best time display. Describe how the main menu updates Best time when shown and why its time-formatting operation is placed in CommonVisual.
- Game Over Menu Separation. Explain how the game-over menu follows the main menu pattern: an artist-provided prefab supplies UI data through GUIRef, AssetManager.GetGameOverUI() retrieves an instance, and GameOverVisual handles initializing, showing, and hiding the menu.
- Game over menu initialization. Trace how Init() stores MetaData and GameData references, obtains the GameOverGUI UI GameObject through the asset manager, and uses its GUIRef component to prepare the time strings and button clicks.
- Game over menu display data. Explain that Show() uses references saved in Init() for MetaData, GameData, and the “Best time” and “Game time” strings, then uses the static CommonVisual helper to format a string as the main menu does.
- Game-over menu navigation. State that the game over menu has a method for returning to the main menu.
- Pause menu unpause transition. Explain how the pause menu unpauses the game and what handles the resulting menu visibility.
- In-game menu Board integration. Describe how the Board's in-game menu is changed: add BoardGUI, update Init() to obtain and wire up the UI, and update Tick() to display game time.
- Menu control data. Explain what the game loop needs to control the menus: references to the menu scripts and a way to know which menu is current.
- Menu state transition flow. Trace how SetMenuState() changes the current menu and its visual display, including a board-visibility exception.
- New and resumed gameplay. Distinguish how StartGame() and ContinueGame() prepare GameData before both switch to the in-game menu.
- Nested store GUIRefs. Explain how the store menu uses nested prefabs and GUIRefs to organize its UI components and reach them from the store's GUIRef.
- On demand inventory items. Explain how the menu can create inventory item UI on demand for a large inventory, and describe the memory and performance tradeoff.
- Asset Data Collaboration Flow. Explain how separating asset data from the asset helps artists hand engineers ready-to-use assets and reduces back-and-forth in the development flow.
- Independent Menus and Code Reuse. Justify implementing menus independently, and describe when their code can be shared in a static function.
What it builds on
- Separating loading by source (chapter 7) · The earlier separation of code by source is the comparison made for solving distinct problems separately.
- Struct vs. class - how data affects performance (chapter 2) · The earlier discussion of structs as a way to group data.
- The GameDataIO static class (chapter 7) · The earlier static class for game-data input and output.
- Common bugs caused by object pools (chapter 3) · Using a GameObject's active state as another kind of state can cause unintended bugs.
- Using the asset manager (chapter 7) · Using the AssetManager to obtain an asset.
- Backwards compatibility (chapter 7) · Keeping older save files loadable when saved GameData changes.
- The Board implementation (chapter 5) · Board's existing elapsed-time formatting.
- The asset manager (chapter 7) · The asset manager used to obtain the UI GameObject.
Section by section
Introduction
This introduction explains why menu development benefits from separating dynamic UI asset data from static components and treating each menu’s needs independently.
- Dynamic and static menu components: Distinguish menu components that code must access from those it need not change, using the main menu title and Start button as examples.
Why it matters: This distinction identifies which parts of a UI prefab engineers need to reach from code. - Asset handoff and development delays: Explain how missing UI components or code can cause repeated handoffs between art and engineering, and why identifying dynamic assets for code access supports a smoother flow.
Why it matters: Repeated fixes can turn a short menu task into a much longer one. - Independent menu design decisions: Justify considering each menu’s data requirements separately instead of imposing one generic solution, including the intended benefit for completed menus.
Why it matters: Menu-specific decisions aim to keep later additions from disrupting menus already approved by QA.
Key terms: dynamic components, static components, UI prefab, asset data, dynamic, data requirement, stand-alone, generic solution
8.1 Separating menu data from logic
The section describes the game’s existing menus and argues for implementing each menu according to its own data needs rather than designing a shared menu system in advance.
- Existing menu data needs: Describe when the three existing menus appear and distinguish the in-game timer from the other menus’ UI.
Why it matters: The menus’ different needs motivate treating them separately. - Menu-specific DOD implementation: Explain why the section favors implementing menus separately around their specific data requirements instead of designing a reusable menu architecture first.
Why it matters: Designing around actual menu data avoids committing to an abstraction that future menus may not fit. - Limits on menu code reuse: Explain why extensive code sharing between menus is discouraged and state the kind of shared function the section allows.
Why it matters: This constraint keeps changes to one menu from creating unexpected problems in another.
Key terms: main menu, in-game menu, game-over menu, in-game timer, Board, GameObjects, DOD, reusable menu architecture, data requirements, code reuse
8.2 The main menu implementation
The section cautions against choosing arrays by habit when designing a menu with DOD and explains when arrays can still be useful.
- Menu data before patterns: Explain why a DOD menu design should begin by identifying the data it has and modifies, rather than automatically creating arrays of buttons and text elements.
Why it matters: It keeps the menu design tied to what the menu actually needs. - Arrays for menu references: Describe when an array may help with a large menu and distinguish that use from cache-line optimization.
Why it matters: It distinguishes a convenient way to hold menu references from a performance claim the section does not make.
Key terms: data-oriented design, data, patterns, array, references, cache-line optimization
8.2.1 Understanding the asset data problem
This section identifies the main menu data that code must access and explains why separating prefab asset data from script logic would simplify the artist–engineer workflow.
- Main menu code-facing data: Describe which parts of the main menu need code access and which can be set in the Unity editor. Explain when the Continue button should be interactable.
Why it matters: Identifying what code actually needs keeps the menu’s asset references focused. - Prefab reference trade-offs: Explain why finding UI objects by name or assigning public script variables through the prefab creates problems for this menu workflow.
Why it matters: These drawbacks explain why simply accessing the existing prefab is not enough. - Artist-to-engineer prefab handoff: Justify the desired division of work between the UI artist and engineer and why it calls for separating asset data from script logic.
Why it matters: A clear handoff reduces risky prefab edits while letting code use the UI objects it needs. - Independent menu data: Explain why the menus can be treated as stand-alone rather than sharing awareness of one another’s data.
Why it matters: The section’s menu design does not need to optimize for accessing several menus at once.
Key terms: Best Time, Continue, New Game, tool time, GameData, GameObject.Find(), public variables, prefab, UI objects, asset data
8.2.2 Separating asset data from logic
The section shows how GUIRef lets artists assign stable keys to prefab UI references so engineers can retrieve them without depending on UI object names.
- GUIRef key contract: Explain how an artist uses GUIRef to assign keys to UI objects on a prefab and how an engineer uses those keys to retrieve references. Describe why keys should normally stay stable even when UI objects are renamed.
Why it matters: Stable keys let artists revise a prefab without forcing code changes for every UI object rename. - Key naming tradeoff: Justify clear, consistent GUIRef keys and explain when changing an existing key may be reasonable.
Why it matters: A stable key is useful only while it still communicates the intended UI reference. - Small reference array: Describe how GUIRef stores GameObject key/value references and justify using an array of structs rather than a dictionary for this menu data.
Why it matters: The expected amount and frequency of lookup work determine whether a more elaborate collection is worthwhile. - Missing key diagnostics: Explain why GUIRef logs an error when a requested key is absent.
Why it matters: An immediate error makes a missing prefab reference easier to detect during development or testing. - Type-specific lookup functions: Justify separate GUIRef retrieval functions for component types such as Buttons rather than retrieving a GameObject and calling GetComponent().
Why it matters: A little more lookup code can make menu loading and component-specific debugging simpler.
Key terms: GUIRef, prefab, key/value pair, key, GameObject.Find, contract, struct, array, dictionary, GetGameObject()
8.2.3 Introducing the meta data
The section separates data used outside core gameplay from GameData and introduces MetaData to hold the best time and menu state.
- Core gameplay and meta data: Explain why the best time belongs in meta data rather than GameData, including when the game needs to read and update it.
Why it matters: This distinction gives data used by menus and other non-gameplay features an appropriate home. - Menu state ownership: Describe why menu state is meta data and how the section proposes representing and changing it.
Why it matters: It keeps the current menu's state with other non-gameplay data without mixing data and logic. - Meta data class placement: State the roles assigned to MetaData, MetaDataIO, and Game in the proposed structure.
Why it matters: These roles show where non-gameplay data lives and which class handles saving and loading it.
Key terms: core gameplay, meta, best time, GameData, menu state, meta data, enum, logic, state machine, MetaData
8.2.4 The main menu implementation
This section implements a main menu controller that stays independent of its UI prefab, caches UI references, and refreshes the menu from game and meta data when shown.
- Menu controller independence: Explain why MainMenuVisual is separate from the main menu UI prefab rather than attached to the GameObject it controls.
Why it matters: The menu's visibility should not accidentally control whether its controller runs. - One-time menu initialization: Describe what MainMenuVisual.Init() sets up and why the frequently used UI references and button listeners are established there.
Why it matters: One-time setup avoids repeated allocation, lookups, and listener registration each time the menu opens. - Continue button eligibility: Explain how Show() decides whether Continue is available and what game and save-data changes make that decision possible.
Why it matters: Without the gameplay-state check, Continue could offer to resume a game that has already ended. - Best time display: Describe how the main menu updates Best time when shown and why its time-formatting operation is placed in CommonVisual.
Why it matters: The displayed record stays current while identical formatting code remains in one reusable place. - Menu method flexibility: Explain why the recurring Init(), Show(), and Hide() methods are not enforced through one interface for every menu.
Why it matters: A common naming pattern need not become a rigid contract that poorly fits individual menus. - Menu loading tradeoff: Describe the memory-versus-performance tradeoff of loading menu data in Show() and unloading it in Hide() instead of retaining it.
Why it matters: The section's once-only allocation approach may need to change when keeping every menu loaded uses too much memory.
Key terms: MainMenuVisual, UI prefab, active false, Unity’s lifecycle, Init(), MainMenuGUI, GUIRef, AssetManager, listener, ContinueButton
8.3 The game over menu implementation
The section applies the main menu’s data-and-logic separation to the game-over menu, identifying where its UI data comes from and what its visual script does.
- Game Over Menu Separation: Explain how the game-over menu follows the main menu pattern: an artist-provided prefab supplies UI data through GUIRef, AssetManager.GetGameOverUI() retrieves an instance, and GameOverVisual handles initializing, showing, and hiding the menu.
Why it matters: This separation lets the game-over menu use the same division of asset data and menu logic as the main menu.
Key terms: game-over UI prefab, GUIRef, AssetManager.GetGameOverUI(), GameOverVisual
8.3.1 Init()
The section describes how Init() prepares the game-over menu by storing data and UI references and wiring its buttons.
- Game over menu initialization: Trace how Init() stores MetaData and GameData references, obtains the GameOverGUI UI GameObject through the asset manager, and uses its GUIRef component to prepare the time strings and button clicks.
Why it matters: This setup gives the menu the references and button events it needs before it is shown.
Key terms: Init(), MetaData, GameData, GameOverGUI, asset manager singleton, GUIRef
8.3.2 Show()
Showing the game over menu uses references saved during Init() and a shared helper to format its time strings.
- Game over menu display data: Explain that Show() uses references saved in Init() for MetaData, GameData, and the “Best time” and “Game time” strings, then uses the static CommonVisual helper to format a string as the main menu does.
Why it matters: It shows how the game over menu reuses initialized references and shared visual formatting.
Key terms: Show(), Init(), MetaData, GameData, Best time, Game time
8.3.3 Hide()
8.3.4 goToMainMenu()
This subsection identifies a game-over-menu method that returns to the main menu.
- Game-over menu navigation: State that the game over menu has a method for returning to the main menu.
Why it matters: It identifies the navigation this menu must support without requiring details deferred to a later section.
Key terms: game over menu, main menu
8.4 The pause menu implementation
The pause menu uses one button to return the menu state to IN_GAME, leaving menu visibility to the state machine.
- Pause menu unpause transition: Explain how the pause menu unpauses the game and what handles the resulting menu visibility.
Why it matters: It separates the pause button’s action from responsibility for which menu is visible.
Key terms: pause menu, IN_GAME, menu state, state machine
8.5 The in-game menu implementation
This section updates the Board-controlled in-game menu to use GUIRef and the same asset-based approach as the other menus.
- In-game menu Board integration: Describe how the Board's in-game menu is changed: add BoardGUI, update Init() to obtain and wire up the UI, and update Tick() to display game time.
Why it matters: It brings the Board-controlled menu into line with the other menus while keeping its pause control and game-time display working.
Key terms: BoardGUI, GUIRef, asset manager, Init(), Tick(), CommonVisual
8.6 Connecting the menus in the game loop
The game loop can control the menus if it has references to their scripts and knows which menu is current.
- Menu control data: Explain what the game loop needs to control the menus: references to the menu scripts and a way to know which menu is current.
Why it matters: These are the pieces of information the game loop needs to drive the menus.
Key terms: game loop, menu scripts, references
8.6.1 Referencing the menus
The Game script initializes the menus, switches between them using menu state, and enters gameplay either with new data or a saved game.
- Menu setup in Game: Describe how Game references and initializes the menus, including when saved meta data is loaded.
Why it matters: This setup gives the menus their data before the player sees the main menu. - Menu state transition flow: Trace how SetMenuState() changes the current menu and its visual display, including a board-visibility exception.
Why it matters: The stored state and visible menus must change together so the player sees the intended screen. - Readable menu conditionals: Justify the section’s choice of explicit if/else checks over an indirect menu dispatch structure.
Why it matters: The choice prioritizes code whose menu behavior a developer can inspect and debug directly. - New and resumed gameplay: Distinguish how StartGame() and ContinueGame() prepare GameData before both switch to the in-game menu.
Why it matters: Starting fresh and resuming must supply different GameData before showing the same in-game view.
Key terms: Game script, Start(), meta data, menu scripts, state machine, MetaData.MenuState, SetMenuState(), Logic.SetMenuState(), if/else, switch
8.7 Building complex menus
This section shows how to build a complex Unity store menu from nested prefabs and GUIRefs, then weighs a memory-saving approach for large inventories against its performance cost.
- Nested store GUIRefs: Explain how the store menu uses nested prefabs and GUIRefs to organize its UI components and reach them from the store's GUIRef.
Why it matters: This arrangement lets menu code reach UI components through a hierarchy of references rather than one flat set. - Store menu lifecycle: Describe what Init(), Show(), and Hide() do for the store menu when its item UI is already present.
Why it matters: The lifecycle separates menu setup from filling in current store and inventory information. - On demand inventory items: Explain how the menu can create inventory item UI on demand for a large inventory, and describe the memory and performance tradeoff.
Why it matters: For a large inventory, the menu can avoid keeping a huge set of item objects in memory at the cost of work when entering and leaving the store.
Key terms: nested prefabs, GUIRef, GameObject containers, Init(), Show(), Hide(), scrolling container, item prefab, asset manager, garbage collector
8.8 Conclusion
The section concludes that separating asset data streamlines collaboration, while independent menus and restrained code sharing keep menu code simpler.
- Asset Data Collaboration Flow: Explain how separating asset data from the asset helps artists hand engineers ready-to-use assets and reduces back-and-forth in the development flow.
Why it matters: A cleaner handoff lets each discipline move forward without repeated art–engineering revisions. - Independent Menus and Code Reuse: Justify implementing menus independently, and describe when their code can be shared in a static function.
Why it matters: Keeping menus independent makes changes less likely to affect other menus or expand QA's testing work. - Simple Code Benefits: State the section's reasons for favoring clean, simple code and less code.
Why it matters: Simplicity is presented as a benefit for both maintainability and performance.
Key terms: asset data, assets, development flow, menus, static function, DOD architecture, code complexity, performance
8.9 Summary
Summarizes how separating menu asset data from logic, keeping metadata distinct from gameplay data, and organizing menus independently can simplify development and debugging.
Concepts explained in this chapter
- menu systemA coordinated collection of game user-interface menus and the logic governing their transitions and interactions.
- asset dataAsset data is game content and configuration data representing resources loaded or assigned for use by a game.
- data-oriented programmingAn approach to structuring software around the flow and transformation of data rather than relationships between objects.
- finite-state machineA computational model that represents behavior as a finite set of states and transitions between them.
- prefabA Unity asset template that stores a reusable GameObject hierarchy and its component configuration.
- arrayAn indexed sequence of elements, typically stored in contiguous memory.
- complexity metricA measure of the structural intricacy and difficulty of understanding, modifying, or executing software code.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.
