docs: add Unity and Godot setup guide
Validate ShrinkSDK Workspace / unity (push) Failing after 3m9s
Validate ShrinkSDK Workspace / unity (push) Failing after 3m9s
This commit is contained in:
+170
@@ -0,0 +1,170 @@
|
||||
# ShrinkSDK for Godot 4.6 C#
|
||||
|
||||
Godot 适配层使用共享 ShrinkSDK 运行时和 MSBuild/Cecil CodeGen。首期目标是 Godot 4.6.3 Mono、.NET 8,以及 Windows、Linux、macOS 桌面项目;不包含 Web、Android 和 iOS。
|
||||
|
||||
## 直接运行 Workspace 示例
|
||||
|
||||
使用 Godot 4.6.3 Mono 打开:
|
||||
|
||||
```text
|
||||
Godot/Samples/ShrinkSDK.Godot.Sample/project.godot
|
||||
```
|
||||
|
||||
首次打开前可以先完成导入和编译:
|
||||
|
||||
```powershell
|
||||
$godot = Join-Path $env:GODOT_HOME "godot_console.exe"
|
||||
& $godot --headless --editor --path .\Godot\Samples\ShrinkSDK.Godot.Sample --quit
|
||||
dotnet build .\Godot\Samples\ShrinkSDK.Godot.Sample\ShrinkSDK.Godot.Sample.csproj
|
||||
& $godot --headless --path .\Godot\Samples\ShrinkSDK.Godot.Sample
|
||||
```
|
||||
|
||||
`GODOT_HOME` 指向 Godot Mono 安装目录。成功运行时会输出 `SHRINK_GODOT_SMOKE_PASS`。
|
||||
|
||||
## 在现有 Godot C# 项目中安装
|
||||
|
||||
项目需要:
|
||||
|
||||
- Godot 4.6.3 Mono;
|
||||
- .NET 8 SDK;
|
||||
- `.csproj` 使用 `Godot.NET.Sdk/4.6.3`;
|
||||
- 项目级 `NuGet.Config` 保留既有源并加入 ShrinkSDK feed。
|
||||
|
||||
`NuGet.Config` 的最小增量如下:
|
||||
|
||||
```xml
|
||||
<configuration>
|
||||
<packageSources>
|
||||
<add key="ShrinkSDK" value="https://git.crash.work/api/packages/ShrinkSDK/nuget/index.json" />
|
||||
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
|
||||
</packageSources>
|
||||
</configuration>
|
||||
```
|
||||
|
||||
安装 Godot 宿主和所需模块:
|
||||
|
||||
```powershell
|
||||
dotnet add package ShrinkSDK.Godot --version 0.1.0
|
||||
dotnet add package ShrinkSDK.EventBus --version 2.1.0
|
||||
dotnet add package ShrinkSDK.Command --version 0.3.0
|
||||
dotnet add package ShrinkSDK.Network --version 0.3.0
|
||||
dotnet add package ShrinkSDK.DataSaver --version 2.3.0
|
||||
dotnet restore
|
||||
dotnet build
|
||||
```
|
||||
|
||||
只有实际使用的模块需要安装。远端源尚未发布目标版本时,可以直接运行 Workspace 示例,或把本仓库生成的 `Godot/Artifacts` 作为临时本地 NuGet 源;不要删除命令中的固定版本号改用浮动版本。
|
||||
|
||||
## Installer 插件
|
||||
|
||||
`ShrinkSDK.Godot.Installer` 的 nupkg 内包含完整的 `addons/shrinksdk`。取得 `0.1.1` 包后,将包内:
|
||||
|
||||
```text
|
||||
contentFiles/any/any/addons/shrinksdk
|
||||
```
|
||||
|
||||
复制到 Godot 项目的:
|
||||
|
||||
```text
|
||||
addons/shrinksdk
|
||||
```
|
||||
|
||||
然后在 Godot 中打开 `Project > Project Settings > Plugins`,启用 **ShrinkSDK Installer**。左侧 ShrinkSDK 面板可以:
|
||||
|
||||
- 查看项目直接安装的 ShrinkSDK NuGet 包及版本;
|
||||
- 安装、升级或移除模块;
|
||||
- 保留项目已有 NuGet 源和无关 `PackageReference`;
|
||||
- 执行 `dotnet restore/build`;
|
||||
- 查看 CodeGen 最后一次织入结果和生成的注册数量。
|
||||
|
||||
已安装版本高于面板目录版本时会显示 `Newer` 并禁用更新按钮,不会执行降级。
|
||||
|
||||
在 Workspace 中生成 Installer 包:
|
||||
|
||||
```powershell
|
||||
dotnet build .\Godot\Installer\ShrinkSDK.Godot.Installer.csproj -c Release
|
||||
dotnet pack .\Godot\Installer\ShrinkSDK.Godot.Installer.csproj -c Release --no-build -o .\Godot\Artifacts --include-symbols --include-source
|
||||
```
|
||||
|
||||
## 宿主节点
|
||||
|
||||
主场景根节点继承 `ShrinkGodotHost`,并在 `_Ready` 中先调用 `base._Ready()`:
|
||||
|
||||
```csharp
|
||||
using Cysharp.Threading.Tasks;
|
||||
using ShrinkSDK.Godot;
|
||||
|
||||
public partial class GameRoot : ShrinkGodotHost
|
||||
{
|
||||
public override void _Ready()
|
||||
{
|
||||
base._Ready();
|
||||
BootAsync().Forget();
|
||||
}
|
||||
|
||||
private async UniTaskVoid BootAsync()
|
||||
{
|
||||
RequireWoven(GetType().Assembly);
|
||||
await StartAppAsync();
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
宿主负责日志、主线程派发、时间、`user://` 持久化路径、截图、应用生命周期和网络 dispatch queue。使用 `ShrinkNetworkService` 时,通过 `AddNetworkService(service)` 交给宿主逐帧泵送,释放前调用 `RemoveNetworkService(service)`。
|
||||
|
||||
## CodeGen 织入
|
||||
|
||||
EventBus、Command、Network 和 App 的特性注册由 `ShrinkSDK.CodeGen` 在构建时生成。项目正常引用这些模块后,`buildTransitive` 会默认设置:
|
||||
|
||||
```xml
|
||||
<ShrinkCodeGenEnabled>true</ShrinkCodeGenEnabled>
|
||||
```
|
||||
|
||||
执行:
|
||||
|
||||
```powershell
|
||||
dotnet build
|
||||
```
|
||||
|
||||
日志应出现类似:
|
||||
|
||||
```text
|
||||
[ShrinkSDK.CodeGen] ... woven: instance=1, static=1, registry=2
|
||||
```
|
||||
|
||||
运行时可以在启动阶段验证程序集:
|
||||
|
||||
```csharp
|
||||
ShrinkGodotHost.RequireWoven(GetType().Assembly);
|
||||
```
|
||||
|
||||
缺少织入时应修复 NuGet 引用或构建配置,不要增加运行时反射扫描兜底。只有明确采用手工注册的普通 .NET 宿主才设置 `<ShrinkCodeGenEnabled>false</ShrinkCodeGenEnabled>`。
|
||||
|
||||
## 特性声明示例
|
||||
|
||||
```csharp
|
||||
using ShrinkCommand;
|
||||
using ShrinkEventBus;
|
||||
using ShrinkNetwork;
|
||||
|
||||
public readonly struct PlayerReadyEvent : IShrinkEvent { }
|
||||
|
||||
[ShrinkEventSubscriber]
|
||||
public sealed class PlayerReadySubscriber
|
||||
{
|
||||
[ShrinkSubscribe]
|
||||
private void OnPlayerReady(PlayerReadyEvent value) { }
|
||||
}
|
||||
|
||||
[ShrinkCommandSubscriber]
|
||||
public static class GameCommands
|
||||
{
|
||||
[ShrinkCommand("game/status")]
|
||||
public static void Status() { }
|
||||
}
|
||||
|
||||
[ShrinkNetworkMessage(41001, "game/player-ready")]
|
||||
public sealed class PlayerReadyMessage : IShrinkNetworkMessage { }
|
||||
```
|
||||
|
||||
实例订阅者使用 `EventBus.Attach(instance)` 绑定;静态订阅、Command、Network 和 App registry 由织入代码自动注册。
|
||||
Reference in New Issue
Block a user