https://github.com/lfeq/improved-game-manager
https://github.com/lfeq/improved-game-manager
Last synced: 11 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/lfeq/improved-game-manager
- Owner: lfeq
- License: mit
- Created: 2025-03-13T23:17:16.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-03-19T19:20:11.000Z (over 1 year ago)
- Last Synced: 2025-03-19T19:28:55.311Z (over 1 year ago)
- Language: ShaderLab
- Size: 2.44 MB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Application Manager
A robust Unity package for efficient game state management using Scriptable Objects and a State Machine pattern.
Managing game states (e.g., Main Menu, Gameplay, Pause, Game Over) usually involves enums and cumbersome switch
statements, leading to maintainability issues in large projects. This package simplifies state management by leveraging
Unity's Scriptable Objects, providing an organized and scalable solution.
### Traditional Approach to Game State Management
Developers often manage game states using enums and switch statements, which can become unwieldy as projects grow. A
typical implementation might look like this:
```csharp
public void ChangeGameState(GameState newState) {
if (m_gameState == newState) {
return;
}
m_gameState = newState;
switch (m_gameState) {
case GameState.None:
break;
case GameState.LoadMainMenu:
LoadMenu();
break;
case GameState.MainMenu:
break;
case GameState.LoadLevel:
LoadLevel();
break;
case GameState.Playing:
break;
case GameState.RestartLevel:
RestartLevel();
break;
case GameState.GameOver:
break;
case GameState.Credits:
break;
case GameState.QuitGame:
QuitGame();
break;
default:
throw new UnityException("Invalid Game State");
}
}
```
While functional, this approach can lead to large, hard-to-maintain scripts as the number of game states increases. To
address this issue, the Application Manager utilizes the State Machine pattern in combination with Unity's Scriptable
Objects, promoting a more modular and maintainable architecture.
---
## Features
- Easy creation and management of game states and events through Scriptable Objects.
- Built-in support for common states (scene changes, quitting, restarting scenes).
- Extendable architecture for custom game state implementations.
- Debug mode to track state changes effectively.
---
## Installation
1. Open Unity's Package Manager.
2. Add the required dependency via Git URL: `https://github.com/UnityCommunity/UnitySingleton.git`
3. Add this package via Git URL: `https://github.com/lfeq/Improved-Game-Manager.git`
4. Done!
---
## Usage
### Improved Usage
- You can create your new `BaseStates` with the editor window found in `Window/Application Manager/Create State`
Here you can create a new state, event and event listener all in one go, just assign the state type, the state name,
the path where you want to save your new state and click create. This will generate the `Event`, `EventListener` and
`State` for you and put them in separated folders. By default, the path is `Assets/Data/Application States/States`,
`Assets/Data/Application States/Application Events` and `Assets/Data/Application States/Application EventListeners`
respectively. Just remember to assign the state to transition to in the `EventListener` inspector.
### Basic Usage
1. Create a new State:
- Right-click in the Project window → `Application Manager/States/[State]`

2. Create an EventListener:
- Right-click in the Project window → `Application Manager/Application EventListeners/[EventListener]`
3. Create an Event:
- Right-click in the Project window → `Application Manager/Application Events/[Event]`
4. Link Event and EventListener
- Assign the Event to the EventListener's inspector.

5. Assign EventListener to State:
- Assign the Event to the EventListener's inspector.

6. Assign EventListener to State:
- Assign the Event to the EventListener's inspector.
### Custom States
You can create your custom states by inheriting from the `BaseState` class. This allows you to define custom logic for
more complex game states.
```csharp
[CreateAssetMenu(fileName = "New Example State", menuName = MENU_ROOT_NAME + "Example State")]
public class ExampleState : BaseState {
public override void OnEnter() {
base.OnEnter();
// Add custom logic here
}
public override void OnExit() {
base.OnExit();
// Add custom logic here
}
}
```
---
## How It Works
The Application Manager leverages the State Machine pattern combined with Unity’s Scriptable Objects for flexible state
management:
- StateMachine: Handles transitioning between different states.
- BaseState: Defines abstract states as Scriptable Objects.
- Events & EventListeners: Trigger state transitions via UnityEvents.
- ApplicationManager: A singleton managing the current game state.
### Architecture Overview:
- Each State inherits from a base ScriptableObject class.
- Events trigger registered EventListeners when raised.
- EventListeners invoke state transition logic defined by UnityEvents.
- StateMachine manages current states and ensures proper transitions.
### Benefits:
- Clean separation of state logic.
- Easy state creation without modifying existing code.
- Enhanced debugging capabilities.
- Reduction of boilerplate and repetitive code.

---
## Contributing
We welcome your contributions to improve Application Manager! To contribute:
1. Fork the repository.
2. Create your feature branch (git checkout -b feature/YourFeatureName).
3. Commit your enhancements (git commit -m 'Describe your feature clearly').
4. Push to your branch (git push origin feature/YourFeatureName).
5. Submit a Pull Request detailing your changes.