# unity_framework **Repository Path**: Raindrips/unity_framework ## Basic Information - **Project Name**: unity_framework - **Description**: unity 通用接口代码组件 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-06-24 - **Last Updated**: 2026-07-07 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Unity Game Core A reusable C# game framework for Unity 2022.3 LTS, providing modular systems for events, state machines, behavior trees, physics detection, pathfinding, object pooling, and more. ## Overview This framework is designed as a **local UPM package** (`com.unity_framework`) that can be embedded into any Unity project. It provides battle-tested, decoupled game systems under the `GameCore` namespace. ## Requirements - Unity 2022.3.x LTS - Input System 1.14.2+ - TextMesh Pro 3.0.7+ ## Installation This package is referenced as a local package in your `Packages/manifest.json`: ```json "com.unity_framework": "file:../package_my/unity_framework" ``` Or copy the `unity_framework` folder into your project's `Packages/` directory. ## Modules ### Core — Pure logic, no Unity dependencies | Module | Description | | ------------------------- | ----------------------------------------------------------------------------------------------------- | | `Core/Event/` | Static generic event bus (`EventManager`) — type-keyed publish/subscribe with no inheritance required | | `Core/ServiceLocator/` | Generic DI / service locator (`Register` / `Resolve` / `Unregister`) | | `Core/BehaviorTree/` | Behavior tree framework — pure C# nodes: Composite (Sequence, Parallel), Decorator, Leaf | | `Core/StateMachine/` | Multi-layer state machine — pure C# core with fluent builder, conditions, event bus, history | | `Core/INetworkSync.cs` | Network sync interface | | `Core/ICommand.cs` | Command pattern interface + `CommandRecord` | | `Core/ISaveable.cs` | Save/load interface | | `Core/SortArray.cs` | Generic sorted array with binary search | ### Utils — Static Unity utility classes | Module | Description | | ------------------------ | -------------------------------------------------------- | | `Utils/GameObjectUtils` | Static GameObject creation helpers | | `Utils/TagExtensions` | Extension methods on `GameObject` for tag operations | | `Utils/TagQuery` | Static tag query utilities (FindWithTag, FindInRadius) | | `Utils/AStarPathfinder` | Static A* pathfinding algorithm | ### Utilities — Non-static Unity wrappers | Module | Description | | ---------------------------- | ----------------------------------------------------------------------------------------------- | | `Utilities/Singleton` | Generic MonoBehaviour singleton — lazy-instantiated, auto-creates GameObject, DontDestroyOnLoad | | `Utilities/AStarGrid` | A* grid map with obstacle baking | | `Utilities/AStarNode` | A* pathfinding node data | | `Utilities/GridCanvas` | Pure-color grid canvas — engine-agnostic pixel buffer with `SetCell`, `Clear`, `FillRect` | | `Utilities/TransformUtils` | Transform helper methods (LookAt, etc.) | | `Utilities/WorldDrawing` | 3D world-space drawing component | | `Utilities/NormalVisualizer` | Scene view ground normal visualizer | | `Utilities/GlobalMethodInvoker` | Reflection-based global method invoker | | `Utilities/DraggableSprite` | 2D sprite drag-and-drop component | ### Manager — Singleton managers | Module | Description | | --------------------- | --------------------------------------------------------------------------------------------- | | `Manager/Audio/` | Audio manager singleton | | `Manager/Pool/` | Object pooling — active `ObjectPool` + passive `PassiveObjectPool` | | `Manager/TagManager/` | Multi-tag system — O(1) HashSet-based queries, custom inspector, TAG + AND/OR filters | | `Manager/FrameSync/` | Deterministic lockstep frame sync (`SimpleFrameManager`, `SimpleRecorder`) | | `Manager/UI/` | Simple UI manager | ### Module — Composite feature collections | Module | Description | | --------------------------------------- | ------------------------------------------------------------------------- | | `Module/Framework/Save/` | JSON save/load system (`SaveManager` + `ISaveable` interface) | | `Module/Framework/Replay/` | Command pattern + replay (`CommandExecutor`, `ICommand`, `ReplayManager`) | | `Module/Framework/Network/` | Network abstraction layer (`NetworkManager`, `INetworkSync`) | | `Module/Gameplay/Pathfinding/` | A* pathfinding tile data component | | `Module/Gameplay/BehaviorTree/Runtime/` | MonoBehaviour wrapper driving the behavior tree | | `Module/Gameplay/BehaviorTree/Example/` | Behavior tree usage examples | | `Module/Gameplay/StateMachine/Runtime/` | MonoBehaviour wrapper + ScriptableObject definition | | `Module/Gameplay/StateMachine/Example/` | State machine usage examples | | `Module/Gameplay/StateMachine/Editor/` | State machine debug window | | `UI/` | UI window system — layer management, modal blocking, transitions, object pool | | `UI/Core/` | UIManager, LayerManager, ModalManager, WindowManager, BaseWindow, WindowConfig | | `UI/Transition/` | Transition system — IUITransition, FadeTransition, NoopTransition | | `UI/Example/` | Example windows (Settings, ConfirmDialog) + auto-bootstrap | ### Component — MonoBehaviour components | Module | Description | | ----------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------- | | `Component/Physics/Cast/` | 3D physics detectors — Raycast, BoxCast, SphereCast, CapsuleCast, OverlapBox, OverlapSphere, OverlapCapsule | | `Component/Physics/Cast2D/` | 2D physics detectors — Raycast2D, BoxCast2D, CircleCast2D, CapsuleCast2D, OverlapBox2D, OverlapCircle2D, OverlapArea2D, OverlapCapsule2D, OverlapPoint2D | | `Component/Physics/RTSRaycastDetection` | RTS-style mouse click and box-select raycasting | | `Component/CameraFreeMove` | Free-look camera controller (WSAD + mouse) | | `Component/UniqueID` | GUID-based unique identifier | | `Component/Trcker` | Tick-based timer component | | `Component/PoolableObject` | Auto-return to pool on destroy | | `Component/TagComponent` | Per-GameObject tag storage | | `Component/GridDisplay` | Renders `GridCanvas` via Texture2D + RawImage | | `Component/GridRendererExample` | Grid renderer demo (A*, Game of Life, Snake) | ## Key Patterns ### Event System ```csharp // Subscribe EventManager.On(data => Debug.Log(data.Message)); // Publish EventManager.Publish(new MyEvent { Message = "Hello" }); ``` ### Service Locator ```csharp // Register ServiceLocator.Register(new MyService()); // Resolve var service = ServiceLocator.Resolve(); ``` ### Singleton ```csharp public class GameManager : Singleton { public void DoSomething() { } } // Access anywhere GameManager.Instance.DoSomething(); ``` ### Object Pooling ```csharp // Get from pool var obj = ObjectPool.Instance.Get(prefab, position, rotation); // Return to pool ObjectPool.Instance.Return(obj); ``` ### UI Window System ```csharp // Open a window UIManager.Instance.Open("Settings"); // Open with args UIManager.Instance.Open("Confirm", new ConfirmDialogData { title = "Confirm", message = "Are you sure?", callback = (yes) => Debug.Log(yes) }); // Close UIManager.Instance.Close("Settings"); // Close with fade transition UIManager.Instance.CloseWithTransition("Settings", TransitionType.Fade, 0.3f); // Check state bool isOpen = UIManager.Instance.IsOpen("Settings"); ``` ### Grid Renderer ```csharp // Initialize (via GridDisplay component or directly with GridCanvas) var canvas = new GridCanvas(columns: 40, rows: 30, cellSize: 16, spacing: 1); // Set cell color canvas.SetCell(5, 10, new Color32(255, 0, 0, 255)); // Batch operations canvas.Clear(new Color32(20, 20, 30, 255)); canvas.FillRect(0, 0, 10, 10, new Color32(0, 255, 0, 255)); canvas.DrawLine(0, 0, 20, 15, new Color32(255, 255, 0, 255)); canvas.DrawRect(5, 5, 20, 15, new Color32(255, 255, 255, 255)); // Flush to display (Unity) display.Flush(); ``` ## Namespaces | Namespace | Scope | | ----------------------- | ----------------------------------------------------- | | `GameCore` | Root — utilities (`Singleton`, `GlobalMethodInvoker`) | | `GameCore.Events` | Static event bus | | `GameCore.GridRenderer` | Grid canvas and display | | `GameCore.UI` | UI window system (layers, transitions, modal, pool) | | `GameCore.Managers` | Managers (Audio, Tag, Pool, FrameSync, UI) | | `Project.Core` | Service locator | | `Project.Save` | Save/load system | | `Project.Replay` | Command pattern + replay | | `Project.Network` | Network abstraction | ## Project Structure ``` unity_framework/ ├── Core/ # Pure logic, no Unity dependencies │ ├── BehaviorTree/ # Behavior tree nodes (Composite, Decorator, Leaf) │ ├── StateMachine/ # State machine core (conditions, transitions, layers) │ ├── Event/ # Static event bus │ ├── ServiceLocator/ # DI / service locator │ ├── INetworkSync.cs # Network interface │ ├── ICommand.cs # Command pattern interface │ ├── ISaveable.cs # Save/load interface │ └── SortArray.cs # Generic sorted array ├── Utils/ # Static Unity utility classes │ ├── GameObjectUtils.cs │ ├── TagExtensions.cs │ ├── TagQuery.cs │ └── AStarPathfinder.cs ├── Utilities/ # Non-static Unity wrappers │ ├── Singleton.cs │ ├── AStarGrid.cs, AStarNode.cs │ ├── GridCanvas.cs │ ├── TransformUtils.cs, WorldDrawing.cs │ ├── NormalVisualizer.cs, GlobalMethodInvoker.cs │ └── DraggableSprite.cs ├── UI/ # UI window system │ ├── Core/ # UIManager, WindowManager, LayerManager, ModalManager │ ├── Transition/ # FadeTransition, NoopTransition, TransitionManager │ └── Example/ # SettingsWindow, ConfirmDialog, ExampleUI, UIBootstrap ├── Manager/ # Singleton managers │ ├── Audio/ │ ├── FrameSync/ │ ├── Pool/ │ ├── TagManager/ │ └── UI/ ├── Module/ # Composite feature collections │ ├── Framework/ # Save, Replay, Network │ └── Gameplay/ # Pathfinding, BehaviorTree, StateMachine └── Component/ # MonoBehaviour components ├── Physics/Cast/ # 3D detectors ├── Physics/Cast2D/ # 2D detectors ├── CameraFreeMove.cs, UniqueID.cs, Trcker.cs ├── PoolableObject.cs, TagComponent.cs └── GridDisplay.cs, GridRendererExample.cs ```