A modular game-state coordinator for Unity.
Drives the high-level state machine, orchestrates chapter/mission transitions, and manages pause/resume.
Supports JSON chapter manifests for modding.
- State machine —
MainMenu,Loading,Playing,Paused,GameOver,VictorywithChangeState()transitions - Pause / Resume — optional
Time.timeScalecontrol viaPause()/Resume() - Chapter registry — define chapters (id, display name, scene name, ordering index, required flags) in the Inspector
- Chapter loading —
LoadChapter(id),LoadChapter(index),LoadNextChapter(),StartNewGame() - Unlock checks —
IsChapterUnlocked(id)respectsrequiredFlagsevaluated against SaveManager flags - JSON / Modding — load and merge chapter definitions from
StreamingAssets/game_config/at startup; JSON entries override Inspector entries by id and can add new ones - SaveManager integration — flag-based chapter unlock evaluation; auto-save on
StartNewGame()(activated viaGAMEMANAGER_SM) - MapLoaderFramework integration — chapter transitions routed through MapLoader (activated via
GAMEMANAGER_MLF) - EventManager integration — state changes and chapter loads broadcast as named GameEvents (activated via
GAMEMANAGER_EM) - StateManager integration —
GameStatechanges are automatically mapped toAppStateby StateManager'sGameManagerBridge(consumed viaSTATEMANAGER_GM) - LoadScreenManager integration —
GameManagerLoadScreenBridgein LoadScreenManager auto-shows the load screen onOnBeforeChapterLoadand hides it onOnAfterChapterLoad(activated viaLOADSCREENMANAGER_GMin the LoadScreenManager module) - Custom Inspector — live state display, per-chapter load buttons, state controls
- Odin Inspector integration —
SerializedMonoBehaviourbase for full Inspector serialization of complex types; runtime-display fields marked[ReadOnly]in Play Mode (activated viaODIN_INSPECTOR)
-
Open Window → Package Manager
-
Click + → Add package from git URL…
-
Enter:
https://github.com/RolandKaechele/GameManager.git
git clone https://github.com/RolandKaechele/GameManager.git Assets/GameManagercd Assets/GameManager
npm installpostinstall.js creates the required StreamingAssets/ folder under Assets/ and optionally copies example JSON files.
- Create a persistent manager GameObject (or reuse an existing one).
- Attach
GameManager. - Define chapters in the Inspector or enable JSON loading.
- Set
startingChapterIdto the first chapter's id. - Add
DontDestroyOnLoad(gameObject)if the manager should persist across scenes.
| Field | Default | Description |
|---|---|---|
initialState |
MainMenu |
State set on Awake |
chapters |
(empty) | Chapter definitions array |
startingChapterId |
"chapter_01" |
ID loaded by StartNewGame() |
loadChaptersFromJson |
false |
Merge from JSON on Awake |
chaptersJsonPath |
"game_config/" |
Folder relative to StreamingAssets/ containing .json files to merge. Falls back to single-file mode if the value points to an existing file. |
controlTimeScale |
true |
Set Time.timeScale on Pause/Resume |
var gm = FindFirstObjectByType<GameManager.Runtime.GameManager>();
// Start a new game
gm.StartNewGame();
// Load a specific chapter
gm.LoadChapter("chapter_05");
// Load by list index
gm.LoadChapter(4);
// Called from chapter scene init code when the scene is fully ready
gm.NotifyChapterLoaded();
// Advance to the next chapter
gm.LoadNextChapter();gm.OnStateChanged += state => Debug.Log($"State: {state}");
gm.OnBeforeChapterLoad += id => Debug.Log($"Loading: {id}");
gm.OnAfterChapterLoad += id => Debug.Log($"Ready: {id}");
gm.OnGameOver += () => ShowGameOverScreen();
gm.OnVictory += () => ShowVictoryScreen();gm.Pause(); // GameState.Paused, Time.timeScale = 0
gm.Resume(); // GameState.Playing, Time.timeScale = 1
gm.TriggerGameOver(); // GameState.GameOver
gm.TriggerVictory(); // GameState.VictoryEnable loadChaptersFromJson and place one or more .json files in StreamingAssets/game_config/.
All *.json files in the folder are loaded and merged by id at startup.
Example: StreamingAssets/game_config/main.json
{
"chapters": [
{
"id": "chapter_01",
"displayName": "Angriff der grünen Spinnen",
"sceneName": "Chapter01",
"index": 1,
"requiredFlags": []
},
{
"id": "chapter_02",
"displayName": "Tödlicher Nebel",
"sceneName": "Chapter02",
"index": 2,
"requiredFlags": ["chapter_01_complete"]
}
]
}JSON and Inspector entries are merged by id. JSON entries override Inspector entries with the same id.
New ids in JSON are appended to the list and sorted by index.
Add GAMEMANAGER_SM to Edit → Project Settings → Player → Scripting Define Symbols.
IsChapterUnlocked(id)evaluates eachrequiredFlagsentry againstSaveManager.IsSet(flag).StartNewGame()triggers a save on the active slot before loading the starting chapter.
Requires SaveManager in the project.
| Member | Description |
|---|---|
ChangeState(state) |
Transition to a new GameState |
Pause() |
Transition to Paused; sets Time.timeScale = 0 if enabled |
Resume() |
Transition to Playing; restores Time.timeScale |
TriggerGameOver() |
Transition to GameOver; fires OnGameOver |
TriggerVictory() |
Transition to Victory; fires OnVictory |
StartNewGame() |
Load startingChapterId (saves first if GAMEMANAGER_SM) |
LoadChapter(id) |
Load chapter by id |
LoadChapter(index) |
Load chapter by list index |
LoadNextChapter() |
Load the chapter after the current one; triggers Victory if at end |
NotifyChapterLoaded() |
Call from scene init — transitions to Playing, fires OnAfterChapterLoad |
GetChapter(id) |
Returns ChapterDefinition or null |
IsChapterUnlocked(id) |
True if all required flags are satisfied |
CurrentState |
Current GameState |
CurrentChapterId |
Id of the active chapter (null if none) |
IsPlaying |
True when state is Playing |
Chapters |
IReadOnlyList<ChapterDefinition> (merged) |
OnStateChanged |
event Action<GameState> |
OnBeforeChapterLoad |
event Action<string> — chapter id |
OnAfterChapterLoad |
event Action<string> — chapter id |
OnGameOver |
event Action |
OnVictory |
event Action |
Requires GAMEMANAGER_SM define and SaveManager.
Requires GAMEMANAGER_MLF define. Chapter scene transitions call MapLoaderFramework.Runtime.MapLoader.LoadScene(sceneName).
Requires GAMEMANAGER_EM define. The following named GameEvents are fired:
| Event name | When |
|---|---|
GameStateChanged |
State machine transitions; value = state name |
ChapterLoad |
Before chapter scene loads; value = chapter id |
ChapterLoaded |
After NotifyChapterLoaded(); value = chapter id |
GameOver |
TriggerGameOver() |
Victory |
TriggerVictory() |
Requires ODIN_INSPECTOR define (standard Odin Inspector scripting define). Inherits from SerializedMonoBehaviour for full Inspector serialization; runtime-display fields are marked [ReadOnly].
Open via JSON Editors → Game Manager in the Unity menu bar, or via the Open JSON Editor button in the GameManager Inspector.
| Action | Result |
|---|---|
| Load | Reads StreamingAssets/game_config.json; creates the file if missing |
| Edit | Add / remove / reorder chapter definitions using the Inspector list |
| Save | Writes back to StreamingAssets/game_config.json and calls AssetDatabase.Refresh() |
With ODIN_INSPECTOR active, the list uses Odin's enhanced drawer (drag-to-sort, collapsible entries).
| Dependency | Required | Notes |
|---|---|---|
| Unity 2022.3+ | ✓ | |
| SaveManager | optional | Required when GAMEMANAGER_SM is defined |
| MapLoaderFramework | optional | Required when GAMEMANAGER_MLF is defined |
| EventManager | optional | Required when GAMEMANAGER_EM is defined |
| Odin Inspector | optional | Required when ODIN_INSPECTOR is defined |
https://github.com/RolandKaechele/GameManager
MIT — see LICENSE.