feat: make Unity integration optional

This commit is contained in:
cneicy committed 2026-10-03 23:36:43 +08:00
1 parent a92c9d2cea
commit 240d24ba7a
27 files changed
+916 -69

No files matched your search

+62 -2
View File
@@ -1,9 +1,69 @@
# ShrinkEventBus 2.0
# ShrinkEventBus
ShrinkEventBus 2.0 是面向 Unity、普通 C# 宿主和 Entities/Burst 生产端的统一事件总线。运行时只有一种事件模型、一个订阅入口和一组发布 API;不同 Bus 只表达生命周期、所有权与调度策略。
Godot 和普通 .NET 项目安装 `ShrinkSDK.EventBus`;可打包源码位于 `DotNet~`,订阅注册由 `ShrinkSDK.CodeGen` 在构建时织入。
## Unity 安装
要求 Unity 2022.3 或更新版本。在 Package Manager 中使用 **Add package from git URL**,依次安装:
```text
https://github.com/Cysharp/UniTask.git?path=src/UniTask/Assets/Plugins/UniTask#7c0f199fe0d3fc528024488ccd671e6c7b27745b
https://github.com/cneicy/ShrinkEventBus.git
```
第一个地址固定 UniTask 2.5.10。Mono.Cecil 1.11.4 由 Unity 官方 registry 自动解析;无需配置 ShrinkSDK registry。需要锁定 EventBus 版本时,在 Git URL 后添加 `#<commit>`。
### 可选 ShrinkSDK 集成
| 已安装的共享包 | 行为 |
|---|---|
| 无 | 使用包内调度接口和 EventBus 专用 ILPostProcessor |
| Runtime Abstractions 0.1.0+ | 额外启用共享主线程调度器适配程序集 |
| Shared CodeGen 0.2.1+ | 停用包内织入,交给共享 CodeGen 统一生成订阅 |
| 两者都有 | 同时启用上述两项集成 |
通过 asmdef `versionDefines` 自动生成 `SHRINK_EVENTBUS_HAS_SHARED_RUNTIME` 和 `SHRINK_EVENTBUS_HAS_SHARED_CODEGEN`,不需要手工设置宏。两个共享包由完整工程自行安装,EventBus 不再强制拉取它们。
Unity 自定义调度器现在实现 `IShrinkEventBusMainThreadDispatcher`。已有的共享 `IShrinkMainThreadDispatcher` 实现使用以下适配方式:
```csharp
using ShrinkEventBus;
using ShrinkEventBus.Integration;
ShrinkEventBusRuntime.ConfigureMainThreadDispatcher(
new ShrinkSharedMainThreadDispatcher(sharedDispatcher));
```
使用自定义 asmdef 的业务程序集还需引用 `ShrinkEventBus.Integration.Runtime`。默认 UniTask PlayerLoop 调度无需配置;请在创建需要自定义调度的 Bus 前完成配置。此调整只影响 Unity,现有 .NET/Godot 配置接口保持不变。
### 最小使用示例
业务 asmdef 引用 `ShrinkEventBus.Runtime`,使用 UniTask 时再引用 `UniTask`。无需 `partial`、反射注册或手工生成绑定:
```csharp
using ShrinkEventBus;
using UnityEngine;
public readonly struct PlayerJoined : IShrinkEvent { }
[ShrinkEventSubscriber]
public sealed class PlayerHandlers
{
[ShrinkSubscribe]
private void OnJoined(PlayerJoined value) => Debug.Log("Player joined");
}
// 在 Unity 主线程执行;binding 应由实际宿主持有并在生命周期结束时释放。
using (var binding = EventBus.Attach(new PlayerHandlers()))
{
EventBus.Post(new PlayerJoined());
}
```
包内 `Development~/UnityProject` 是独立验证工程,包含外部消费程序集的同步/异步订阅测试。使用 Unity Test Runner 运行 EditMode 和 PlayMode 测试;包本身的测试通过 manifest 的 `testables` 开启。
## 核心契约
```csharp
@@ -181,7 +241,7 @@ ShrinkEventBusBenchmark.Run(
- 外部 Mod DLL 必须携带生成合同;没有合同的生产 DLL 不自动反射注册。
- Editor 诊断是可选观察层,不参与正式无诊断热路径。
架构审计与来源说明见 `Docs/JustAnyProjectArchitectureAudit.md` 和根目录 `DESIGN.md`。
Unity 独立安装不包含 Entities/Burst 适配包;需要时由宿主额外安装。
## 可复用能力