unity-composition-pattern
Unity Composition Pattern
A fundamental design skill to prevent "Inheritance Hell." This pattern replaces 10 specific classes (e.g., RedStone, GoldStone) with one generic class that composes behaviors from ScriptableObject assets.
Core Features
- Has-A Logic: Objects define their properties through members (Configs) rather than their base class type.
- SO-to-Runtime Bridge: Uses ScriptableObjects as static data templates that generate dynamic runtime instances.
- Tuple Deconstruction: Uses C# ValueTuples to return multi-part data from composite objects without extra boilerplate.
- Hierarchy Flattening: Drastically reduces the number of C# files needed by offloading variations to Project Assets.
Core Files (Max 3)
EntityConfigSO.cs.txt: Base class for creating interchangeable data packets.ModularEntity.cs.txt: The generic container that holds one or more configurations.CompositionExample.cs.txt: Demonstrates resource harvesting with Tuple destructuring.
Usage
1. Identify "Is-A" vs "Has-A"
- Bad:
ElectricCar : Car : Vehicle(Inheritance) - Good:
Vehiclehas anEngineConfig(Composition)
2. Multi-Value Returns (Tuples)
Instead of internal state-tracking:
public (int gold, int weight) Gather() => (10, 5);
// Client destructures instantly:
var (g, w) = node.Gather();
3. Strategy via SO
Attach different EntityConfigSO assets to the same ModularEntity to change its identity in the Inspector.
More from muharremtozan/unity-agent-skills
unity-so-prefab-manager
Manages the structured relationship between ScriptableObjects (Data) and Prefabs (Logic/Visuals) in Unity 6. Follows the 'SO-to-Mono' Bridge pattern to ensure instance independence (e.g., individual health for identical robots) while maintaining a clean, data-driven architecture. Use when: (1) Creating new unit/item types, (2) Wiring SO data to Prefab MonoBehaviours, (3) Resolving data-sharing bugs where changing one SO affects all instances.
16unity-fsm
Specialized skill for implementing a robust, extensible Finite State Machine in Unity using the State and Strategy patterns. Based on the pattern by Adam Myhre (3D Platformer). Use when creating complex AI, player controllers, or any system requiring structured state management.
11unity-code-reviewer
Professional Unity C# Code Reviewer. Detects anti-patterns, performance leaks, and enforces project-specific architecture.
10unity-assembly-management
Manage project boundaries using Assembly Definitions (.asmdef) for faster compile times and modular architecture. Based on the patterns by Adam Myhre. Enforces responsibility-based organization and handles Runtime/Editor/Tests splits.
9unity-event-bus
Advanced code-driven event bus with reflection-based bootstrapping. Provides zero-setup global messaging.
9unity-ui-data-binding
Implementation of MVVM-style Data Binding for Unity UI Toolkit using the [CreateProperty] attribute and BindableProperty wrappers.
9