This Unity project is a learning exercise focused on understanding dependencies in Unity and their proper management. The goal is to explore various approaches to referencing and using components like Inventory in Unity projects, highlighting their pros and cons.
-
Tile Collection System:
- Collect different types of tiles (
Tile_1andTile_2). - Destroy tiles upon collection and update the inventory dynamically.
- Collect different types of tiles (
-
Inventory Management:
- Tracks and displays the count of different tile types in real-time.
- Uses Unity's serialization system to auto-initialize the inventory list.
-
UI Update Mechanisms:
- Two UI systems to explore dependency management:
- Without Dependency Injection (direct references using
FindObjectOfType). - With Dependency Injection (references passed through script).
- Without Dependency Injection (direct references using
- Two UI systems to explore dependency management:
The primary purpose of this project is to demonstrate:
-
Serialization in Unity:
- How Unity automatically initializes public fields in MonoBehaviours.
-
Dependency Management:
- Comparing hardcoded dependencies vs. dependency injection approaches.
- Highlighting how dependency management impacts maintainability and scalability.
-
Real-Time Updates:
- Updating UI elements dynamically based on gameplay events like tile collection.
Manages the addition of tiles to the player's inventory. Handles destroying collected tiles and updating the inventory.
public class GameManager : MonoBehaviour { ... }- Adds tiles to the inventory (
Tile_1orTile_2). - Logs actions for debugging purposes.
Handles the player's inventory. Tracks all collected items and calculates counts for specific item types.
public class Inventory : MonoBehaviour { ... }- Uses a
List<string>to store collected items. - Dynamically calculates item counts using the
GetCountmethod.
Updates the inventory UI without dependency injection by repeatedly finding the Inventory object.
public class UIHandlerExpnsive : MonoBehaviour { ... }- Uses
FindObjectOfType<Inventory>in the Update method. - Demonstrates a less efficient approach to dependency management.
Updates the inventory UI using dependency injection to retrieve the Inventory reference once.
public class UIHandlerWithDependency : MonoBehaviour { ... }- Retrieves the Inventory reference in the
Startmethod. - Demonstrates a more efficient and maintainable approach to dependency management.
Unity auto-initializes public fields in MonoBehaviours. This project demonstrates that you can rely on Unity's serialization system for initializing List<string> fields in the Editor, but it's good practice to explicitly initialize fields when needed.
- Simple to implement.
- Can lead to performance overhead due to repetitive searches (
FindObjectOfType). - Harder to scale and maintain.
- Efficient and scalable.
- Reduces coupling between components.
- Improves maintainability and code clarity.
